http2: fix subsequent end calls to not throw

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/15385
PR-URL: https://github.com/nodejs/node/pull/15414
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Anatoli Papirovski 2017-09-14 14:02:54 -04:00 committed by Matteo Collina
parent bd85752871
commit a4e923f5c1
2 changed files with 7 additions and 5 deletions

View File

@ -458,14 +458,13 @@ class Http2ServerResponse extends Stream {
cb = encoding; cb = encoding;
encoding = 'utf8'; encoding = 'utf8';
} }
if (stream === undefined || stream.finished === true) {
return false;
}
if (chunk !== null && chunk !== undefined) { if (chunk !== null && chunk !== undefined) {
this.write(chunk, encoding); this.write(chunk, encoding);
} }
if (stream === undefined) {
return;
}
if (typeof cb === 'function') { if (typeof cb === 'function') {
stream.once('finish', cb); stream.once('finish', cb);
} }

View File

@ -4,7 +4,7 @@
const { mustCall, mustNotCall, hasCrypto, skip } = require('../common'); const { mustCall, mustNotCall, hasCrypto, skip } = require('../common');
if (!hasCrypto) if (!hasCrypto)
skip('missing crypto'); skip('missing crypto');
const { strictEqual } = require('assert'); const { doesNotThrow, strictEqual } = require('assert');
const { const {
createServer, createServer,
connect, connect,
@ -19,6 +19,9 @@ const {
// but may be invoked repeatedly without throwing errors. // but may be invoked repeatedly without throwing errors.
const server = createServer(mustCall((request, response) => { const server = createServer(mustCall((request, response) => {
strictEqual(response.closed, false); strictEqual(response.closed, false);
response.on('finish', mustCall(() => process.nextTick(
mustCall(() => doesNotThrow(() => response.end('test', mustNotCall())))
)));
response.end(mustCall(() => { response.end(mustCall(() => {
server.close(); server.close();
})); }));