Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: deduplicate SetALPN implementations #43756

Merged
merged 1 commit into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace node {

using v8::Array;
using v8::ArrayBuffer;
using v8::ArrayBufferView;
using v8::BackingStore;
using v8::Context;
using v8::EscapableHandleScope;
Expand Down Expand Up @@ -87,18 +86,10 @@ void LogSecret(
keylog_cb(ssl.get(), line.c_str());
}

bool SetALPN(const SSLPointer& ssl, const std::string& alpn) {
return SSL_set_alpn_protos(
ssl.get(),
reinterpret_cast<const uint8_t*>(alpn.c_str()),
alpn.length()) == 0;
}

bool SetALPN(const SSLPointer& ssl, Local<Value> alpn) {
if (!alpn->IsArrayBufferView())
return false;
ArrayBufferViewContents<unsigned char> protos(alpn.As<ArrayBufferView>());
return SSL_set_alpn_protos(ssl.get(), protos.data(), protos.length()) == 0;
bool SetALPN(const SSLPointer& ssl, std::string_view alpn) {
return SSL_set_alpn_protos(ssl.get(),
reinterpret_cast<const uint8_t*>(alpn.data()),
alpn.length()) == 0;
}

MaybeLocal<Value> GetSSLOCSPResponse(
Expand Down
5 changes: 2 additions & 3 deletions src/crypto/crypto_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ void LogSecret(
const unsigned char* secret,
size_t secretlen);

bool SetALPN(const SSLPointer& ssl, const std::string& alpn);

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

v8::MaybeLocal<v8::Value> GetSSLOCSPResponse(
Environment* env,
Expand Down
3 changes: 2 additions & 1 deletion src/crypto/crypto_tls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,8 @@ void TLSWrap::SetALPNProtocols(const FunctionCallbackInfo<Value>& args) {
return env->ThrowTypeError("Must give a Buffer as first argument");

if (w->is_client()) {
CHECK(SetALPN(w->ssl_, args[0]));
ArrayBufferViewContents<char> protos(args[0].As<ArrayBufferView>());
CHECK(SetALPN(w->ssl_, {protos.data(), protos.length()}));
} else {
CHECK(
w->object()->SetPrivate(
Expand Down