From d9c86f3e9385edc721446cd4046313d1a88fd5dc Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Thu, 5 Dec 2019 21:30:49 +0100 Subject: [PATCH] Wrap ca_file_name option --- examples/WorkerThreads.js | 2 +- src/AppWrapper.h | 128 +++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 66 deletions(-) diff --git a/examples/WorkerThreads.js b/examples/WorkerThreads.js index 043875c..b06be08 100644 --- a/examples/WorkerThreads.js +++ b/examples/WorkerThreads.js @@ -17,7 +17,7 @@ if (isMainThread) { /* I guess main thread joins by default? */ } else { /* Here we are inside a worker thread */ - const app = uWS./*SSL*/App({ + const app = uWS.SSLApp({ key_file_name: 'misc/key.pem', cert_file_name: 'misc/cert.pem', passphrase: '1234' diff --git a/src/AppWrapper.h b/src/AppWrapper.h index 67441b5..30af474 100644 --- a/src/AppWrapper.h +++ b/src/AppWrapper.h @@ -256,80 +256,78 @@ template void uWS_App(const FunctionCallbackInfo &args) { Isolate *isolate = args.GetIsolate(); - Local appTemplate = FunctionTemplate::New(isolate); + appTemplate->SetClassName(String::NewFromUtf8(isolate, std::is_same::value ? "uWS.SSLApp" : "uWS.App", NewStringType::kNormal).ToLocalChecked()); - APP *app; + /* Read the options object if any */ + us_socket_context_options_t options = {}; + if (args.Length() == 1) { - /* These won't outlive the function, uSockets will have to copy strings it wants to keep! */ - std::string keyFileName; - std::string certFileName; - std::string passphrase; - std::string dhParamsFileName; + Local optionsObject = Local::Cast(args[0]); - /* Name differs based on type */ - if (std::is_same::value) { - appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.SSLApp", NewStringType::kNormal).ToLocalChecked()); + std::string keyFileName; + std::string certFileName; + std::string passphrase; + std::string dhParamsFileName; + std::string caFileName; - /* We fill these below */ - us_socket_context_options_t ssl_options = {}; - - /* Read the options object (SSL options) */ - if (args.Length() == 1) { - - Local optionsObject = Local::Cast(args[0]); - - /* Key file name */ - NativeString keyFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "key_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); - if (keyFileNameValue.isInvalid(args)) { - return; - } - if (keyFileNameValue.getString().length()) { - keyFileName = keyFileNameValue.getString(); - ssl_options.key_file_name = keyFileName.c_str(); - } - - /* Cert file name */ - NativeString certFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "cert_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); - if (certFileNameValue.isInvalid(args)) { - return; - } - if (certFileNameValue.getString().length()) { - certFileName = certFileNameValue.getString(); - ssl_options.cert_file_name = certFileName.c_str(); - } - - /* Passphrase */ - NativeString passphraseValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "passphrase", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); - if (passphraseValue.isInvalid(args)) { - return; - } - if (passphraseValue.getString().length()) { - passphrase = passphraseValue.getString(); - ssl_options.passphrase = passphrase.c_str(); - } - - /* DH params file name */ - NativeString dhParamsFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "dh_params_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); - if (dhParamsFileNameValue.isInvalid(args)) { - return; - } - if (dhParamsFileNameValue.getString().length()) { - dhParamsFileName = dhParamsFileNameValue.getString(); - ssl_options.dh_params_file_name = dhParamsFileName.c_str(); - } - - /* ssl_prefer_low_memory_usage */ - ssl_options.ssl_prefer_low_memory_usage = BooleanValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ssl_prefer_low_memory_usage", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + /* Key file name */ + NativeString keyFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "key_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + if (keyFileNameValue.isInvalid(args)) { + return; + } + if (keyFileNameValue.getString().length()) { + keyFileName = keyFileNameValue.getString(); + options.key_file_name = keyFileName.c_str(); } - /* uSockets should really copy strings it wants to keep */ - app = new APP(ssl_options); - } else { - appTemplate->SetClassName(String::NewFromUtf8(isolate, "uWS.App", NewStringType::kNormal).ToLocalChecked()); - app = new APP; + /* Cert file name */ + NativeString certFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "cert_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + if (certFileNameValue.isInvalid(args)) { + return; + } + if (certFileNameValue.getString().length()) { + certFileName = certFileNameValue.getString(); + options.cert_file_name = certFileName.c_str(); + } + + /* Passphrase */ + NativeString passphraseValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "passphrase", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + if (passphraseValue.isInvalid(args)) { + return; + } + if (passphraseValue.getString().length()) { + passphrase = passphraseValue.getString(); + options.passphrase = passphrase.c_str(); + } + + /* DH params file name */ + NativeString dhParamsFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "dh_params_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + if (dhParamsFileNameValue.isInvalid(args)) { + return; + } + if (dhParamsFileNameValue.getString().length()) { + dhParamsFileName = dhParamsFileNameValue.getString(); + options.dh_params_file_name = dhParamsFileName.c_str(); + } + + /* CA file name */ + NativeString caFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ca_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); + if (caFileNameValue.isInvalid(args)) { + return; + } + if (caFileNameValue.getString().length()) { + caFileName = caFileNameValue.getString(); + options.ca_file_name = caFileName.c_str(); + } + + /* ssl_prefer_low_memory_usage */ + options.ssl_prefer_low_memory_usage = BooleanValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ssl_prefer_low_memory_usage", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked()); } + /* uSockets copies strings here */ + APP *app = new APP(options); + /* Throw if we failed to construct the app */ if (app->constructorFailed()) { delete app;