http2: improve nghttp2 error callback

The http2 implementation uses the deprecated function
nghttp2_session_callbacks_set_error_callback, which does not supply an
error code but only an error message. This so far forced node's error
callback to rely on the error message in order to distinguish between
different errors, which is fragile and inefficient.

Use the newer nghttp2_session_callbacks_set_error_callback2 function
instead, which is not deprecated and which provides the exact error code
to node's error callback.

PR-URL: https://github.com/nodejs/node/pull/47840
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Tobias Nießen authored and GitHub committed 2023-05-05 14:03:39 +00:00
1 parent 64549731b6
commit 259ea3ed59
2 files changed
+8 -12

No files matched your search

+3 -7
View File
@@ -437,8 +437,7 @@ Http2Session::Callbacks::Callbacks(bool kHasGetPaddingCallback) {
callbacks_, OnFrameNotSent);
nghttp2_session_callbacks_set_on_invalid_header_callback2(
callbacks_, OnInvalidHeader);
nghttp2_session_callbacks_set_error_callback(
callbacks_, OnNghttpError);
nghttp2_session_callbacks_set_error_callback2(callbacks_, OnNghttpError);
nghttp2_session_callbacks_set_send_data_callback(
callbacks_, OnSendData);
nghttp2_session_callbacks_set_on_invalid_frame_recv_callback(
@@ -1257,13 +1256,10 @@ ssize_t Http2Session::OnSelectPadding(nghttp2_session* handle,
return padding;
}
#define BAD_PEER_MESSAGE "Remote peer returned unexpected data while we " \
"expected SETTINGS frame. Perhaps, peer does not " \
"support HTTP/2 properly."
// We use this currently to determine when an attempt is made to use the http2
// protocol with a non-http2 peer.
int Http2Session::OnNghttpError(nghttp2_session* handle,
int lib_error_code,
const char* message,
size_t len,
void* user_data) {
@@ -1271,7 +1267,7 @@ int Http2Session::OnNghttpError(nghttp2_session* handle,
// the session errored because the peer is not an http2 peer.
Http2Session* session = static_cast<Http2Session*>(user_data);
Debug(session, "Error '%s'", message);
if (strncmp(message, BAD_PEER_MESSAGE, len) == 0) {
if (lib_error_code == NGHTTP2_ERR_SETTINGS_EXPECTED) {
Environment* env = session->env();
Isolate* isolate = env->isolate();
HandleScope scope(isolate);
+5 -5
View File
@@ -843,11 +843,11 @@ class Http2Session : public AsyncWrap,
const nghttp2_frame* frame,
size_t maxPayloadLen,
void* user_data);
static int OnNghttpError(
nghttp2_session* session,
const char* message,
size_t len,
void* user_data);
static int OnNghttpError(nghttp2_session* session,
int lib_error_code,
const char* message,
size_t len,
void* user_data);
static int OnSendData(
nghttp2_session* session,
nghttp2_frame* frame,