Skip to content

Commit

Permalink
http2: makes response.writeHead return the response
Browse files Browse the repository at this point in the history
Fixes: #25935

PR-URL: #25974
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
qubyte authored and BethGriggs committed Oct 7, 2019
1 parent ccf2823 commit 9258496
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -3284,15 +3284,23 @@ should be sent. See the [`'checkContinue'`][] event on `Http2Server` and
#### response.writeHead(statusCode[, statusMessage][, headers])
<!-- YAML
added: v8.4.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/25974
description: Return `this` from `writeHead()` to allow chaining with
`end()`.
-->

* `statusCode` {number}
* `statusMessage` {string}
* `headers` {Object}
* Returns: {http2.Http2ServerResponse}

Sends a response header to the request. The status code is a 3-digit HTTP
status code, like `404`. The last argument, `headers`, are the response headers.

Returns a reference to the `Http2ServerResponse`, so that calls can be chained.

For compatibility with [HTTP/1][], a human-readable `statusMessage` may be
passed as the second argument. However, because the `statusMessage` has no
meaning within HTTP/2, the argument will have no effect and a process warning
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,8 @@ 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;
return this;

if (typeof statusMessage === 'string')
statusMessageWarn();
Expand All @@ -621,6 +619,8 @@ class Http2ServerResponse extends Stream {

state.statusCode = statusCode;
this[kBeginSend]();

return this;
}

write(chunk, encoding, cb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const server = h2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
server.once('request', common.mustCall((request, response) => {
response.writeHead(200, [
const returnVal = response.writeHead(200, [
['foo', 'bar'],
['ABC', 123]
]);
assert.strictEqual(returnVal, response);
response.end(common.mustCall(() => { server.close(); }));
}));

Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-http2-compat-serverresponse-writehead.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ server.listen(0, common.mustCall(function() {
const port = server.address().port;
server.once('request', common.mustCall(function(request, response) {
response.setHeader('foo-bar', 'def456');
response.writeHead(418, { 'foo-bar': 'abc123' }); // Override

// Override
const returnVal = response.writeHead(418, { 'foo-bar': 'abc123' });

assert.strictEqual(returnVal, response);

common.expectsError(() => { response.writeHead(300); }, {
code: 'ERR_HTTP2_HEADERS_SENT'
Expand Down

0 comments on commit 9258496

Please sign in to comment.