Skip to content

Commit 6e34c6d

Browse files
committed
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.
1 parent acd2a32 commit 6e34c6d

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

src/crypto/crypto_common.cc

Lines changed: 2 additions & 10 deletions
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,20 +86,13 @@ void LogSecret(
8786
keylog_cb(ssl.get(), line.c_str());
8887
}
8988

90-
bool SetALPN(const SSLPointer& ssl, const std::string& alpn) {
89+
bool SetALPN(const SSLPointer& ssl, std::string_view alpn) {
9190
return SSL_set_alpn_protos(
9291
ssl.get(),
93-
reinterpret_cast<const uint8_t*>(alpn.c_str()),
92+
reinterpret_cast<const uint8_t*>(alpn.data()),
9493
alpn.length()) == 0;
9594
}
9695

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;
102-
}
103-
10496
MaybeLocal<Value> GetSSLOCSPResponse(
10597
Environment* env,
10698
SSL* ssl,

src/crypto/crypto_common.h

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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)