Skip to content

Commit

Permalink
Merge 70ed400 into 22e81fc
Browse files Browse the repository at this point in the history
  • Loading branch information
mbargiel committed May 25, 2022
2 parents 22e81fc + 70ed400 commit e7ac1c5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
10 changes: 9 additions & 1 deletion index.js
Expand Up @@ -5,6 +5,7 @@ var https = require("https");
var Writable = require("stream").Writable;
var assert = require("assert");
var debug = require("./debug");
const path = require("path");

// Create handlers that pass events from native requests
var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
Expand Down Expand Up @@ -277,7 +278,14 @@ RedirectableRequest.prototype._performRequest = function () {
// Create the native request
var request = this._currentRequest =
nativeProtocol.request(this._options, this._onNativeResponse);
this._currentUrl = url.format(this._options);
if (path.isAbsolute(this._options.path)) {
// retrieving resource on target host
this._currentUrl = url.format(this._options);
}
else {
// retrieving resource on other host - proxying
this._currentUrl = this._options.path;
}

// Set up event handlers
request._redirectable = this;
Expand Down
42 changes: 41 additions & 1 deletion test/test.js
Expand Up @@ -17,6 +17,7 @@ var delay = util.delay;
var redirectsTo = util.redirectsTo;
var sendsJson = util.sendsJson;
var asPromise = util.asPromise;
var proxy = util.proxy;

var testFile = path.resolve(__dirname, "assets/input.txt");
var testFileBuffer = fs.readFileSync(testFile);
Expand Down Expand Up @@ -297,7 +298,7 @@ describe("follow-redirects", function () {
});
});

it("emits an error whem url.resolve fails", function () {
it("emits an error when url.resolve fails", function () {
app.get("/a", redirectsTo("/b"));
var urlResolve = url.resolve;
url.resolve = function () {
Expand Down Expand Up @@ -2080,6 +2081,45 @@ describe("follow-redirects", function () {
});
});
});

describe("when request is going through an HTTP proxy (without a tunnel)", function () {
[
{ redirectType: "absolute", redirectUrl: "http://localhost:3600/b" },
{ redirectType: "relative", redirectUrl: "/b" },
].forEach(function (testCase) {
it("redirects to proper URL when Location header is " + testCase.redirectType, function () {
app.get("/a", redirectsTo(testCase.redirectUrl));
app.get("/b", sendsJson({ good: "yes" }));
app2.port = 3601;
app2.all("*", proxy("localhost:3601"));

function setProxy(opts) {
// assuming opts is a url.parse result
// Update path and Host header
opts.path = opts.href;
opts.pathname = opts.href;

// Update port and host to target proxy host
opts.port = 3601;
opts.host = opts.hostname + ":" + opts.port;

// redirected requests use proxy too
opts.beforeRedirect = setProxy;
}
return Promise.all([server.start(app), server.start(app2)])
.then(asPromise(function (resolve, reject) {
var opts = Object.assign({}, url.parse("http://localhost:3600/a"));
setProxy(opts);

http.get(opts, concatJson(resolve, reject)).on("error", reject);
}))
.then(function (res) {
assert.deepEqual(res.parsedJson, { good: "yes" });
assert.deepEqual(res.responseUrl, "http://localhost:3600/b");
});
});
});
});
});

function noop() { /* noop */ }
25 changes: 25 additions & 0 deletions test/util.js
@@ -1,4 +1,7 @@
var concat = require("concat-stream");
var http = require("http");
var https = require("https");
var url = require("url");

function redirectsTo() {
var args = Array.prototype.slice.call(arguments);
Expand Down Expand Up @@ -42,10 +45,32 @@ function asPromise(cb) {
};
}

function proxy(proxyHost) {
return function (req, res) {
var upstreamUrl = url.parse(req.originalUrl);
if (upstreamUrl.host === proxyHost) {
res.writeHead(400, "Bad request");
res.write(JSON.stringify({ bad: "detected proxy recursion" }));
res.end();
}
else {
var transport = /https:?/.test(upstreamUrl.protocol) ? https : http;
var upstreamReq = transport.request(req.originalUrl, {
headers: req.headers,
}, function (upstreamRes) {
res.writeHead(upstreamRes.statusCode, upstreamRes.statusMessage, upstreamRes.headers);
upstreamRes.pipe(res);
});
upstreamReq.end();
}
};
}

module.exports = {
asPromise: asPromise,
concatJson: concatJson,
delay: delay,
proxy: proxy,
redirectsTo: redirectsTo,
sendsJson: sendsJson,
};

0 comments on commit e7ac1c5

Please sign in to comment.