Hook up req->getParameter

This commit is contained in:
Alex Hultman
2018-10-05 00:10:43 +02:00
parent be0eb69597
commit 5e5d0de0e9
4 changed files with 29 additions and 21 deletions
+7 -6
View File
@@ -16,24 +16,25 @@ int main(int argc, char **argv) {
res->end("GET /WILDCARD");
})*/.get("/:param1/:param2", [](auto *res, auto *req/*, auto &params*/) {
})*/.get("/:param1/:param2", [](auto *res, auto *req) {
// todo: read from params (req->getParameter(0))
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 &params*/) {
}).post("/hello", [asyncFileStreamer](auto *res, auto *req) {
// depending on the file type we want to also add mime!
//asyncFileStreamer->streamFile(res, req->getUrl());
res->end("POST /hello");
}).get("/hello", [](auto *res, auto *req/*, auto &params*/) {
}).get("/hello", [](auto *res, auto *req) {
res->end("GET /hello");
}).unhandled([](auto *res, auto *req/*, auto &params*/) {
}).unhandled([](auto *res, auto *req) {
res->end("Here's nothing for you to see!");
+4 -3
View File
@@ -215,9 +215,10 @@ public:
void onGet(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
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!
user->httpRequest->setParameters(params);
handler(user->httpResponse, user->httpRequest);
});
@@ -226,7 +227,7 @@ public:
void onPost(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
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);
});
}
@@ -234,7 +235,7 @@ public:
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
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);
});
}
+14
View File
@@ -20,6 +20,8 @@ private:
} headers[MAX_HEADERS];
int querySeparator;
std::pair<int, std::string_view *> currentParameters;
public:
std::string_view getHeader(std::string_view header) {
for (Header *h = headers; (++h)->key.length(); ) {
@@ -47,6 +49,18 @@ public:
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 {
+4 -12
View File
@@ -40,17 +40,9 @@ private:
/* Same here, we cannot pop outside */
paramsTop--;
}
public:
std::string_view operator[](unsigned int index) {
if ((int) index <= paramsTop) {
return params[index];
} else {
return {};
}
}
} routeParameters;
std::vector<std::function<void(USERDATA, RouteParameters &)>> handlers;
std::vector<std::function<void(USERDATA, std::pair<int, std::string_view *>)>> handlers;
struct Node {
std::string name;
@@ -179,7 +171,7 @@ public:
}
/* 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()) {
handlers[0] = handler;
} else {
@@ -189,7 +181,7 @@ public:
}
/* 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 */
if (pattern[0] == '/') {
pattern = pattern.substr(1);
@@ -248,7 +240,7 @@ public:
/* Route the method and url pair. Calls registered callback or unhandled handler */
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});
}
};