Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
var events = require('events'),
http = require('http'),
util = require('util'),
url = require('url'),
httpProxy = require('../node-http-proxy');

//
Expand Down Expand Up @@ -228,7 +229,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
if (this.changeOrigin) {
outgoing.headers.host = this.target.host + ':' + this.target.port;
}

//
// Open new HTTP request to internal resource with will act
// as a reverse proxy pass
Expand All @@ -248,11 +249,15 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
}

if ((response.statusCode === 301) || (response.statusCode === 302)) {
if (self.source.https && !self.target.https) {
response.headers.location = response.headers.location.replace(/^http\:/, 'https:');
}
if (self.target.https && !self.source.https) {
response.headers.location = response.headers.location.replace(/^https\:/, 'http:');
// Only change protocol if url under target host
var parsedLocation = url.parse(response.headers.location);
if (parsedLocation.host === req.headers.host) {
if (self.source.https && !self.target.https) {
response.headers.location = response.headers.location.replace(/^http\:/, 'https:');
}
if (self.target.https && !self.source.https) {
response.headers.location = response.headers.location.replace(/^https\:/, 'http:');
}
}
}

Expand Down