Skip to content

http-proxy should not modify the protocol in redirect request for external sites. #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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