fix proxy issues on https @imports
authorTobias Gurtzick <magic@wizardtales.com>
Sun, 27 Mar 2016 09:25:49 +0000 (11:25 +0200)
committerTobias Gurtzick <magic@wizardtales.com>
Sun, 27 Mar 2016 09:25:49 +0000 (11:25 +0200)
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
test/protocol-imports-test.js

index c6686a7..cca126b 100644 (file)
@@ -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) {
index 35fa86a..54e3ede 100644 (file)
@@ -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 () {