Hook up req->getParameter
This commit is contained in:
+4
-3
@@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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
@@ -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});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user