From 5e5d0de0e9a100298c4f5a1865cb5a8c4324e33b Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Fri, 5 Oct 2018 00:10:43 +0200 Subject: [PATCH] Hook up req->getParameter --- misc/main.cpp | 13 +++++++------ src/HttpContext.h | 7 ++++--- src/HttpParser.h | 14 ++++++++++++++ src/HttpRouter.h | 16 ++++------------ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/misc/main.cpp b/misc/main.cpp index 316912b..0c734f0 100644 --- a/misc/main.cpp +++ b/misc/main.cpp @@ -16,24 +16,25 @@ int main(int argc, char **argv) { 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->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! //asyncFileStreamer->streamFile(res, req->getUrl()); res->end("POST /hello"); - }).get("/hello", [](auto *res, auto *req/*, auto ¶ms*/) { + }).get("/hello", [](auto *res, auto *req) { 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!"); diff --git a/src/HttpContext.h b/src/HttpContext.h index 7761f0f..92c3be1 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -215,9 +215,10 @@ public: void onGet(std::string pattern, std::function *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); - httpContextData->router.add("get", pattern, [handler](typename HttpContextData::UserData *user, auto &args) { + httpContextData->router.add("get", pattern, [handler](typename HttpContextData::UserData *user, std::pair 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 *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); - httpContextData->router.add("post", pattern, [handler](typename HttpContextData::UserData *user, auto &args) { + httpContextData->router.add("post", pattern, [handler](typename HttpContextData::UserData *user, std::pair params) { handler(user->httpResponse, user->httpRequest); }); } @@ -234,7 +235,7 @@ public: void onUnhandled(std::function *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); - httpContextData->router.unhandled([handler](typename HttpContextData::UserData *user, auto &args) { + httpContextData->router.unhandled([handler](typename HttpContextData::UserData *user, std::pair params) { handler(user->httpResponse, user->httpRequest); }); } diff --git a/src/HttpParser.h b/src/HttpParser.h index e6c5385..27651a6 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -20,6 +20,8 @@ private: } headers[MAX_HEADERS]; int querySeparator; + std::pair 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 parameters) { + currentParameters = parameters; + } + + std::string_view getParameter(int index) { + if (currentParameters.first < index) { + return {}; + } else { + return currentParameters.second[index]; + } + } + }; class HttpParser { diff --git a/src/HttpRouter.h b/src/HttpRouter.h index 1d3d956..afb2f00 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -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> handlers; + std::vector)>> handlers; struct Node { std::string name; @@ -179,7 +171,7 @@ public: } /* Captures all unhandled routes */ - HttpRouter *unhandled(std::function handler) { + HttpRouter *unhandled(std::function 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 handler) { + HttpRouter *add(std::string method, std::string_view pattern, std::function)> 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}); } };