-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add setDefaultHeaders option to http.request
This makes it possible to disable the various default headers directly from the constructor. While this is possible for many use cases by manually calling removeHeader on the request object instead, when passing a raw header array to the request constructor the headers are serialized and prepared to send immediately, and removeHeader cannot subsequently be used. With this change, it's now possible to 100% control sent request headers by passing 'setDefaultHeaders: false' and a raw headers array to http.request. PR-URL: #56112 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
test/parallel/test-http-dont-set-default-headers-with-set-header.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(common.mustCall(function(req, res) { | ||
assert.deepStrictEqual(req.rawHeaders, [ | ||
'test', 'value', | ||
'HOST', `127.0.0.1:${server.address().port}`, | ||
'foo', 'bar', | ||
'foo', 'baz', | ||
'connection', 'close', | ||
]); | ||
|
||
res.end('ok'); | ||
server.close(); | ||
})); | ||
server.listen(0, common.localhostIPv4, function() { | ||
const req = http.request({ | ||
method: 'POST', | ||
host: common.localhostIPv4, | ||
port: this.address().port, | ||
setDefaultHeaders: false, | ||
}); | ||
|
||
req.setHeader('test', 'value'); | ||
req.setHeader('HOST', `${common.localhostIPv4}:${server.address().port}`); | ||
req.setHeader('foo', ['bar', 'baz']); | ||
req.setHeader('connection', 'close'); | ||
|
||
req.end(); | ||
}); |
23 changes: 23 additions & 0 deletions
23
test/parallel/test-http-dont-set-default-headers-with-setHost.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(common.mustCall(function(req, res) { | ||
assert.deepStrictEqual(req.rawHeaders, [ | ||
'Host', `${common.localhostIPv4}:${server.address().port}`, | ||
]); | ||
|
||
res.end('ok'); | ||
server.close(); | ||
})); | ||
server.listen(0, common.localhostIPv4, function() { | ||
http.request({ | ||
method: 'POST', | ||
host: common.localhostIPv4, | ||
port: this.address().port, | ||
setDefaultHeaders: false, | ||
setHost: true | ||
}).end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
|
||
const server = http.createServer(common.mustCall(function(req, res) { | ||
assert.deepStrictEqual(req.rawHeaders, [ | ||
'host', `${common.localhostIPv4}:${server.address().port}`, | ||
'foo', 'bar', | ||
'test', 'value', | ||
'foo', 'baz', | ||
]); | ||
|
||
res.end('ok'); | ||
server.close(); | ||
})); | ||
server.listen(0, common.localhostIPv4, function() { | ||
http.request({ | ||
method: 'POST', | ||
host: common.localhostIPv4, | ||
port: this.address().port, | ||
setDefaultHeaders: false, | ||
headers: [ | ||
'host', `${common.localhostIPv4}:${server.address().port}`, | ||
'foo', 'bar', | ||
'test', 'value', | ||
'foo', 'baz', | ||
] | ||
}).end(); | ||
}); |