Hook up req->getParameter
This commit is contained in:
+7
-6
@@ -16,24 +16,25 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
res->end("GET /WILDCARD");
|
res->end("GET /WILDCARD");
|
||||||
|
|
||||||
})*/.get("/:param1/:param2", [](auto *res, auto *req/*, auto ¶ms*/) {
|
})*/.get("/:param1/:param2", [](auto *res, auto *req) {
|
||||||
|
|
||||||
// todo: read from params (req->getParameter(0))
|
|
||||||
res->write("GET /:param1/:param2 = ");
|
res->write("GET /:param1/:param2 = ");
|
||||||
res->end(req->getUrl());
|
res->write(req->getParameter(0));
|
||||||
|
res->write(" and ");
|
||||||
|
res->end(req->getParameter(1));
|
||||||
|
|
||||||
}).post("/hello", [asyncFileStreamer](auto *res, auto *req/*, auto ¶ms*/) {
|
}).post("/hello", [asyncFileStreamer](auto *res, auto *req) {
|
||||||
|
|
||||||
// depending on the file type we want to also add mime!
|
// depending on the file type we want to also add mime!
|
||||||
//asyncFileStreamer->streamFile(res, req->getUrl());
|
//asyncFileStreamer->streamFile(res, req->getUrl());
|
||||||
|
|
||||||
res->end("POST /hello");
|
res->end("POST /hello");
|
||||||
|
|
||||||
}).get("/hello", [](auto *res, auto *req/*, auto ¶ms*/) {
|
}).get("/hello", [](auto *res, auto *req) {
|
||||||
|
|
||||||
res->end("GET /hello");
|
res->end("GET /hello");
|
||||||
|
|
||||||
}).unhandled([](auto *res, auto *req/*, auto ¶ms*/) {
|
}).unhandled([](auto *res, auto *req) {
|
||||||
|
|
||||||
res->end("Here's nothing for you to see!");
|
res->end("Here's nothing for you to see!");
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -215,9 +215,10 @@ public:
|
|||||||
void onGet(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
void onGet(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|
||||||
httpContextData->router.add("get", pattern, [handler](typename HttpContextData<SSL>::UserData *user, auto &args) {
|
httpContextData->router.add("get", pattern, [handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
||||||
|
|
||||||
// todo: attach params to the req here!
|
// todo: attach params to the req here!
|
||||||
|
user->httpRequest->setParameters(params);
|
||||||
|
|
||||||
handler(user->httpResponse, user->httpRequest);
|
handler(user->httpResponse, user->httpRequest);
|
||||||
});
|
});
|
||||||
@@ -226,7 +227,7 @@ public:
|
|||||||
void onPost(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
void onPost(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|
||||||
httpContextData->router.add("post", pattern, [handler](typename HttpContextData<SSL>::UserData *user, auto &args) {
|
httpContextData->router.add("post", pattern, [handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
||||||
handler(user->httpResponse, user->httpRequest);
|
handler(user->httpResponse, user->httpRequest);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -234,7 +235,7 @@ public:
|
|||||||
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|
||||||
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::UserData *user, auto &args) {
|
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::UserData *user, std::pair<int, std::string_view *> params) {
|
||||||
handler(user->httpResponse, user->httpRequest);
|
handler(user->httpResponse, user->httpRequest);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ private:
|
|||||||
} headers[MAX_HEADERS];
|
} headers[MAX_HEADERS];
|
||||||
int querySeparator;
|
int querySeparator;
|
||||||
|
|
||||||
|
std::pair<int, std::string_view *> currentParameters;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string_view getHeader(std::string_view header) {
|
std::string_view getHeader(std::string_view header) {
|
||||||
for (Header *h = headers; (++h)->key.length(); ) {
|
for (Header *h = headers; (++h)->key.length(); ) {
|
||||||
@@ -47,6 +49,18 @@ public:
|
|||||||
return std::string_view(headers->value.data() + querySeparator, headers->value.length() - querySeparator);
|
return std::string_view(headers->value.data() + querySeparator, headers->value.length() - querySeparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setParameters(std::pair<int, std::string_view *> parameters) {
|
||||||
|
currentParameters = parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view getParameter(int index) {
|
||||||
|
if (currentParameters.first < index) {
|
||||||
|
return {};
|
||||||
|
} else {
|
||||||
|
return currentParameters.second[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class HttpParser {
|
class HttpParser {
|
||||||
|
|||||||
+4
-12
@@ -40,17 +40,9 @@ private:
|
|||||||
/* Same here, we cannot pop outside */
|
/* Same here, we cannot pop outside */
|
||||||
paramsTop--;
|
paramsTop--;
|
||||||
}
|
}
|
||||||
public:
|
|
||||||
std::string_view operator[](unsigned int index) {
|
|
||||||
if ((int) index <= paramsTop) {
|
|
||||||
return params[index];
|
|
||||||
} else {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} routeParameters;
|
} routeParameters;
|
||||||
|
|
||||||
std::vector<std::function<void(USERDATA, RouteParameters &)>> handlers;
|
std::vector<std::function<void(USERDATA, std::pair<int, std::string_view *>)>> handlers;
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
std::string name;
|
std::string name;
|
||||||
@@ -179,7 +171,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Captures all unhandled routes */
|
/* Captures all unhandled routes */
|
||||||
HttpRouter *unhandled(std::function<void(USERDATA, RouteParameters &)> handler) {
|
HttpRouter *unhandled(std::function<void(USERDATA, std::pair<int, std::string_view *> params)> handler) {
|
||||||
if (handlers.size()) {
|
if (handlers.size()) {
|
||||||
handlers[0] = handler;
|
handlers[0] = handler;
|
||||||
} else {
|
} else {
|
||||||
@@ -189,7 +181,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Register a route to be routed */
|
/* Register a route to be routed */
|
||||||
HttpRouter *add(std::string method, std::string_view pattern, std::function<void(USERDATA, RouteParameters &)> handler) {
|
HttpRouter *add(std::string method, std::string_view pattern, std::function<void(USERDATA, std::pair<int, std::string_view *>)> handler) {
|
||||||
/* Step over any initial slash */
|
/* Step over any initial slash */
|
||||||
if (pattern[0] == '/') {
|
if (pattern[0] == '/') {
|
||||||
pattern = pattern.substr(1);
|
pattern = pattern.substr(1);
|
||||||
@@ -248,7 +240,7 @@ public:
|
|||||||
|
|
||||||
/* Route the method and url pair. Calls registered callback or unhandled handler */
|
/* Route the method and url pair. Calls registered callback or unhandled handler */
|
||||||
void route(std::string_view method, std::string_view url, USERDATA userData) {
|
void route(std::string_view method, std::string_view url, USERDATA userData) {
|
||||||
handlers[lookupNew(method, url)](userData, routeParameters);
|
handlers[lookupNew(method, url)](userData, {routeParameters.paramsTop, routeParameters.params});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user