From 3e5b07a8fb6103dd6f6b33d9945d82a84d01e0a6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 31 Jul 2017 14:10:24 -0700 Subject: [PATCH] http2: refactor trailers API Rather than using the `'fetchTrailers'` event to collect trailers, a new `getTrailers` callback option is supported. If not set, the internals will skip calling out for trailers at all. Expands the test to make sure trailers work from the client side also. Backport-PR-URL: https://github.com/nodejs/node/pull/14813 Backport-Reviewed-By: Anna Henningsen Backport-Reviewed-By: Timothy Gu PR-URL: https://github.com/nodejs/node/pull/14239 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Matteo Collina --- doc/api/http2.md | 125 ++++++++++++++---- lib/internal/http2/core.js | 97 +++++++++++--- src/node_http2.cc | 10 +- src/node_http2_core-inl.h | 24 +++- src/node_http2_core.cc | 50 ++++--- src/node_http2_core.h | 27 +++- .../test-http2-misused-pseudoheaders.js | 10 +- test/parallel/test-http2-trailers.js | 19 ++- 8 files changed, 272 insertions(+), 90 deletions(-) diff --git a/doc/api/http2.md b/doc/api/http2.md index ae08fe49a7bb50..da6df4f7e6acac 100755 --- a/doc/api/http2.md +++ b/doc/api/http2.md @@ -346,6 +346,9 @@ added: REPLACEME * `weight` {number} Specifies the relative dependency of a stream in relation to other streams with the same `parent`. The value is a number between `1` and `256` (inclusive). + * `getTrailers` {Function} Callback function invoked to collect trailer + headers. + * Returns: {ClientHttp2Stream} For HTTP/2 Client `Http2Session` instances only, the `http2session.request()` @@ -371,6 +374,16 @@ req.on('response', (headers) => { }); ``` +When set, the `options.getTrailers()` function is called immediately after +queuing the last chunk of payload data to be sent. The callback is passed a +single object (with a `null` prototype) that the listener may used to specify +the trailing header fields to send to the peer. + +*Note*: The HTTP/1 specification forbids trailers from containing HTTP/2 +"pseudo-header" fields (e.g. `':method'`, `':path'`, etc). An `'error'` event +will be emitted if the `getTrailers` callback attempts to set such header +fields. + #### http2session.rstStream(stream, code) - -The `'fetchTrailers'` event is emitted by the `Http2Stream` immediately after -queuing the last chunk of payload data to be sent. The listener callback is -passed a single object (with a `null` prototype) that the listener may used -to specify the trailing header fields to send to the peer. - -```js -stream.on('fetchTrailers', (trailers) => { - trailers['ABC'] = 'some value to send'; -}); -``` - -*Note*: The HTTP/1 specification forbids trailers from containing HTTP/2 -"pseudo-header" fields (e.g. `':status'`, `':path'`, etc). An `'error'` event -will be emitted if the `'fetchTrailers'` event handler attempts to set such -header fields. - #### Event: 'frameError'