Skip to content

Commit 103a056

Browse files
committed
Wrap missingServerName, update example
1 parent f77d070 commit 103a056

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

‎examples/ServerName.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ const app = uWS.SSLApp({
77
key_file_name: 'misc/key.pem',
88
cert_file_name: 'misc/cert.pem',
99
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'
10+
}).missingServerName((hostname) => {
11+
12+
/* Note: You don't have to use this callback but can pre-load
13+
* relevant server names up front. This callback is not "async",
14+
* you either add the server name HERE IMMEDIATELY, or the hangshake
15+
* will continue with default certificate (which will most likely fail) */
16+
17+
console.log("Hello! We are missing server name <" + hostname + ">");
18+
19+
/* We simply assume it is localhost, so adding it here */
20+
app.addServerName("localhost", {
21+
key_file_name: 'misc/key.pem',
22+
cert_file_name: 'misc/cert.pem',
23+
passphrase: '1234'
24+
});
25+
1826
}).get('/*', (res, req) => {
1927
res.end('Hello World!');
2028
}).listen(port, (token) => {

‎src/AppWrapper.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,25 @@ void uWS_App_removeServerName(const FunctionCallbackInfo<Value> &args) {
422422
args.GetReturnValue().Set(args.Holder());
423423
}
424424

425+
template <typename APP>
426+
void uWS_App_missingServerName(const FunctionCallbackInfo<Value> &args) {
427+
APP *app = (APP *) args.Holder()->GetAlignedPointerFromInternalField(0);
428+
Isolate *isolate = args.GetIsolate();
429+
430+
UniquePersistent<Function> missingPf;
431+
missingPf.Reset(args.GetIsolate(), Local<Function>::Cast(args[0]));
432+
433+
app->missingServerName([missingPf = std::move(missingPf), isolate](const char *hostname) {
434+
/* We hand a JavaScript string here */
435+
HandleScope hs(isolate);
436+
Local<Function> missingLf = Local<Function>::New(isolate, missingPf);
437+
Local<Value> argv[1] = {String::NewFromUtf8(isolate, hostname, NewStringType::kNormal).ToLocalChecked()};
438+
CallJS(isolate, missingLf, 1, argv);
439+
});
440+
441+
args.GetReturnValue().Set(args.Holder());
442+
}
443+
425444
template <typename APP>
426445
void uWS_App(const FunctionCallbackInfo<Value> &args) {
427446

@@ -496,6 +515,7 @@ void uWS_App(const FunctionCallbackInfo<Value> &args) {
496515
/* SNI */
497516
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "addServerName", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_addServerName<APP>, args.Data()));
498517
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "removeServerName", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_removeServerName<APP>, args.Data()));
518+
appTemplate->PrototypeTemplate()->Set(String::NewFromUtf8(isolate, "missingServerName", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, uWS_App_missingServerName<APP>, args.Data()));
499519

500520
Local<Object> localApp = appTemplate->GetFunction(isolate->GetCurrentContext()).ToLocalChecked()->NewInstance(isolate->GetCurrentContext()).ToLocalChecked();
501521
localApp->SetAlignedPointerInInternalField(0, app);

0 commit comments

Comments
 (0)