Skip to content

Commit 17cb272

Browse files
tniessendanielleadams
authored andcommitted
src: deduplicate SetALPN implementations
Instead of accepting either a std::string or a mysterious Local<Value>, accept any std::string_view, which can trivially be constructed from both strings and ArrayBufferViews. This also removes the need to check IsArrayBufferView() inside of SetALPN, which was dead code anyway. PR-URL: #43756 Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent b4c75a9 commit 17cb272

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

src/crypto/crypto_common.cc

+4-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace node {
2626

2727
using v8::Array;
2828
using v8::ArrayBuffer;
29-
using v8::ArrayBufferView;
3029
using v8::BackingStore;
3130
using v8::Context;
3231
using v8::EscapableHandleScope;
@@ -87,18 +86,10 @@ void LogSecret(
8786
keylog_cb(ssl.get(), line.c_str());
8887
}
8988

90-
bool SetALPN(const SSLPointer& ssl, const std::string& alpn) {
91-
return SSL_set_alpn_protos(
92-
ssl.get(),
93-
reinterpret_cast<const uint8_t*>(alpn.c_str()),
94-
alpn.length()) == 0;
95-
}
96-
97-
bool SetALPN(const SSLPointer& ssl, Local<Value> alpn) {
98-
if (!alpn->IsArrayBufferView())
99-
return false;
100-
ArrayBufferViewContents<unsigned char> protos(alpn.As<ArrayBufferView>());
101-
return SSL_set_alpn_protos(ssl.get(), protos.data(), protos.length()) == 0;
89+
bool SetALPN(const SSLPointer& ssl, std::string_view alpn) {
90+
return SSL_set_alpn_protos(ssl.get(),
91+
reinterpret_cast<const uint8_t*>(alpn.data()),
92+
alpn.length()) == 0;
10293
}
10394

10495
MaybeLocal<Value> GetSSLOCSPResponse(

src/crypto/crypto_common.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ void LogSecret(
3333
const unsigned char* secret,
3434
size_t secretlen);
3535

36-
bool SetALPN(const SSLPointer& ssl, const std::string& alpn);
37-
38-
bool SetALPN(const SSLPointer& ssl, v8::Local<v8::Value> alpn);
36+
// TODO(tniessen): use std::u8string_view when we switch to C++20.
37+
bool SetALPN(const SSLPointer& ssl, std::string_view alpn);
3938

4039
v8::MaybeLocal<v8::Value> GetSSLOCSPResponse(
4140
Environment* env,

src/crypto/crypto_tls.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,8 @@ void TLSWrap::SetALPNProtocols(const FunctionCallbackInfo<Value>& args) {
15301530
return env->ThrowTypeError("Must give a Buffer as first argument");
15311531

15321532
if (w->is_client()) {
1533-
CHECK(SetALPN(w->ssl_, args[0]));
1533+
ArrayBufferViewContents<char> protos(args[0].As<ArrayBufferView>());
1534+
CHECK(SetALPN(w->ssl_, {protos.data(), protos.length()}));
15341535
} else {
15351536
CHECK(
15361537
w->object()->SetPrivate(

0 commit comments

Comments
 (0)