Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
#515, #2894 Strip port suffix from Host header if the protocol is kno…
…wn. (#2904)

* Strip port suffix from Host header if the protocol is known.

This partially revert ff6d6c6, and still works for IPv6 addresses as well.

* Port is a string out of url.parse().

See https://nodejs.org/api/url.html#url_url_port

* Port is a string out of url.parse().

See https://nodejs.org/api/url.html#url_url_port

* Add tests for the new Host header changes.
  • Loading branch information
paambaati authored and mikeal committed Jul 18, 2018
1 parent 45ffc4b commit a92e138
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
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

0 comments on commit a92e138

Please sign in to comment.