Add per-SNI HttpRouter support (initial)
This commit is contained in:
+10
-30
@@ -2,46 +2,26 @@
|
|||||||
|
|
||||||
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
|
/* Note that uWS::SSLApp({options}) is the same as uWS::App() when compiled without SSL support */
|
||||||
|
|
||||||
struct us_listen_socket_t *globalListenSocket;
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
/* Overly simple hello world app (SNI) */
|
/* The SSL context given in SSLApp constructor is the default / catch-all context */
|
||||||
uWS::SSLApp app = uWS::SSLApp({
|
uWS::SSLApp app = uWS::SSLApp({
|
||||||
.key_file_name = "misc/key.pem",
|
.key_file_name = "misc/key.pem",
|
||||||
.cert_file_name = "misc/cert.pem",
|
.cert_file_name = "misc/cert.pem",
|
||||||
.passphrase = "1234"
|
.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*/) {
|
}).get("/*", [](auto *res, auto */*req*/) {
|
||||||
res->end("Hello world!");
|
res->end("Hello from catch-all context!");
|
||||||
}).get("/exit", [](auto *res, auto */*req*/) {
|
}).addServerName("*.google.*", {
|
||||||
res->end("Shutting down!");
|
/* Following is the context for *.google.* domain */
|
||||||
/* We use this to check graceful closedown */
|
.key_file_name = "misc/key.pem",
|
||||||
us_listen_socket_close(1, globalListenSocket);
|
.cert_file_name = "misc/cert.pem",
|
||||||
|
.passphrase = "1234"
|
||||||
|
}).domain("*.google.*").get("/*", [](auto *res, auto */*req*/) {
|
||||||
|
res->end("Hello from *.google.* context!");
|
||||||
}).listen(3000, [](auto *listenSocket) {
|
}).listen(3000, [](auto *listenSocket) {
|
||||||
if (listenSocket) {
|
if (listenSocket) {
|
||||||
std::cout << "Listening on port " << 3000 << std::endl;
|
std::cout << "Listening on port " << 3000 << std::endl;
|
||||||
globalListenSocket = listenSocket;
|
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Failed to listen on port 3000" << std::endl;
|
std::cout << "Failed to listen on port 3000" << std::endl;
|
||||||
}
|
}
|
||||||
});
|
}).run();
|
||||||
|
|
||||||
/* Let's add a wildcard SNI to begin with */
|
|
||||||
app.addServerName("*.google.*", {
|
|
||||||
.key_file_name = "../misc/key.pem",
|
|
||||||
.cert_file_name = "../misc/cert.pem",
|
|
||||||
.passphrase = "1234"
|
|
||||||
});
|
|
||||||
|
|
||||||
app.run();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,7 +104,10 @@ public:
|
|||||||
/* Server name */
|
/* Server name */
|
||||||
TemplatedApp &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
|
TemplatedApp &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {
|
||||||
|
|
||||||
us_socket_context_add_server_name(SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str(), options);
|
/* First we create a new router for this domain */
|
||||||
|
auto *domainRouter = new HttpRouter<typename HttpContextData<SSL>::RouterData>();
|
||||||
|
|
||||||
|
us_socket_context_add_server_name(SSL, (struct us_socket_context_t *) httpContext, hostname_pattern.c_str(), options, domainRouter);
|
||||||
return std::move(*this);
|
return std::move(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -407,6 +410,22 @@ public:
|
|||||||
return std::move(*this);
|
return std::move(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Browse to a server name, changing the router to this domain */
|
||||||
|
TemplatedApp &&domain(std::string serverName) {
|
||||||
|
HttpContextData<SSL> *httpContextData = httpContext->getSocketContextData();
|
||||||
|
|
||||||
|
void *domainRouter = us_socket_context_find_server_name_userdata(SSL, (struct us_socket_context_t *) httpContext, serverName.c_str());
|
||||||
|
if (domainRouter) {
|
||||||
|
std::cout << "Browsed to SNI: " << serverName << std::endl;
|
||||||
|
httpContextData->currentRouter = (decltype(httpContextData->currentRouter)) domainRouter;
|
||||||
|
} else {
|
||||||
|
std::cout << "Cannot browse to SNI: " << serverName << std::endl;
|
||||||
|
httpContextData->currentRouter = &httpContextData->router;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(*this);
|
||||||
|
}
|
||||||
|
|
||||||
TemplatedApp &&get(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
TemplatedApp &&get(std::string pattern, MoveOnlyFunction<void(HttpResponse<SSL> *, HttpRequest *)> &&handler) {
|
||||||
if (httpContext) {
|
if (httpContext) {
|
||||||
httpContext->onHttp("get", pattern, std::move(handler));
|
httpContext->onHttp("get", pattern, std::move(handler));
|
||||||
|
|||||||
+14
-5
@@ -159,9 +159,18 @@ private:
|
|||||||
httpResponseData->state |= HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
|
httpResponseData->state |= HttpResponseData<SSL>::HTTP_CONNECTION_CLOSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Select the router based on SNI (only possible for SSL) */
|
||||||
|
auto *selectedRouter = &httpContextData->router;
|
||||||
|
if constexpr (SSL) {
|
||||||
|
void *domainRouter = us_socket_server_name_userdata(SSL, (struct us_socket_t *) s);
|
||||||
|
if (domainRouter) {
|
||||||
|
selectedRouter = (decltype(selectedRouter)) domainRouter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Route the method and URL */
|
/* Route the method and URL */
|
||||||
httpContextData->router.getUserData() = {(HttpResponse<SSL> *) s, httpRequest};
|
selectedRouter->getUserData() = {(HttpResponse<SSL> *) s, httpRequest};
|
||||||
if (!httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl())) {
|
if (!selectedRouter->route(httpRequest->getMethod(), httpRequest->getUrl())) {
|
||||||
/* We have to force close this socket as we have no handler for it */
|
/* We have to force close this socket as we have no handler for it */
|
||||||
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
|
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@@ -406,12 +415,12 @@ public:
|
|||||||
/* Todo: This is ugly, fix */
|
/* Todo: This is ugly, fix */
|
||||||
std::vector<std::string> methods;
|
std::vector<std::string> methods;
|
||||||
if (method == "*") {
|
if (method == "*") {
|
||||||
methods = httpContextData->router.methods;
|
methods = httpContextData->currentRouter->methods;
|
||||||
} else {
|
} else {
|
||||||
methods = {method};
|
methods = {method};
|
||||||
}
|
}
|
||||||
|
|
||||||
httpContextData->router.add(methods, pattern, [handler = std::move(handler)](auto *r) mutable {
|
httpContextData->currentRouter->add(methods, pattern, [handler = std::move(handler)](auto *r) mutable {
|
||||||
auto user = r->getUserData();
|
auto user = r->getUserData();
|
||||||
user.httpRequest->setYield(false);
|
user.httpRequest->setYield(false);
|
||||||
user.httpRequest->setParameters(r->getParameters());
|
user.httpRequest->setParameters(r->getParameters());
|
||||||
@@ -429,7 +438,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}, method == "*" ? httpContextData->router.LOW_PRIORITY : (upgrade ? httpContextData->router.HIGH_PRIORITY : httpContextData->router.MEDIUM_PRIORITY));
|
}, method == "*" ? httpContextData->currentRouter->LOW_PRIORITY : (upgrade ? httpContextData->currentRouter->HIGH_PRIORITY : httpContextData->currentRouter->MEDIUM_PRIORITY));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Listen to port using this HttpContext */
|
/* Listen to port using this HttpContext */
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ private:
|
|||||||
HttpRequest *httpRequest;
|
HttpRequest *httpRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* This is the currently browsed-to router when using SNI */
|
||||||
|
HttpRouter<RouterData> *currentRouter = &router;
|
||||||
|
|
||||||
|
/* This is the default router for default SNI or non-SSL */
|
||||||
HttpRouter<RouterData> router;
|
HttpRouter<RouterData> router;
|
||||||
void *upgradedWebSocket = nullptr;
|
void *upgradedWebSocket = nullptr;
|
||||||
bool isParsingHttp = false;
|
bool isParsingHttp = false;
|
||||||
|
|||||||
+1
-1
Submodule uSockets updated: 3f0b39ea94...d8967af421
Reference in New Issue
Block a user