-
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.
http2: add compat trailers, adjust multi-headers
Adds Http2ServerRequest trailers & rawTrailers functionality. Also fixes behaviour of multi-headers to conform with the spec (all values but set-cookie and cookie should be comma delimited, cookie should be semi-colon delimited and only set-cookie should be an array). Adds setter for statusMessage that warns, for backwards compatibility. End readable side of the stream on trailers or bodyless requests Refs: expressjs/express#3390 (comment) PR-URL: #15193 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
ddbcc9e
commit 764213c
Showing
10 changed files
with
298 additions
and
49 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Flags: --expose-http2 | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const h2 = require('http2'); | ||
|
||
// Http2ServerRequest should always end readable stream | ||
// even on GET requests with no body | ||
|
||
const server = h2.createServer(); | ||
server.listen(0, common.mustCall(function() { | ||
const port = server.address().port; | ||
server.once('request', common.mustCall(function(request, response) { | ||
request.on('data', () => {}); | ||
request.on('end', common.mustCall(() => { | ||
response.on('finish', common.mustCall(function() { | ||
server.close(); | ||
})); | ||
response.end(); | ||
})); | ||
})); | ||
|
||
const url = `http://localhost:${port}`; | ||
const client = h2.connect(url, common.mustCall(function() { | ||
const headers = { | ||
':path': '/foobar', | ||
':method': 'GET', | ||
':scheme': 'http', | ||
':authority': `localhost:${port}` | ||
}; | ||
const request = client.request(headers); | ||
request.resume(); | ||
request.on('end', common.mustCall(function() { | ||
client.destroy(); | ||
})); | ||
request.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,71 @@ | ||
// Flags: --expose-http2 | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const assert = require('assert'); | ||
const h2 = require('http2'); | ||
|
||
// Http2ServerRequest should have getter for trailers & rawTrailers | ||
|
||
const expectedTrailers = { | ||
'x-foo': 'xOxOxOx, OxOxOxO, xOxOxOx, OxOxOxO', | ||
'x-foo-test': 'test, test' | ||
}; | ||
|
||
const server = h2.createServer(); | ||
server.listen(0, common.mustCall(function() { | ||
const port = server.address().port; | ||
server.once('request', common.mustCall(function(request, response) { | ||
let data = ''; | ||
request.setEncoding('utf8'); | ||
request.on('data', common.mustCall((chunk) => data += chunk)); | ||
request.on('end', common.mustCall(() => { | ||
const trailers = request.trailers; | ||
for (const [name, value] of Object.entries(expectedTrailers)) { | ||
assert.strictEqual(trailers[name], value); | ||
} | ||
assert.deepStrictEqual([ | ||
'x-foo', | ||
'xOxOxOx', | ||
'x-foo', | ||
'OxOxOxO', | ||
'x-foo', | ||
'xOxOxOx', | ||
'x-foo', | ||
'OxOxOxO', | ||
'x-foo-test', | ||
'test, test' | ||
], request.rawTrailers); | ||
assert.strictEqual(data, 'test\ntest'); | ||
response.end(); | ||
})); | ||
})); | ||
|
||
const url = `http://localhost:${port}`; | ||
const client = h2.connect(url, common.mustCall(function() { | ||
const headers = { | ||
':path': '/foobar', | ||
':method': 'POST', | ||
':scheme': 'http', | ||
':authority': `localhost:${port}` | ||
}; | ||
const request = client.request(headers, { | ||
getTrailers(trailers) { | ||
trailers['x-fOo'] = 'xOxOxOx'; | ||
trailers['x-foO'] = 'OxOxOxO'; | ||
trailers['X-fOo'] = 'xOxOxOx'; | ||
trailers['X-foO'] = 'OxOxOxO'; | ||
trailers['x-foo-test'] = 'test, test'; | ||
} | ||
}); | ||
request.resume(); | ||
request.on('end', common.mustCall(function() { | ||
server.close(); | ||
client.destroy(); | ||
})); | ||
request.write('test\n'); | ||
request.end('test'); | ||
})); | ||
})); |
51 changes: 51 additions & 0 deletions
51
test/parallel/test-http2-compat-serverresponse-statusmessage-property-set.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,51 @@ | ||
// Flags: --expose-http2 | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
const assert = require('assert'); | ||
const h2 = require('http2'); | ||
|
||
// Http2ServerResponse.statusMessage should warn | ||
|
||
const unsupportedWarned = common.mustCall(1); | ||
process.on('warning', ({ name, message }) => { | ||
const expectedMessage = | ||
'Status message is not supported by HTTP/2 (RFC7540 8.1.2.4)'; | ||
if (name === 'UnsupportedWarning' && message === expectedMessage) | ||
unsupportedWarned(); | ||
}); | ||
|
||
const server = h2.createServer(); | ||
server.listen(0, common.mustCall(function() { | ||
const port = server.address().port; | ||
server.once('request', common.mustCall(function(request, response) { | ||
response.on('finish', common.mustCall(function() { | ||
response.statusMessage = 'test'; | ||
response.statusMessage = 'test'; // only warn once | ||
assert.strictEqual(response.statusMessage, ''); // no change | ||
server.close(); | ||
})); | ||
response.end(); | ||
})); | ||
|
||
const url = `http://localhost:${port}`; | ||
const client = h2.connect(url, common.mustCall(function() { | ||
const headers = { | ||
':path': '/', | ||
':method': 'GET', | ||
':scheme': 'http', | ||
':authority': `localhost:${port}` | ||
}; | ||
const request = client.request(headers); | ||
request.on('response', common.mustCall(function(headers) { | ||
assert.strictEqual(headers[':status'], 200); | ||
}, 1)); | ||
request.on('end', common.mustCall(function() { | ||
client.destroy(); | ||
})); | ||
request.end(); | ||
request.resume(); | ||
})); | ||
})); |
Oops, something went wrong.