Repository navigation
Closed
Description
Describe the bug
- Node.js version: v14
- OS & version: macOS (10)
- Got version: 11.8.2
Actual behavior
The pipe request forwards all the client headers including the "Host"
Expected behavior
The pipe request should forwards all the client headers but not the "Host"
Code to reproduce
npm 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
- I have read the documentation.
- I have tried my code with the latest version of Node.js and Got.
Metadata
Metadata
Assignees
Labels
No labels
Activity
szmarczak commentedon Jun 8, 2021
Simply
delete req.headers.host;before piping to got.szmarczak commentedon Jun 8, 2021
got/source/core/index.ts
Lines 1415 to 1422 in f896aa5
szmarczak commentedon Jun 8, 2021
Or you can use a
beforeRequesthook: