Add concept of route yielding
This commit is contained in:
+1
-1
@@ -31,7 +31,7 @@ int main(int argc, char **argv) {
|
||||
/* Use this route to signal stop listening */
|
||||
us_listen_socket_close(token);
|
||||
token = nullptr;
|
||||
}).ws<PerSocketData>("/ws", {
|
||||
}).ws<PerSocketData>("/*", {
|
||||
/* Settings */
|
||||
.compression = uWS::DEDICATED_COMPRESSOR,
|
||||
.maxPayloadLength = 16 * 1024 * 1024,
|
||||
|
||||
@@ -217,8 +217,8 @@ public:
|
||||
/* We do not need to check for any close or shutdown here as we immediately return from get handler */
|
||||
|
||||
} else {
|
||||
/* For now we do not support having HTTP and websocket routes on the same URL */
|
||||
res->close();
|
||||
/* Tell the router that we did not handle this request */
|
||||
req->setYield(true);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
+6
-3
@@ -145,8 +145,7 @@ private:
|
||||
|
||||
/* Route the method and URL in two passes */
|
||||
typename HttpContextData<SSL>::RouterData routerData = {(HttpResponse<SSL> *) s, httpRequest};
|
||||
bool firstPass = httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), routerData);
|
||||
if (!firstPass) {
|
||||
if (!httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), routerData)) {
|
||||
/* If first pass failed, we try and match by "any" method */
|
||||
if (!httpContextData->router.route("*", httpRequest->getUrl(), routerData)) {
|
||||
/* If second pass fail, we have to force close this socket as we have no handler for it */
|
||||
@@ -311,10 +310,14 @@ public:
|
||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||
|
||||
httpContextData->router.add(method, pattern, [handler](typename HttpContextData<SSL>::RouterData &user, std::pair<int, std::string_view *> params) {
|
||||
user.httpRequest->setYield(false);
|
||||
user.httpRequest->setParameters(params);
|
||||
handler(user.httpResponse, user.httpRequest);
|
||||
|
||||
// for now all routes handle it
|
||||
/* If any handler yielded, the router will keep looking for a suitable handler. */
|
||||
if (user.httpRequest->getYield()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,10 +37,20 @@ private:
|
||||
std::string_view key, value;
|
||||
} headers[MAX_HEADERS];
|
||||
int querySeparator;
|
||||
bool didYield;
|
||||
|
||||
std::pair<int, std::string_view *> currentParameters;
|
||||
|
||||
public:
|
||||
bool getYield() {
|
||||
return didYield;
|
||||
}
|
||||
|
||||
/* If you do not want to handle this route */
|
||||
void setYield(bool yield) {
|
||||
didYield = yield;
|
||||
}
|
||||
|
||||
std::string_view getHeader(std::string_view header) {
|
||||
for (Header *h = headers; (++h)->key.length(); ) {
|
||||
if (h->key.length() == header.length() && !strncmp(h->key.data(), header.data(), header.length())) {
|
||||
|
||||
Reference in New Issue
Block a user