Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: compat head request should not start finished #24339

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,10 @@ class Http2ServerResponse extends Stream {

get finished() {
const stream = this[kStream];
const state = this[kState];
return stream.destroyed ||
stream._writableState.ended ||
this[kState].closed;
state.closed ||
(stream.headRequest ? state.ending : stream._writableState.ended);
}

get socket() {
Expand Down Expand Up @@ -630,7 +631,7 @@ class Http2ServerResponse extends Stream {
if (chunk !== null && chunk !== undefined)
this.write(chunk, encoding);

const isFinished = this.finished;
const isFinished = stream._writableState.ended;
state.headRequest = stream.headRequest;
state.ending = true;

Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-http2-compat-head-finished.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const { strictEqual } = require('assert');
const h2 = require('http2');

// Http2ServerResponse.finished cannot be true before res.end() is
// called or the request is aborted.
const server = h2
.createServer()
.listen(0, common.mustCall(function() {
const port = server.address().port;
server.once('request', common.mustCall((req, res) => {
strictEqual(res.finished, false);
res.writeHead(400);
strictEqual(res.finished, false);
strictEqual(res.headersSent, true);
res.end();
strictEqual(res.finished, true);
}));

const url = `http://localhost:${port}`;
const client = h2.connect(url, common.mustCall(function() {
const headers = {
':path': '/',
':method': 'HEAD',
':scheme': 'http',
':authority': `localhost:${port}`
};
const request = client.request(headers);
request.on('end', common.mustCall(function() {
client.close();
server.close();
}));
request.end();
request.resume();
}));
}));
2 changes: 1 addition & 1 deletion test/parallel/test-http2-compat-serverresponse-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const {
// Http2ServerResponse.end is necessary on HEAD requests in compat
// for http1 compatibility
const server = createServer(mustCall((request, response) => {
strictEqual(response.finished, true);
strictEqual(response.finished, false);
response.writeHead(HTTP_STATUS_OK, { foo: 'bar' });
response.end('data', mustCall());
}));
Expand Down