Skip to content

Commit

Permalink
http2: improve nghttp2 error callback
Browse files Browse the repository at this point in the history
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: #47840
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
tniessen authored and targos committed May 12, 2023
1 parent 54607bf commit a4d6543
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1257,21 +1256,18 @@ 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) {
// Unfortunately, this is currently the only way for us to know if
// 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);
Expand Down
10 changes: 5 additions & 5 deletions src/node_http2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a4d6543

Please sign in to comment.