Skip to content

Commit

Permalink
http2: get trailers working with the compat api
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jasnell authored and addaleax committed Aug 14, 2017
1 parent 3e5b07a commit 9a4be4a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,12 @@ class Http2ServerResponse extends Stream {
addTrailers(headers) {
let trailers = this[kTrailers];
const keys = Object.keys(headers);
let key = '';
if (keys.length > 0)
if (keys.length === 0)
return;
if (trailers === undefined)
trailers = this[kTrailers] = Object.create(null);
for (var i = 0; i < keys.length; i++) {
key = String(keys[i]).trim().toLowerCase();
const value = headers[key];
assertValidHeader(key, value);
trailers[key] = String(value);
trailers[keys[i]] = String(headers[keys[i]]);
}
}

Expand Down Expand Up @@ -508,12 +504,16 @@ class Http2ServerResponse extends Stream {

[kBeginSend](options) {
const stream = this[kStream];
options = options || Object.create(null);
if (stream !== undefined && stream.headersSent === false) {
const state = this[kState];
const headers = this[kHeaders] || Object.create(null);
headers[constants.HTTP2_HEADER_STATUS] = state.statusCode;
if (stream.finished === true)
options.endStream = true;
options.getTrailers = (trailers) => {
Object.assign(trailers, this[kTrailers]);
};
if (stream.destroyed === false) {
stream.respond(headers, options);
}
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-http2-compat-serverresponse-trailers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Flags: --expose-http2
'use strict';

const common = require('../common');
const assert = require('assert');
const http2 = require('http2');

const server = http2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
server.once('request', common.mustCall((request, response) => {
response.addTrailers({
ABC: 123
});
response.end('hello');
}));

const url = `http://localhost:${port}`;
const client = http2.connect(url, common.mustCall(() => {
const request = client.request();
request.on('trailers', common.mustCall((headers) => {
assert.strictEqual(headers.abc, '123');
}));
request.resume();
request.on('end', common.mustCall(() => {
client.destroy();
server.close();
}));
}));
}));

0 comments on commit 9a4be4a

Please sign in to comment.