mirror of
https://github.com/nodejs/node.git
synced 2026-11-26 21:58:26 -05:00
http2: do not end compat requests reset before END_STREAM
When the peer reset a stream before sending END_STREAM, the compat `Http2ServerRequest` still pushed EOF from its 'close' handler, so it emitted 'end' for a truncated body (on main even after 'error' and 'close'). Unlike http1, where an aborted `IncomingMessage` is destroyed and never emits 'end', applications listening for 'end' then processed the partial body as if it were complete. Record in `onStreamClose()` that the peer closed the stream before END_STREAM, since `destroy()` ends the readable side afterwards, and destroy the request instead of ending it in that case. Requests whose body was complete before the reset still end normally. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/67061 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
1 parent
c8984c6b26
commit
29c4d3b9be
4 files changed
+185
-5
No files matched your search
@@ -57,6 +57,7 @@ const {
|
||||
kSocket,
|
||||
kRequest,
|
||||
kProxySocket,
|
||||
kIncomplete,
|
||||
assertValidPseudoHeader,
|
||||
getAuthority,
|
||||
} = require('internal/http2/util');
|
||||
@@ -154,9 +155,12 @@ function onStreamTrailers(trailers, flags, rawTrailers) {
|
||||
}
|
||||
|
||||
function onStreamEnd() {
|
||||
// Cause the request stream to end as well.
|
||||
// Cause the request stream to end as well. Skip when the body is
|
||||
// incomplete (the peer never sent END_STREAM and the stream was torn down,
|
||||
// e.g. reset, local close, or session teardown); destroying the readable on
|
||||
// close emits 'end' spuriously, so it must not reach the compat request.
|
||||
const request = this[kRequest];
|
||||
if (request !== undefined)
|
||||
if (request !== undefined && !this[kIncomplete])
|
||||
this[kRequest].push(null);
|
||||
}
|
||||
|
||||
@@ -291,15 +295,24 @@ function onStreamCloseRequest() {
|
||||
const state = req[kState];
|
||||
state.closed = true;
|
||||
|
||||
this[kProxySocket] = null;
|
||||
this[kRequest] = undefined;
|
||||
|
||||
// The peer closed the stream before END_STREAM, so the request body is
|
||||
// incomplete. Like http1's IncomingMessage on abort, destroy the request
|
||||
// instead of ending it, so 'end' is not emitted for a truncated body.
|
||||
// destroy() emits 'close'.
|
||||
if (this[kIncomplete]) {
|
||||
req.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
req.push(null);
|
||||
// If the user didn't interact with incoming data and didn't pipe it,
|
||||
// dump it for compatibility with http1
|
||||
if (!state.didRead && !req._readableState.resumeScheduled)
|
||||
req.resume();
|
||||
|
||||
this[kProxySocket] = null;
|
||||
this[kRequest] = undefined;
|
||||
|
||||
req.emit('close');
|
||||
}
|
||||
|
||||
|
||||
@@ -156,6 +156,7 @@ const {
|
||||
kStrictSingleValueFields,
|
||||
kSocket,
|
||||
kRequest,
|
||||
kIncomplete,
|
||||
kProtocol,
|
||||
kProxySocket,
|
||||
MAX_ADDITIONAL_SETTINGS,
|
||||
@@ -655,6 +656,10 @@ function onStreamClose(code, peerReset) {
|
||||
const cleanCode = code === NGHTTP2_NO_ERROR || code === NGHTTP2_CANCEL;
|
||||
|
||||
if (!peerReadableEnded) {
|
||||
// The peer closed the stream before END_STREAM, so the incoming message
|
||||
// is incomplete. Bear in mind we must not 'end' the compat request. The
|
||||
// decision is recorded in _destroy() via this._readableState.ended, which
|
||||
// is the same signal as peerReadableEnded here.
|
||||
stream.destroy(cleanCode ?
|
||||
new ERR_HTTP2_STREAM_ABORTED() :
|
||||
new ERR_HTTP2_STREAM_ERROR(nameForErrorCode[code] || code));
|
||||
@@ -2698,6 +2703,14 @@ class Http2Stream extends Duplex {
|
||||
if (hasHandle)
|
||||
handle.rstStream(code);
|
||||
}
|
||||
|
||||
// If the peer never sent END_STREAM, the readable side was torn down
|
||||
// before reaching EOF, so the incoming message is incomplete. _destroy()
|
||||
// always ends the readable below, so record it here for the compat layer
|
||||
// (onStreamCloseRequest). Doing it in one place covers remote reset,
|
||||
// local close, and unexpected session teardown alike.
|
||||
if (!this._readableState.ended)
|
||||
this[kIncomplete] = true;
|
||||
this.push(null);
|
||||
|
||||
|
||||
|
||||
@@ -46,6 +46,7 @@ const kSocket = Symbol('socket');
|
||||
const kProtocol = Symbol('protocol');
|
||||
const kProxySocket = Symbol('proxySocket');
|
||||
const kRequest = Symbol('request');
|
||||
const kIncomplete = Symbol('incomplete');
|
||||
|
||||
const {
|
||||
NGHTTP2_NV_FLAG_NONE,
|
||||
@@ -1003,6 +1004,7 @@ module.exports = {
|
||||
kStrictSingleValueFields,
|
||||
kSocket,
|
||||
kProtocol,
|
||||
kIncomplete,
|
||||
kProxySocket,
|
||||
kRequest,
|
||||
MAX_ADDITIONAL_SETTINGS,
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
|
||||
// A request whose stream is reset by the peer before END_STREAM has an
|
||||
// incomplete body. Like http1, the compat request must not emit 'end' for it.
|
||||
|
||||
const assert = require('assert');
|
||||
const http2 = require('http2');
|
||||
const { finished } = require('stream');
|
||||
|
||||
const codes = [
|
||||
http2.constants.NGHTTP2_NO_ERROR,
|
||||
http2.constants.NGHTTP2_CANCEL,
|
||||
];
|
||||
|
||||
const server = http2.createServer(common.mustCall((req, res) => {
|
||||
// Tell the client the partial body arrived, so it can reset the stream.
|
||||
req.once('data', common.mustCall(() => res.writeHead(200).flushHeaders()));
|
||||
req.on('end', common.mustNotCall());
|
||||
req.on('aborted', common.mustCall());
|
||||
req.on('error', common.expectsError({ code: 'ERR_HTTP2_STREAM_ABORTED' }));
|
||||
req.on('close', common.mustCall(() => {
|
||||
assert.strictEqual(req.aborted, true);
|
||||
assert.strictEqual(req.readableEnded, false);
|
||||
}));
|
||||
finished(req, common.mustCall((err) => {
|
||||
assert.ok(err);
|
||||
}));
|
||||
}, codes.length));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const client = http2.connect(`http://localhost:${server.address().port}`);
|
||||
let pending = codes.length;
|
||||
|
||||
for (const code of codes) {
|
||||
const req = client.request({
|
||||
':method': 'POST',
|
||||
'content-length': '1000',
|
||||
});
|
||||
req.on('error', () => {});
|
||||
req.on('close', common.mustCall(() => {
|
||||
if (--pending === 0) {
|
||||
client.close();
|
||||
server.close();
|
||||
}
|
||||
}));
|
||||
// Send part of the body, then reset the stream without END_STREAM once
|
||||
// the server has seen it.
|
||||
req.write('{"action":"partial"}');
|
||||
req.on('response', common.mustCall(() => req.close(code)));
|
||||
}
|
||||
}));
|
||||
|
||||
// A complete body followed by a reset still ends the request normally.
|
||||
{
|
||||
const server = http2.createServer(common.mustCall((req, res) => {
|
||||
let body = '';
|
||||
req.setEncoding('utf8');
|
||||
req.on('data', (chunk) => body += chunk);
|
||||
req.on('end', common.mustCall(() => {
|
||||
assert.strictEqual(body, 'complete');
|
||||
res.writeHead(200).flushHeaders();
|
||||
}));
|
||||
req.on('error', common.mustNotCall());
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const client = http2.connect(`http://localhost:${server.address().port}`);
|
||||
const req = client.request({ ':method': 'POST' });
|
||||
req.on('error', () => {});
|
||||
req.on('response', common.mustCall(() => {
|
||||
req.close(http2.constants.NGHTTP2_CANCEL);
|
||||
}));
|
||||
req.on('close', common.mustCall(() => {
|
||||
client.close();
|
||||
server.close();
|
||||
}));
|
||||
req.end('complete');
|
||||
}));
|
||||
}
|
||||
|
||||
// A stream closed locally by the server before the peer sends END_STREAM also
|
||||
// has an incomplete body and must not emit 'end'.
|
||||
{
|
||||
const server = http2.createServer(common.mustCall((req, res) => {
|
||||
req.once('data', common.mustCall(() => {
|
||||
// Tear the stream down locally while the body is still in flight.
|
||||
req.stream.destroy();
|
||||
}));
|
||||
req.on('end', common.mustNotCall());
|
||||
req.on('aborted', common.mustCall());
|
||||
req.on('error', common.mustNotCall());
|
||||
req.on('close', common.mustCall(() => {
|
||||
assert.strictEqual(req.aborted, true);
|
||||
assert.strictEqual(req.readableEnded, false);
|
||||
}));
|
||||
finished(req, common.mustCall((err) => {
|
||||
assert.ok(err);
|
||||
}));
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const client = http2.connect(`http://localhost:${server.address().port}`);
|
||||
const req = client.request({
|
||||
':method': 'POST',
|
||||
'content-length': '1000',
|
||||
});
|
||||
req.on('error', () => {});
|
||||
req.on('close', common.mustCall(() => {
|
||||
client.close();
|
||||
server.close();
|
||||
}));
|
||||
req.write('{"action":"partial"}');
|
||||
}));
|
||||
}
|
||||
|
||||
// An unexpected session teardown mid-body also produces an incomplete request
|
||||
// that must not emit 'end'.
|
||||
{
|
||||
const server = http2.createServer(common.mustCall((req, res) => {
|
||||
req.once('data', common.mustCall(() => {
|
||||
req.stream.session.destroy();
|
||||
}));
|
||||
req.on('end', common.mustNotCall());
|
||||
req.on('aborted', common.mustCall());
|
||||
req.on('error', common.mustNotCall());
|
||||
req.on('close', common.mustCall(() => {
|
||||
assert.strictEqual(req.aborted, true);
|
||||
assert.strictEqual(req.readableEnded, false);
|
||||
}));
|
||||
finished(req, common.mustCall((err) => {
|
||||
assert.ok(err);
|
||||
}));
|
||||
}));
|
||||
|
||||
server.listen(0, common.mustCall(() => {
|
||||
const client = http2.connect(`http://localhost:${server.address().port}`);
|
||||
const req = client.request({
|
||||
':method': 'POST',
|
||||
'content-length': '1000',
|
||||
});
|
||||
req.on('error', () => {});
|
||||
req.on('close', common.mustCall(() => {
|
||||
client.close();
|
||||
server.close();
|
||||
}));
|
||||
req.write('{"action":"partial"}');
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user