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

[v9.x backport] http2: callback valid check before closing request #19229

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
4 changes: 2 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,8 @@ class Http2Stream extends Duplex {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
if (code < 0 || code > kMaxInt)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');
if (callback !== undefined && typeof callback !== 'function')
throw new errors.TypeError('ERR_INVALID_CALLBACK');

// Unenroll the timeout.
unenroll(this);
Expand All @@ -1780,8 +1782,6 @@ class Http2Stream extends Duplex {
state.rstCode = code;

if (callback !== undefined) {
if (typeof callback !== 'function')
throw new errors.TypeError('ERR_INVALID_CALLBACK');
this.once('close', callback);
}

Expand Down
26 changes: 25 additions & 1 deletion test/parallel/test-http2-client-rststream-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@ server.on('stream', (stream) => {
server.listen(0, common.mustCall(() => {
const client = h2.connect(`http://localhost:${server.address().port}`);
const req = client.request();
req.close(1);
const closeCode = 1;

common.expectsError(
() => req.close(2 ** 32),
{
type: RangeError,
code: 'ERR_OUT_OF_RANGE',
message: 'The value of "code" is out of range.'
}
);
assert.strictEqual(req.closed, false);

[true, 1, {}, [], null, 'test'].forEach((notFunction) => {
common.expectsError(
() => req.close(closeCode, notFunction),
{
type: TypeError,
code: 'ERR_INVALID_CALLBACK',
message: 'Callback must be a function'
}
);
assert.strictEqual(req.closed, false);
});

req.close(closeCode, common.mustCall());
assert.strictEqual(req.closed, true);

// make sure that destroy is called
Expand Down