Skip to content

Commit

Permalink
http2: fix subsequent end calls to not throw
Browse files Browse the repository at this point in the history
Calling Http2ServerResponse.end multiple times should never
cause the code to throw an error, subsequent calls should
instead return false. Fix behaviour to match http1.

Fixes: https://github.com/nodejs/node/issues/15385m
  • Loading branch information
apapirovski committed Sep 14, 2017
1 parent c1fce1e commit 8be7991
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,14 +458,13 @@ class Http2ServerResponse extends Stream {
cb = encoding;
encoding = 'utf8';
}
if (stream === undefined || stream.finished === true) {
return false;
}
if (chunk !== null && chunk !== undefined) {
this.write(chunk, encoding);
}

if (stream === undefined) {
return;
}

if (typeof cb === 'function') {
stream.once('finish', cb);
}
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const { mustCall, mustNotCall, hasCrypto, skip } = require('../common');
if (!hasCrypto)
skip('missing crypto');
const { strictEqual } = require('assert');
const { doesNotThrow, strictEqual } = require('assert');
const {
createServer,
connect,
Expand All @@ -19,6 +19,9 @@ const {
// but may be invoked repeatedly without throwing errors.
const server = createServer(mustCall((request, response) => {
strictEqual(response.closed, false);
response.on('finish', mustCall(() => process.nextTick(
mustCall(() => doesNotThrow(() => response.end('test', mustNotCall())))
)));
response.end(mustCall(() => {
server.close();
}));
Expand Down

0 comments on commit 8be7991

Please sign in to comment.