Skip to content

Commit 9a4be4a

Browse files
jasnelladdaleax
authored andcommitted
http2: get trailers working with the compat api
Backport-PR-URL: #14813 Backport-Reviewed-By: Anna Henningsen <[email protected]> Backport-Reviewed-By: Timothy Gu <[email protected]> PR-URL: #14239 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 3e5b07a commit 9a4be4a

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

lib/internal/http2/compat.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -328,16 +328,12 @@ class Http2ServerResponse extends Stream {
328328
addTrailers(headers) {
329329
let trailers = this[kTrailers];
330330
const keys = Object.keys(headers);
331-
let key = '';
332-
if (keys.length > 0)
331+
if (keys.length === 0)
333332
return;
334333
if (trailers === undefined)
335334
trailers = this[kTrailers] = Object.create(null);
336335
for (var i = 0; i < keys.length; i++) {
337-
key = String(keys[i]).trim().toLowerCase();
338-
const value = headers[key];
339-
assertValidHeader(key, value);
340-
trailers[key] = String(value);
336+
trailers[keys[i]] = String(headers[keys[i]]);
341337
}
342338
}
343339

@@ -508,12 +504,16 @@ class Http2ServerResponse extends Stream {
508504

509505
[kBeginSend](options) {
510506
const stream = this[kStream];
507+
options = options || Object.create(null);
511508
if (stream !== undefined && stream.headersSent === false) {
512509
const state = this[kState];
513510
const headers = this[kHeaders] || Object.create(null);
514511
headers[constants.HTTP2_HEADER_STATUS] = state.statusCode;
515512
if (stream.finished === true)
516513
options.endStream = true;
514+
options.getTrailers = (trailers) => {
515+
Object.assign(trailers, this[kTrailers]);
516+
};
517517
if (stream.destroyed === false) {
518518
stream.respond(headers, options);
519519
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Flags: --expose-http2
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const http2 = require('http2');
7+
8+
const server = http2.createServer();
9+
server.listen(0, common.mustCall(() => {
10+
const port = server.address().port;
11+
server.once('request', common.mustCall((request, response) => {
12+
response.addTrailers({
13+
ABC: 123
14+
});
15+
response.end('hello');
16+
}));
17+
18+
const url = `http://localhost:${port}`;
19+
const client = http2.connect(url, common.mustCall(() => {
20+
const request = client.request();
21+
request.on('trailers', common.mustCall((headers) => {
22+
assert.strictEqual(headers.abc, '123');
23+
}));
24+
request.resume();
25+
request.on('end', common.mustCall(() => {
26+
client.destroy();
27+
server.close();
28+
}));
29+
}));
30+
}));

0 commit comments

Comments
 (0)