mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 23:52:40 +00:00

This slightly alters the behaviour of session close by first using .end() on a session socket to finish writing the data and only then calls .destroy() to make sure the Readable side is closed. This allows the socket to finish transmitting data, receive proper FIN packet and avoid ECONNRESET errors upon graceful close. onStreamClose now directly calls stream.destroy() instead of kMaybeDestroy because the latter will first check that the stream has writableFinished set. And that may not be true as we have just (synchronously) called .end() on the stream if it was not closed and that doesn't give it enough time to finish. Furthermore there is no point in waiting for 'finish' as the other party have already closed the stream and we won't be able to write anyway. This also changes a few tests to correctly handle graceful session close. This includes: * not reading request data (on client side) * not reading push stream data (on client side) * relying on socket.destroy() (on client) to finish server session due to the destroy of the socket without closing the server session. As the goaway itself is *not* a session close. Added few 'close' event mustCall checks. PR-URL: https://github.com/nodejs/node/pull/30854 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const assert = require('assert');
|
|
const http2 = require('http2');
|
|
|
|
const server = http2.createServer();
|
|
server.on('stream', common.mustCall((stream, headers) => {
|
|
const port = server.address().port;
|
|
if (headers[':path'] === '/') {
|
|
stream.pushStream({
|
|
':scheme': 'http',
|
|
':path': '/foobar',
|
|
':authority': `localhost:${port}`,
|
|
}, common.mustCall((err, push, headers) => {
|
|
assert.ifError(err);
|
|
push.respond({
|
|
'content-type': 'text/html',
|
|
':status': 200,
|
|
'x-push-data': 'pushed by server',
|
|
});
|
|
push.end('pushed by server data');
|
|
|
|
common.expectsError(() => {
|
|
push.pushStream({}, common.mustNotCall());
|
|
}, {
|
|
code: 'ERR_HTTP2_NESTED_PUSH',
|
|
type: Error
|
|
});
|
|
|
|
stream.end('test');
|
|
}));
|
|
}
|
|
stream.respond({
|
|
'content-type': 'text/html',
|
|
':status': 200
|
|
});
|
|
}));
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const port = server.address().port;
|
|
const headers = { ':path': '/' };
|
|
const client = http2.connect(`http://localhost:${port}`);
|
|
const req = client.request(headers);
|
|
|
|
client.on('stream', common.mustCall((stream, headers) => {
|
|
assert.strictEqual(headers[':scheme'], 'http');
|
|
assert.strictEqual(headers[':path'], '/foobar');
|
|
assert.strictEqual(headers[':authority'], `localhost:${port}`);
|
|
stream.on('push', common.mustCall((headers) => {
|
|
assert.strictEqual(headers[':status'], 200);
|
|
assert.strictEqual(headers['content-type'], 'text/html');
|
|
assert.strictEqual(headers['x-push-data'], 'pushed by server');
|
|
}));
|
|
stream.on('aborted', common.mustNotCall());
|
|
// We have to read the data of the push stream to end gracefully.
|
|
stream.resume();
|
|
}));
|
|
|
|
let data = '';
|
|
|
|
req.on('data', common.mustCall((d) => data += d));
|
|
req.on('end', common.mustCall(() => {
|
|
assert.strictEqual(data, 'test');
|
|
server.close();
|
|
client.close();
|
|
}));
|
|
req.end();
|
|
}));
|