http2: make compat writeHead not crash if the stream is destroyed

Fixes: https://github.com/nodejs/node/issues/24470

PR-URL: https://github.com/nodejs/node/pull/24723
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Matteo Collina 2018-11-29 14:33:20 +01:00 committed by Rich Trott
parent 175164e5d1
commit 32fed93aee
2 changed files with 34 additions and 0 deletions

View File

@ -568,6 +568,11 @@ class Http2ServerResponse extends Stream {
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();
// If the stream is destroyed, we return false,
// like require('http').
if (this.stream.destroyed)
return false;
if (typeof statusMessage === 'string')
statusMessageWarn();

View File

@ -0,0 +1,29 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const http2 = require('http2');
// Check that writeHead, write and end do not crash in compatibility mode
const server = http2.createServer(common.mustCall((req, res) => {
// destroy the stream first
req.stream.destroy();
res.writeHead(200);
res.write('hello ');
res.end('world');
}));
server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.on('response', common.mustNotCall());
req.on('close', common.mustCall((arg) => {
client.close();
server.close();
}));
req.resume();
}));