src: micro-optimize ALPN negotiation · nodejs/node@54753f2 (original) (raw)

Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ using v8::DontDelete;
63 63 using v8::EscapableHandleScope;
64 64 using v8::Exception;
65 65 using v8::External;
66 +using v8::False;
66 67 using v8::Function;
67 68 using v8::FunctionCallback;
68 69 using v8::FunctionCallbackInfo;
@@ -2400,11 +2401,20 @@ void SSLWrap::GetALPNNegotiatedProto(
2400 2401
2401 2402 SSL_get0_alpn_selected(w->ssl_.get(), &alpn_proto, &alpn_proto_len);
2402 2403
2403 -if (!alpn_proto)
2404 -return args.GetReturnValue().Set(false);
2404 + Local result;
2405 +if (alpn_proto_len == 0) {
2406 + result = False(args.GetIsolate());
2407 + } else if (alpn_proto_len == sizeof("h2") - 1 &&
2408 +0 == memcmp(alpn_proto, "h2", sizeof("h2") - 1)) {
2409 + result = w->env()->h2_string();
2410 + } else if (alpn_proto_len == sizeof("http/1.1") - 1 &&
2411 +0 == memcmp(alpn_proto, "http/1.1", sizeof("http/1.1") - 1)) {
2412 + result = w->env()->http_1_1_string();
2413 + } else {
2414 + result = OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len);
2415 + }
2405 2416
2406 - args.GetReturnValue().Set(
2407 -OneByteString(args.GetIsolate(), alpn_proto, alpn_proto_len));
2417 + args.GetReturnValue().Set(result);
2408 2418 }
2409 2419
2410 2420