From 9a61d3679c50c8021704c11dedd0822a578e413d Mon Sep 17 00:00:00 2001 From: Tobias Gurtzick Date: Sun, 27 Mar 2016 11:25:49 +0200 Subject: [PATCH] fix proxy issues on https @imports When an @import used a https url, the false protocol was tried to be used for the http proxy. fixes #741 --- lib/imports/inliner.js | 12 +++++++-- test/protocol-imports-test.js | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/imports/inliner.js b/lib/imports/inliner.js index c6686a706..cca126b5d 100644 --- a/lib/imports/inliner.js +++ b/lib/imports/inliner.js @@ -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; @@ -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) { diff --git a/test/protocol-imports-test.js b/test/protocol-imports-test.js index 35fa86ac0..54e3ede4c 100644 --- a/test/protocol-imports-test.js +++ b/test/protocol-imports-test.js @@ -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 () {