Skip to content
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

#515, #2894 Strip port suffix from Host header if the protocol is known. #2904

Merged
merged 5 commits into from Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions request.js
Expand Up @@ -287,10 +287,14 @@ Request.prototype.init = function (options) {
self.setHost = false
if (!self.hasHeader('host')) {
var hostHeaderName = self.originalHostHeaderName || 'host'
// When used with an IPv6 address, `host` will provide
// the correct bracketed format, unlike using `hostname` and
// optionally adding the `port` when necessary.
self.setHeader(hostHeaderName, self.uri.host)
// Drop :port suffix from Host header if known protocol.
if (self.uri.port) {
if ((self.uri.port === '80' && self.uri.protocol === 'http:') ||
(self.uri.port === '443' && self.uri.protocol === 'https:')) {
self.setHeader(hostHeaderName, self.uri.hostname)
}
}
self.setHost = true
}

Expand Down
50 changes: 50 additions & 0 deletions tests/test-headers.js
Expand Up @@ -180,6 +180,56 @@ tape('undefined headers', function (t) {
})
})

tape('preserve port in host header if non-standard port', function (t) {
var r = request({
url: s.url + '/headers.json'
}, function (err, res, body) {
t.equal(err, null)
t.equal(r.originalHost, 'localhost:' + s.port)
t.end()
})
})

tape('strip port in host header if explicit standard port (:80) & protocol (HTTP)', function (t) {
var r = request({
url: 'http://localhost:80/headers.json'
}, function (err, res, body) {
t.notEqual(err, null)
t.equal(r.req.socket._host, 'localhost')
t.end()
})
})

tape('strip port in host header if explicit standard port (:443) & protocol (HTTPS)', function (t) {
var r = request({
url: 'https://localhost:443/headers.json'
}, function (err, res, body) {
t.notEqual(err, null)
t.equal(r.req.socket._host, 'localhost')
t.end()
})
})

tape('strip port in host header if implicit standard port & protocol (HTTP)', function (t) {
var r = request({
url: 'http://localhost/headers.json'
}, function (err, res, body) {
t.notEqual(err, null)
t.equal(r.req.socket._host, 'localhost')
t.end()
})
})

tape('strip port in host header if implicit standard port & protocol (HTTPS)', function (t) {
var r = request({
url: 'https://localhost/headers.json'
}, function (err, res, body) {
t.notEqual(err, null)
t.equal(r.req.socket._host, 'localhost')
t.end()
})
})

var isExpectedHeaderCharacterError = function (headerName, err) {
return err.message === 'The header content contains invalid characters' ||
err.message === ('Invalid character in header content ["' + headerName + '"]')
Expand Down