From 0ec612876d8422ea641ff34a06e2d6ca6b117bb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 3 May 2023 13:46:50 +0000 Subject: [PATCH] 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. --- src/node_http2.cc | 10 +++------- src/node_http2.h | 10 +++++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/node_http2.cc b/src/node_http2.cc index 29d1207ecff58f..01d0eb3418b39f 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -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(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); diff --git a/src/node_http2.h b/src/node_http2.h index fea51d2d7f3b28..87f6ab8305a7d8 100644 --- a/src/node_http2.h +++ b/src/node_http2.h @@ -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,