Skip to content

Commit 19dfa47

Browse files
tniessentargos
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 67ecd27 commit 19dfa47

File tree

3 files changed

+8
-17
lines changed

3 files changed

+8
-17
lines changed

src/crypto/crypto_common.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace node {
2727

2828
using v8::Array;
2929
using v8::ArrayBuffer;
30-
using v8::ArrayBufferView;
3130
using v8::BackingStore;
3231
using v8::Context;
3332
using v8::EscapableHandleScope;
@@ -83,18 +82,10 @@ void LogSecret(
8382
keylog_cb(ssl.get(), line.c_str());
8483
}
8584

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

10091
MaybeLocal<Value> GetSSLOCSPResponse(

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)