http2: makes response.writeHead return the response

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

PR-URL: https://github.com/nodejs/node/pull/25974
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Mark S. Everitt 2019-02-06 22:24:50 +00:00 committed by Anna Henningsen
parent f93df51155
commit 1aa11e1444
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9
4 changed files with 18 additions and 5 deletions

View File

@ -3283,15 +3283,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

View File

@ -568,10 +568,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();
@ -596,6 +594,8 @@ class Http2ServerResponse extends Stream {
state.statusCode = statusCode;
this[kBeginSend]();
return this;
}
write(chunk, encoding, cb) {

View File

@ -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(); }));
}));

View File

@ -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'