Skip to content

Commit

Permalink
fix proxy issues on https @imports
Browse files Browse the repository at this point in the history
When an @import used a https url, the false protocol was tried to be used for the http proxy.

fixes clean-css#741
  • Loading branch information
wzrdtales authored and silverwind committed Sep 25, 2016
1 parent 567e87a commit cd974a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/imports/inliner.js
Expand Up @@ -268,7 +268,10 @@ function inlineRemoteResource(importedFile, mediaQuery, context) {

context.visited.push(importedUrl);

var get = importedUrl.indexOf('http://') === 0 ?
var proxyProtocol = context.inliner.request.protocol || context.inliner.request.hostname;
var get =
((proxyProtocol && proxyProtocol.indexOf('https://') !== 0 ) ||
importedUrl.indexOf('http://') === 0) ?
http.get :
https.get;

Expand All @@ -287,8 +290,13 @@ function inlineRemoteResource(importedFile, mediaQuery, context) {
}

var requestOptions = override(url.parse(importedUrl), context.inliner.request);
if (context.inliner.request.hostname !== undefined)
if (context.inliner.request.hostname !== undefined) {

//overwrite as we always expect a http proxy currently
requestOptions.protocol = context.inliner.request.protocol || 'http:';
requestOptions.path = requestOptions.href;
}


get(requestOptions, function (res) {
if (res.statusCode < 200 || res.statusCode > 399) {
Expand Down
47 changes: 47 additions & 0 deletions test/protocol-imports-test.js
Expand Up @@ -591,6 +591,53 @@ vows.describe('protocol imports').addBatch({
this.proxyServer.destroy();
}
}
}).addBatch({
'of a proxied resource with https url': {
topic: function () {
var self = this;
nock.enableNetConnect();

this.proxied = false;

this.reqMocks = nock('http://assets.127.0.0.1')
.get('/sslstyles.css')
.reply(200, 'a{color:red}');

var proxy = httpProxy.createProxyServer();
this.proxyServer = http.createServer(function (req, res) {
self.proxied = true;
self.isSSL = req.url.indexOf('https://') === 0;
proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function () {});
});
this.proxyServer.listen(8080, function () {
var options = {
inliner: {
request: {
hostname: '127.0.0.1',
port: 8080
}
}
};

new CleanCSS(options).minify('@import url(https://assets.127.0.0.1/sslstyles.css);', self.callback);
});
enableDestroy(this.proxyServer);
},
'proxies the connection': function () {
assert.isTrue(this.proxied);
},
'ssl was used': function () {
assert.isTrue(this.isSSL);
},
'gets right output': function (errors, minified) {
assert.equal(minified.styles, 'a{color:red}');
},
teardown: function () {
assert.isTrue(this.reqMocks.isDone());
nock.cleanAll();
this.proxyServer.destroy();
}
}
}).addBatch({
'of a proxied resource via env variables': {
topic: function () {
Expand Down

0 comments on commit cd974a3

Please sign in to comment.