From f69282c478abe97095b7a34739c176a0c0c2cf60 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 17 Aug 2020 05:39:14 +0200 Subject: [PATCH] missingServerName and updated ServerName example --- examples/ServerName.cpp | 21 +++++++++++++++------ src/App.h | 13 +++++++++++++ src/HttpContextData.h | 3 +++ uSockets | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/examples/ServerName.cpp b/examples/ServerName.cpp index b2e139e..dd7d01c 100644 --- a/examples/ServerName.cpp +++ b/examples/ServerName.cpp @@ -4,21 +4,30 @@ int main() { /* Overly simple hello world app (SNI) */ - uWS::SSLApp({ - .key_file_name = "../misc/key.pem", - .cert_file_name = "../misc/cert.pem", - .passphrase = "1234" - }).addServerName("localhost", { + uWS::SSLApp app = uWS::SSLApp({ .key_file_name = "../misc/key.pem", .cert_file_name = "../misc/cert.pem", .passphrase = "1234" + }).missingServerName([&app](const char *hostname) { + + printf("We are missing server name: <%s>\n", hostname); + + /* Assume it is localhost, so add it */ + app.addServerName("localhost", { + .key_file_name = "../misc/key.pem", + .cert_file_name = "../misc/cert.pem", + .passphrase = "1234" + }); + }).get("/*", [](auto *res, auto *req) { res->end("Hello world!"); }).listen(3000, [](auto *token) { if (token) { std::cout << "Listening on port " << 3000 << std::endl; } - }).run(); + }); + + app.run(); std::cout << "Failed to listen on port 3000" << std::endl; } diff --git a/src/App.h b/src/App.h index cce04cd..896f0a1 100644 --- a/src/App.h +++ b/src/App.h @@ -51,6 +51,19 @@ public: return std::move(*this); } + TemplatedApp &&missingServerName(fu2::unique_function handler) { + + httpContext->getSocketContextData()->missingServerNameHandler = std::move(handler); + + us_socket_context_on_server_name(SSL, (struct us_socket_context_t *) httpContext, [](struct us_socket_context_t *context, const char *hostname) { + + /* This is the only requirements of being friends with HttpContextData */ + HttpContext *httpContext = (HttpContext *) context; + httpContext->getSocketContextData()->missingServerNameHandler(hostname); + }); + return std::move(*this); + } + /* Returns the SSL_CTX of this app, or nullptr. */ void *getNativeHandle() { return us_socket_context_get_native_handle(SSL, (struct us_socket_context_t *) httpContext); diff --git a/src/HttpContextData.h b/src/HttpContextData.h index 77760df..3bf4412 100644 --- a/src/HttpContextData.h +++ b/src/HttpContextData.h @@ -31,9 +31,12 @@ template struct alignas(16) HttpContextData { template friend struct HttpContext; template friend struct HttpResponse; + template friend struct TemplatedApp; private: std::vector *, int)>> filterHandlers; + fu2::unique_function missingServerNameHandler; + struct RouterData { HttpResponse *httpResponse; HttpRequest *httpRequest; diff --git a/uSockets b/uSockets index cd62d44..42bdf7f 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit cd62d4409bcebc1bf2ff72acf8113269b7ecc675 +Subproject commit 42bdf7fbd25a500808f694ac6b125e837eb42a33