Skip to content

Commit

Permalink
http2: do no throw in writeHead if state.closed
Browse files Browse the repository at this point in the history
The http1 implementation does not throw if the connection is down.
The http2 compat implementation should do the same.

See: fastify/fastify-http-proxy#51.
See: fastify/fastify#1494.

PR-URL: #27682
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
mcollina authored and targos committed May 20, 2019
1 parent 05c3d53 commit a66b391
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
7 changes: 2 additions & 5 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,11 @@ class Http2ServerResponse extends Stream {
writeHead(statusCode, statusMessage, headers) {
const state = this[kState];

if (state.closed)
throw new ERR_HTTP2_INVALID_STREAM();
if (state.closed || this.stream.destroyed)
return this;
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();

if (this.stream.destroyed)
return this;

if (typeof statusMessage === 'string')
statusMessageWarn();

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-http2-compat-serverresponse-writehead.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ server.listen(0, common.mustCall(function() {
response.on('finish', common.mustCall(function() {
server.close();
process.nextTick(common.mustCall(() => {
common.expectsError(() => { response.writeHead(300); }, {
code: 'ERR_HTTP2_INVALID_STREAM'
});
// The stream is invalid at this point,
// and this line verifies this does not throw.
response.writeHead(300);
}));
}));
response.end();
Expand Down

0 comments on commit a66b391

Please sign in to comment.