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] Various http2 changes #19579

Closed
wants to merge 6 commits 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
5 changes: 3 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const {
} = require('timers');

const { ShutdownWrap, WriteWrap } = process.binding('stream_wrap');
const { constants } = binding;
const { constants, nameForErrorCode } = binding;

const NETServer = net.Server;
const TLSServer = tls.Server;
Expand Down Expand Up @@ -1841,7 +1841,8 @@ class Http2Stream extends Duplex {
// abort and is already covered by aborted event, also allows more
// seamless compatibility with http1
if (err == null && code !== NGHTTP2_NO_ERROR && code !== NGHTTP2_CANCEL)
err = new errors.Error('ERR_HTTP2_STREAM_ERROR', code);
err = new errors.Error('ERR_HTTP2_STREAM_ERROR',
nameForErrorCode[code] || code);

this[kSession] = undefined;
this[kHandle] = undefined;
Expand Down
59 changes: 36 additions & 23 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2857,29 +2857,39 @@ void Initialize(Local<Object> target,
session->GetFunction()).FromJust();

Local<Object> constants = Object::New(isolate);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SESSION_SERVER);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SESSION_CLIENT);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_IDLE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_OPEN);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_RESERVED_LOCAL);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_RESERVED_REMOTE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_STATE_CLOSED);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_NO_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_PROTOCOL_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_INTERNAL_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLOW_CONTROL_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_TIMEOUT);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_STREAM_CLOSED);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FRAME_SIZE_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_REFUSED_STREAM);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_CANCEL);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_COMPRESSION_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_CONNECT_ERROR);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_ENHANCE_YOUR_CALM);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_INADEQUATE_SECURITY);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_HTTP_1_1_REQUIRED);
Local<Array> name_for_error_code = Array::New(isolate);

#define NODE_NGHTTP2_ERROR_CODES(V) \
V(NGHTTP2_SESSION_SERVER); \
V(NGHTTP2_SESSION_CLIENT); \
V(NGHTTP2_STREAM_STATE_IDLE); \
V(NGHTTP2_STREAM_STATE_OPEN); \
V(NGHTTP2_STREAM_STATE_RESERVED_LOCAL); \
V(NGHTTP2_STREAM_STATE_RESERVED_REMOTE); \
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_LOCAL); \
V(NGHTTP2_STREAM_STATE_HALF_CLOSED_REMOTE); \
V(NGHTTP2_STREAM_STATE_CLOSED); \
V(NGHTTP2_NO_ERROR); \
V(NGHTTP2_PROTOCOL_ERROR); \
V(NGHTTP2_INTERNAL_ERROR); \
V(NGHTTP2_FLOW_CONTROL_ERROR); \
V(NGHTTP2_SETTINGS_TIMEOUT); \
V(NGHTTP2_STREAM_CLOSED); \
V(NGHTTP2_FRAME_SIZE_ERROR); \
V(NGHTTP2_REFUSED_STREAM); \
V(NGHTTP2_CANCEL); \
V(NGHTTP2_COMPRESSION_ERROR); \
V(NGHTTP2_CONNECT_ERROR); \
V(NGHTTP2_ENHANCE_YOUR_CALM); \
V(NGHTTP2_INADEQUATE_SECURITY); \
V(NGHTTP2_HTTP_1_1_REQUIRED); \

#define V(name) \
NODE_DEFINE_CONSTANT(constants, name); \
name_for_error_code->Set(static_cast<int>(name), \
FIXED_ONE_BYTE_STRING(isolate, #name));
NODE_NGHTTP2_ERROR_CODES(V)
#undef V

NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_REQUEST);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_RESPONSE);
Expand Down Expand Up @@ -2944,6 +2954,9 @@ HTTP_STATUS_CODES(V)
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "constants"),
constants).FromJust();
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "nameForErrorCode"),
name_for_error_code).FromJust();
}
} // namespace http2
} // namespace node
Expand Down
5 changes: 5 additions & 0 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,11 @@ exports.expectsError = function expectsError(fn, settings, exact) {
}
assert(error instanceof type,
`${error.name} is not instance of ${type.name}`);
let typeName = error.constructor.name;
if (typeName === 'NodeError' && type.name !== 'NodeError') {
typeName = Object.getPrototypeOf(error.constructor).name;
}
assert.strictEqual(typeName, type.name);
}
if ('message' in settings) {
const message = settings.message;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ common.expectsError(
}, ''),
{
code: 'ERR_INVALID_OPT_VALUE',
type: Error,
type: TypeError,
message: 'The value "undefined" is invalid for option "padding"'
});

Expand All @@ -47,7 +47,7 @@ common.expectsError(
}, ''),
{
code: 'ERR_INVALID_OPT_VALUE',
type: Error,
type: TypeError,
message: 'The value "undefined" is invalid for option "saltLength"'
});

Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-http2-client-http1-server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const http = require('http');
const http2 = require('http2');
const { NghttpError } = require('internal/http2/util');

// Creating an http1 server here...
const server = http.createServer(common.mustNotCall());
Expand All @@ -18,13 +20,14 @@ server.listen(0, common.mustCall(() => {

req.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
message: 'Protocol error'
}));

client.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: 'Protocol error'
}));

Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http2-client-onconnect-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -10,6 +11,7 @@ const {
nghttp2ErrorString
} = process.binding('http2');
const http2 = require('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within requestOnConnect
// - NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE (should emit session error)
Expand Down Expand Up @@ -51,7 +53,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'session'
Expand Down
20 changes: 16 additions & 4 deletions test/parallel/test-http2-client-rststream-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,38 @@ 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 "code" argument is out of range'
}
);
assert.strictEqual(req.closed, false);

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

// Make sure that destroy is called.
req._destroy = common.mustCall(req._destroy.bind(req));

// Second call doesn't do anything.
req.close(8);
req.close(closeCode + 1);

req.on('close', common.mustCall((code) => {
assert.strictEqual(req.destroyed, true);
assert.strictEqual(code, 1);
assert.strictEqual(code, closeCode);
server.close();
client.close();
}));

req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 1'
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
}));

req.on('response', common.mustCall());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ server.on('stream', (stream) => {
// system specific timings.
stream.on('error', (err) => {
assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR');
assert.strictEqual(err.message, 'Stream closed with error code 2');
assert.strictEqual(err.message,
'Stream closed with error code NGHTTP2_INTERNAL_ERROR');
});
stream.respond();
stream.end();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-client-unescaped-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 1'
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
}));
req.on('close', common.mustCall(() => countdown.dec()));
}
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http2-compat-serverresponse-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 2'
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
}));
req.on('close', common.mustCall(() => countdown.dec()));

Expand All @@ -73,7 +73,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 2'
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
}));
req.on('close', common.mustCall(() => countdown.dec()));

Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-http2-info-headers-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');
if (!common.hasCrypto)
Expand All @@ -9,6 +10,7 @@ const {
Http2Stream,
nghttp2ErrorString
} = process.binding('http2');
const { NghttpError } = require('internal/http2/util');

// tests error handling within additionalHeaders
// - every other NGHTTP2 error from binding (should emit stream error)
Expand All @@ -24,7 +26,8 @@ const genericTests = Object.getOwnPropertyNames(constants)
ngError: constants[key],
error: {
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
name: 'Error [ERR_HTTP2_ERROR]',
message: nghttp2ErrorString(constants[key])
},
type: 'stream'
Expand Down Expand Up @@ -69,7 +72,7 @@ function runTest(test) {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 2'
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
}));

req.on('close', common.mustCall(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-max-concurrent-streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 7'
message: 'Stream closed with error code NGHTTP2_REFUSED_STREAM'
}));
}
}));
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ server.on('stream', (stream) => {
stream.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 3'
message: 'Stream closed with error code NGHTTP2_FLOW_CONTROL_ERROR'
}));
stream.on('close', common.mustCall(() => {
server.close();
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-misbehaving-flow-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ server.on('stream', (stream) => {
stream.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 3'
message: 'Stream closed with error code NGHTTP2_FLOW_CONTROL_ERROR'
}));
stream.on('close', common.mustCall(() => {
server.close(common.mustCall());
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-misbehaving-multiplex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// Flags: --expose-internals

const common = require('../common');

Expand All @@ -7,6 +8,7 @@ if (!common.hasCrypto)

const h2 = require('http2');
const net = require('net');
const { NghttpError } = require('internal/http2/util');
const h2test = require('../common/http2');
let client;

Expand All @@ -25,7 +27,7 @@ server.on('stream', common.mustCall((stream) => {
server.on('session', common.mustCall((session) => {
session.on('error', common.expectsError({
code: 'ERR_HTTP2_ERROR',
type: Error,
type: NghttpError,
message: 'Stream was already closed or invalid'
}));
}));
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-misused-pseudoheaders.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 2'
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
}));

req.on('response', common.mustCall());
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-multi-content-length.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 1'
message: 'Stream closed with error code NGHTTP2_PROTOCOL_ERROR'
}));
}
}));
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ server.listen(0, common.mustCall(() => {
req.on('error', common.expectsError({
code: 'ERR_HTTP2_STREAM_ERROR',
type: Error,
message: 'Stream closed with error code 7'
message: 'Stream closed with error code NGHTTP2_REFUSED_STREAM'
}));
}));
Loading