Skip to content

Commit f77d070

Browse files
committed
addServerName, removeServerName, ServerName.js
1 parent 0e63c77 commit f77d070

3 files changed

Lines changed: 99 additions & 15 deletions

File tree

‎examples/ServerName.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Minimal SSL example using ServerNameIndication */
2+
3+
const uWS = require('../dist/uws.js');
4+
const port = 9001;
5+
6+
const app = uWS.SSLApp({
7+
key_file_name: 'misc/key.pem',
8+
cert_file_name: 'misc/cert.pem',
9+
passphrase: '1234'
10+
}).addServerName("localhost", {
11+
key_file_name: 'misc/key.pem',
12+
cert_file_name: 'misc/cert.pem',
13+
passphrase: '1234'
14+
}).removeServerName("localhost").addServerName("localhost", {
15+
key_file_name: 'misc/key.pem',
16+
cert_file_name: 'misc/cert.pem',
17+
passphrase: '1234'
18+
}).get('/*', (res, req) => {
19+
res.end('Hello World!');
20+
}).listen(port, (token) => {
21+
if (token) {
22+
console.log('Listening to port ' + port);
23+
} else {
24+
console.log('Failed to listen to port ' + port);
25+
}
26+
});

‎src/AppWrapper.h‎

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,24 +312,20 @@ void uWS_App_publish(const FunctionCallbackInfo<Value> &args) {
312312
app->publish(topic.getString(), message.getString(), BooleanValue(isolate, args[2]) ? uWS::OpCode::BINARY : uWS::OpCode::TEXT, BooleanValue(isolate, args[3]));
313313
}
314314

315-
template <typename APP>
316-
void uWS_App(const FunctionCallbackInfo<Value> &args) {
317-
315+
/* This one modified per-thread static strings temporarily */
316+
std::pair<struct us_socket_context_options_t, bool> readOptionsObject(const FunctionCallbackInfo<Value> &args, int index) {
318317
Isolate *isolate = args.GetIsolate();
319-
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
320-
appTemplate->SetClassName(String::NewFromUtf8(isolate, std::is_same<APP, uWS::SSLApp>::value ? "uWS.SSLApp" : "uWS.App", NewStringType::kNormal).ToLocalChecked());
321-
322318
/* Read the options object if any */
323319
us_socket_context_options_t options = {};
324-
std::string keyFileName, certFileName, passphrase, dhParamsFileName, caFileName;
325-
if (args.Length() == 1) {
320+
thread_local std::string keyFileName, certFileName, passphrase, dhParamsFileName, caFileName;
321+
if (args.Length() > index) {
326322

327-
Local<Object> optionsObject = Local<Object>::Cast(args[0]);
323+
Local<Object> optionsObject = Local<Object>::Cast(args[index]);
328324

329325
/* Key file name */
330326
NativeString keyFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "key_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
331327
if (keyFileNameValue.isInvalid(args)) {
332-
return;
328+
return {};
333329
}
334330
if (keyFileNameValue.getString().length()) {
335331
keyFileName = keyFileNameValue.getString();
@@ -339,7 +335,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
339335
/* Cert file name */
340336
NativeString certFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "cert_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
341337
if (certFileNameValue.isInvalid(args)) {
342-
return;
338+
return {};
343339
}
344340
if (certFileNameValue.getString().length()) {
345341
certFileName = certFileNameValue.getString();
@@ -349,7 +345,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
349345
/* Passphrase */
350346
NativeString passphraseValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "passphrase", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
351347
if (passphraseValue.isInvalid(args)) {
352-
return;
348+
return {};
353349
}
354350
if (passphraseValue.getString().length()) {
355351
passphrase = passphraseValue.getString();
@@ -359,7 +355,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
359355
/* DH params file name */
360356
NativeString dhParamsFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "dh_params_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
361357
if (dhParamsFileNameValue.isInvalid(args)) {
362-
return;
358+
return {};
363359
}
364360
if (dhParamsFileNameValue.getString().length()) {
365361
dhParamsFileName = dhParamsFileNameValue.getString();
@@ -369,7 +365,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
369365
/* CA file name */
370366
NativeString caFileNameValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ca_file_name", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
371367
if (caFileNameValue.isInvalid(args)) {
372-
return;
368+
return {};
373369
}
374370
if (caFileNameValue.getString().length()) {
375371
caFileName = caFileNameValue.getString();
@@ -380,6 +376,64 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
380376
options.ssl_prefer_low_memory_usage = BooleanValue(isolate, optionsObject->Get(isolate->GetCurrentContext(), String::NewFromUtf8(isolate, "ssl_prefer_low_memory_usage", NewStringType::kNormal).ToLocalChecked()).ToLocalChecked());
381377
}
382378

379+
return {options, true};
380+
}
381+
382+
template <typename APP>
383+
void uWS_App_addServerName(const FunctionCallbackInfo<Value> &args) {
384+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
385+
386+
Isolate *isolate = args.GetIsolate();
387+
NativeString hostnamePatternValue(isolate, args[0]);
388+
if (hostnamePatternValue.isInvalid(args)) {
389+
return;
390+
}
391+
std::string hostnamePattern;
392+
if (hostnamePatternValue.getString().length()) {
393+
hostnamePattern = hostnamePatternValue.getString();
394+
}
395+
396+
auto [options, valid] = readOptionsObject(args, 1);
397+
if (!valid) {
398+
return;
399+
}
400+
401+
app->addServerName(hostnamePattern.c_str(), options);
402+
403+
args.GetReturnValue().Set(args.Holder());
404+
}
405+
406+
template <typename APP>
407+
void uWS_App_removeServerName(const FunctionCallbackInfo<Value> &args) {
408+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
409+
410+
Isolate *isolate = args.GetIsolate();
411+
NativeString hostnamePatternValue(isolate, args[0]);
412+
if (hostnamePatternValue.isInvalid(args)) {
413+
return;
414+
}
415+
std::string hostnamePattern;
416+
if (hostnamePatternValue.getString().length()) {
417+
hostnamePattern = hostnamePatternValue.getString();
418+
}
419+
420+
app->removeServerName(hostnamePattern.c_str());
421+
422+
args.GetReturnValue().Set(args.Holder());
423+
}
424+
425+
template <typename APP>
426+
void uWS_App(const FunctionCallbackInfo<Value> &args) {
427+
428+
Isolate *isolate = args.GetIsolate();
429+
Local<FunctionTemplate> appTemplate = FunctionTemplate::New(isolate);
430+
appTemplate->SetClassName(String::NewFromUtf8(isolate, std::is_same<APP, uWS::SSLApp>::value ? "uWS.SSLApp" : "uWS.App", NewStringType::kNormal).ToLocalChecked());
431+
432+
auto [options, valid] = readOptionsObject(args, 0);
433+
if (!valid) {
434+
return;
435+
}
436+
383437
/* uSockets copies strings here */
384438
APP *app = new APP(options);
385439

@@ -439,6 +493,10 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
439493
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "listen", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_listen<APP>, args.Data()));
440494
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "publish", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_publish<APP>, args.Data()));
441495

496+
/* SNI */
497+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "addServerName", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_addServerName<APP>, args.Data()));
498+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "removeServerName", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_removeServerName<APP>, args.Data()));
499+
442500
Local<Object> localApp = appTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
443501
localApp->SetAlignedPointerInInternalField(0, app);
444502

0 commit comments

Comments
 (0)