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

http2: cleanup and refactoring of http2 internals #32884

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
133 changes: 38 additions & 95 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2970,6 +2970,9 @@ void Initialize(Local<Object> target,

// Method to fetch the nghttp2 string description of an nghttp2 error code
env->SetMethod(target, "nghttp2ErrorString", HttpErrorString);
env->SetMethod(target, "refreshDefaultSettings", RefreshDefaultSettings);
env->SetMethod(target, "packSettings", PackSettings);
env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions);

Local<String> http2SessionClassName =
FIXED_ONE_BYTE_STRING(isolate, "Http2Session");
Expand Down Expand Up @@ -3038,113 +3041,53 @@ void Initialize(Local<Object> target,
session->GetFunction(env->context()).ToLocalChecked()).Check();

Local<Object> constants = Object::New(isolate);
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(env->context(), \
static_cast<int>(name), \
FIXED_ONE_BYTE_STRING(isolate, \
#name)).Check();
NODE_NGHTTP2_ERROR_CODES(V)

// This does alocate one more slot than needed but it's not used.
#define V(name) FIXED_ONE_BYTE_STRING(isolate, #name),
Local<Value> error_code_names[] = {
HTTP2_ERROR_CODES(V)
Local<Value>() // Unused.
jasnell marked this conversation as resolved.
Show resolved Hide resolved
};
#undef V

NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_REQUEST);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_RESPONSE);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_PUSH_RESPONSE);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_HCAT_HEADERS);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_NV_FLAG_NONE);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_NV_FLAG_NO_INDEX);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_DEFERRED);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_INVALID_ARGUMENT);
NODE_DEFINE_HIDDEN_CONSTANT(constants, NGHTTP2_ERR_STREAM_CLOSED);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_ERR_FRAME_SIZE_ERROR);

NODE_DEFINE_HIDDEN_CONSTANT(constants, STREAM_OPTION_EMPTY_PAYLOAD);
NODE_DEFINE_HIDDEN_CONSTANT(constants, STREAM_OPTION_GET_TRAILERS);

NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_NONE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_END_STREAM);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_END_HEADERS);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_ACK);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_PADDED);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_FLAG_PRIORITY);

NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_HEADER_TABLE_SIZE);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_ENABLE_PUSH);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_MAX_FRAME_SIZE);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE);
NODE_DEFINE_CONSTANT(constants, DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL);
NODE_DEFINE_CONSTANT(constants, MAX_MAX_FRAME_SIZE);
NODE_DEFINE_CONSTANT(constants, MIN_MAX_FRAME_SIZE);
NODE_DEFINE_CONSTANT(constants, MAX_INITIAL_WINDOW_SIZE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_DEFAULT_WEIGHT);
Local<Array> name_for_error_code =
Array::New(
isolate,
error_code_names,
arraysize(error_code_names) - 1);

target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "nameForErrorCode"),
name_for_error_code).Check();

#define V(constant) NODE_DEFINE_HIDDEN_CONSTANT(constants, constant);
HTTP2_HIDDEN_CONSTANTS(V)
#undef V

NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_HEADER_TABLE_SIZE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_ENABLE_PUSH);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_MAX_FRAME_SIZE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE);
NODE_DEFINE_CONSTANT(constants, NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL);
#define V(constant) NODE_DEFINE_CONSTANT(constants, constant);
HTTP2_CONSTANTS(V)
#undef V

NODE_DEFINE_CONSTANT(constants, PADDING_STRATEGY_NONE);
NODE_DEFINE_CONSTANT(constants, PADDING_STRATEGY_ALIGNED);
NODE_DEFINE_CONSTANT(constants, PADDING_STRATEGY_MAX);
NODE_DEFINE_CONSTANT(constants, PADDING_STRATEGY_CALLBACK);
// NGHTTP2_DEFAULT_WEIGHT is a macro and not a regular define
// it won't be set properly on the constants object if included
// in the HTTP2_CONSTANTS macro.
NODE_DEFINE_CONSTANT(constants, NGHTTP2_DEFAULT_WEIGHT);

#define STRING_CONSTANT(NAME, VALUE) \
#define V(NAME, VALUE) \
NODE_DEFINE_STRING_CONSTANT(constants, "HTTP2_HEADER_" # NAME, VALUE);
HTTP_KNOWN_HEADERS(STRING_CONSTANT)
#undef STRING_CONSTANT
HTTP_KNOWN_HEADERS(V)
#undef V

#define STRING_CONSTANT(NAME, VALUE) \
#define V(NAME, VALUE) \
NODE_DEFINE_STRING_CONSTANT(constants, "HTTP2_METHOD_" # NAME, VALUE);
HTTP_KNOWN_METHODS(STRING_CONSTANT)
#undef STRING_CONSTANT
HTTP_KNOWN_METHODS(V)
#undef V

#define V(name, _) NODE_DEFINE_CONSTANT(constants, HTTP_STATUS_##name);
HTTP_STATUS_CODES(V)
HTTP_STATUS_CODES(V)
#undef V

env->SetMethod(target, "refreshDefaultSettings", RefreshDefaultSettings);
env->SetMethod(target, "packSettings", PackSettings);
env->SetMethod(target, "setCallbackFunctions", SetCallbackFunctions);

target->Set(context,
env->constants_string(),
constants).Check();
target->Set(context,
FIXED_ONE_BYTE_STRING(isolate, "nameForErrorCode"),
name_for_error_code).Check();
target->Set(context, env->constants_string(), constants).Check();
}
} // namespace http2
} // namespace node
Expand Down
90 changes: 80 additions & 10 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ constexpr size_t kDefaultMaxSettings = 10;
constexpr uint64_t kDefaultMaxSessionMemory = 10000000;

// These are the standard HTTP/2 defaults as specified by the RFC
#define DEFAULT_SETTINGS_HEADER_TABLE_SIZE 4096
#define DEFAULT_SETTINGS_ENABLE_PUSH 1
#define DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS 0xffffffffu
#define DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE 65535
#define DEFAULT_SETTINGS_MAX_FRAME_SIZE 16384
#define DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE 65535
#define DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL 0
#define MAX_MAX_FRAME_SIZE 16777215
#define MIN_MAX_FRAME_SIZE DEFAULT_SETTINGS_MAX_FRAME_SIZE
#define MAX_INITIAL_WINDOW_SIZE 2147483647
constexpr uint32_t DEFAULT_SETTINGS_HEADER_TABLE_SIZE = 4096;
constexpr uint32_t DEFAULT_SETTINGS_ENABLE_PUSH = 1;
constexpr uint32_t DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS = 0xffffffffu;
constexpr uint32_t DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE = 65535;
constexpr uint32_t DEFAULT_SETTINGS_MAX_FRAME_SIZE = 16384;
constexpr uint32_t DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE = 65535;
constexpr uint32_t DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL = 0;
constexpr uint32_t MAX_MAX_FRAME_SIZE = 16777215;
constexpr uint32_t MIN_MAX_FRAME_SIZE = DEFAULT_SETTINGS_MAX_FRAME_SIZE;
constexpr uint32_t MAX_INITIAL_WINDOW_SIZE = 2147483647;

template <typename T, void(*fn)(T*)>
struct Nghttp2Deleter {
Expand Down Expand Up @@ -1054,6 +1054,76 @@ class Origins {
MaybeStackBuffer<char, 512> buf_;
};

#define HTTP2_HIDDEN_CONSTANTS(V) \
V(NGHTTP2_HCAT_REQUEST) \
V(NGHTTP2_HCAT_RESPONSE) \
V(NGHTTP2_HCAT_PUSH_RESPONSE) \
V(NGHTTP2_HCAT_HEADERS) \
V(NGHTTP2_NV_FLAG_NONE) \
V(NGHTTP2_NV_FLAG_NO_INDEX) \
V(NGHTTP2_ERR_DEFERRED) \
V(NGHTTP2_ERR_STREAM_ID_NOT_AVAILABLE) \
V(NGHTTP2_ERR_INVALID_ARGUMENT) \
V(NGHTTP2_ERR_STREAM_CLOSED) \
V(STREAM_OPTION_EMPTY_PAYLOAD) \
V(STREAM_OPTION_GET_TRAILERS)

#define HTTP2_ERROR_CODES(V) \
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 HTTP2_CONSTANTS(V) \
V(NGHTTP2_ERR_FRAME_SIZE_ERROR) \
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_FLAG_NONE) \
V(NGHTTP2_FLAG_END_STREAM) \
V(NGHTTP2_FLAG_END_HEADERS) \
V(NGHTTP2_FLAG_ACK) \
V(NGHTTP2_FLAG_PADDED) \
V(NGHTTP2_FLAG_PRIORITY) \
V(DEFAULT_SETTINGS_HEADER_TABLE_SIZE) \
V(DEFAULT_SETTINGS_ENABLE_PUSH) \
V(DEFAULT_SETTINGS_MAX_CONCURRENT_STREAMS) \
V(DEFAULT_SETTINGS_INITIAL_WINDOW_SIZE) \
V(DEFAULT_SETTINGS_MAX_FRAME_SIZE) \
V(DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE) \
V(DEFAULT_SETTINGS_ENABLE_CONNECT_PROTOCOL) \
V(MAX_MAX_FRAME_SIZE) \
V(MIN_MAX_FRAME_SIZE) \
V(MAX_INITIAL_WINDOW_SIZE) \
V(NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) \
V(NGHTTP2_SETTINGS_ENABLE_PUSH) \
V(NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS) \
V(NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE) \
V(NGHTTP2_SETTINGS_MAX_FRAME_SIZE) \
V(NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE) \
V(NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL) \
V(PADDING_STRATEGY_NONE) \
V(PADDING_STRATEGY_ALIGNED) \
V(PADDING_STRATEGY_MAX) \
V(PADDING_STRATEGY_CALLBACK) \
HTTP2_ERROR_CODES(V)

} // namespace http2
} // namespace node

Expand Down