Skip to content

Commit

Permalink
Default to localhost if no host given.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Sep 13, 2022
1 parent 449e895 commit 6e2b86d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ function wrap(protocols) {
maxBodyLength: exports.maxBodyLength,
}, input, options);
options.nativeProtocols = nativeProtocols;
if (!isString(options.host) && !isString(options.hostname)) {
options.hostname = "::1";
}

assert.equal(options.protocol, protocol, "protocol mismatch");
debug("options", options);
Expand Down Expand Up @@ -596,7 +599,8 @@ function abortRequest(request) {
}

function isSubdomain(subdomain, domain) {
const dot = subdomain.length - domain.length - 1;
assert(isString(subdomain) && isString(domain));
var dot = subdomain.length - domain.length - 1;
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
}

Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,29 @@ describe("follow-redirects", function () {
assert.equal(body.host, "localhost:3600");
});
});

it("defaults to localhost for relative redirect if no host given", function () {
app.get("/a", redirectsTo(302, "/b"));
app.get("/b", function (req, res) {
res.write(JSON.stringify(req.headers));
req.pipe(res); // will invalidate JSON if non-empty
});

return server.start(app)
.then(asPromise(function (resolve, reject) {
var opts = { port: 3600, path: "/a" };
http.get(opts, resolve).on("error", reject);
}))
.then(asPromise(function (resolve, reject, res) {
assert.deepEqual(res.statusCode, 200);
assert(res.responseUrl.endsWith("/b"));
res.pipe(concat({ encoding: "string" }, resolve)).on("error", reject);
}))
.then(function (str) {
var body = JSON.parse(str);
assert(body.host.endsWith(":3600"));
});
});
});

[
Expand Down

0 comments on commit 6e2b86d

Please sign in to comment.