The pipe forwards the client host. #1748
-
Describe the bug
Actual behaviorThe pipe request forwards all the client headers including the "Host" Expected behaviorThe pipe request should forwards all the client headers but not the "Host" Code to reproducenpm install got nock express request const got = require('got')
const nock = require('nock')
const express = require('express')
const request = require('request')
nock('http://www.google.com')
.persist()
.get('/echo')
.reply(function (uri, body) {
return [200, { headers: this.req.headers }]
})
const proxy = express()
proxy.get('/got/pipe', (req, res) => {
req.pipe(got.stream('http://www.google.com/echo')).pipe(res)
})
proxy.get('/request/pipe', (req, res) => {
req.pipe(request.get('http://www.google.com/echo')).pipe(res)
})
proxy.listen(3000, () => {
console.log('Try the following:')
console.log("curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/got/pipe'")
console.log("curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/request/pipe'")
})Output // With Got
curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/got/pipe'
{
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"hi": "Bye",
"host": "Value", **// Should not have been this.**
"user-agent": "got (https://github.com/sindresorhus/got)"
}
}
// With Request
curl -H 'Hi: Bye' -H 'Host: Value' 'http://127.0.0.1:3000/request/pipe'
{
"headers": {
"accept": "*/*",
"hi": "Bye",
"host": "www.google.com",
"user-agent": "curl/7.64.1"
}
}Since the host doesn't match the hostname in the URL, it leads to a certificate error when hitting the actual service than the mocked one. Checklist
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
Simply Lines 1415 to 1422 in f896aa5 Or you can use a {
hooks: {
beforeRequest: [
options => {
delete options.headers.host;
}
]
}
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Hi @szmarczak , Is it not supposed to be handled in the library itself as |
Beta Was this translation helpful? Give feedback.
Simply
delete req.headers.host;before piping to got.got/source/core/index.ts
Lines 1415 to 1422 in f896aa5
Or you can use a
beforeRequesthook: