Skip to content

Commit

Permalink
Merge c79957a into 167065e
Browse files Browse the repository at this point in the history
  • Loading branch information
lxs137 committed Dec 1, 2018
2 parents 167065e + c79957a commit 993fb37
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -285,7 +285,7 @@ Request.prototype.init = function (options) {
self._redirect.onRequest(options)

self.setHost = false
if (!self.hasHeader('host')) {
if (!self.hasHeader('host') && !self.uri.isUnix) {
var hostHeaderName = self.originalHostHeaderName || 'host'
self.setHeader(hostHeaderName, self.uri.host)
// Drop :port suffix from Host header if known protocol.
Expand Down
13 changes: 13 additions & 0 deletions tests/server.js
Expand Up @@ -71,6 +71,19 @@ exports.createSSLServer = function (opts) {
return s
}

exports.createUnixSockServer = function (unixSockPath) {
var s = http.createServer(function (req, resp) {
s.emit(req.url, req, resp)
})
if (fs.existsSync(unixSockPath)) {
s.on('close', function () {
fs.unlinkSync(unixSockPath)
})
}
s.listen(unixSockPath)
return s
}

exports.createPostStream = function (text) {
var postStream = new stream.Stream()
postStream.writeable = true
Expand Down
25 changes: 25 additions & 0 deletions tests/test-headers.js
Expand Up @@ -263,6 +263,31 @@ tape('catch invalid characters error - POST', function (t) {
})
})

tape('Host header is not required when use UNIX Domain Sockets', function (t) {
if (os.platform() === 'win32') {
t.end()
}
var sockPath = '/tmp/request.sock'
var sockServer = server.createUnixSockServer(sockPath)
sockServer.on('/', function (req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
})
res.end(JSON.stringify(req.headers))
})

request({
method: 'GET',
url: 'http://unix:' + sockPath + ':/',
json: true
}, function (err, res, body) {
t.equal(err, null)
t.equal(typeof body['host'], 'undefined')
sockServer.close()
t.end()
})
})

if (hasIPv6interface) {
tape('IPv6 Host header', function (t) {
// Horrible hack to observe the raw data coming to the server
Expand Down

0 comments on commit 993fb37

Please sign in to comment.