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: #15385
PR-URL: #15414
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
apapirovski authored and jasnell committed Sep 20, 2017
1 parent ba96c8f commit b61220e
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 @@ -466,14 +466,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 b61220e

Please sign in to comment.