Fix up router, add .post route
This commit is contained in:
+12
-2
@@ -12,10 +12,20 @@ int main(int argc, char **argv) {
|
|||||||
.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
|
.cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem",
|
||||||
.dh_params_file_name = "/home/alexhultman/dhparams.pem",
|
.dh_params_file_name = "/home/alexhultman/dhparams.pem",
|
||||||
.passphrase = "1234"
|
.passphrase = "1234"
|
||||||
}*/).get("/hello", [asyncFileStreamer](auto *res, auto *req) {
|
}*/).get("/*", [](auto *res, auto *req) {
|
||||||
|
|
||||||
|
res->end("GET /WILDCARD");
|
||||||
|
|
||||||
|
}).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");
|
||||||
|
|
||||||
|
}).get("/hello", [](auto *res, auto *req) {
|
||||||
|
|
||||||
|
res->end("GET /hello");
|
||||||
|
|
||||||
}).unhandled([](auto *res, auto *req) {
|
}).unhandled([](auto *res, auto *req) {
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,11 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TemplatedApp &post(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
|
httpContext->onPost(pattern, handler);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
TemplatedApp &unhandled(std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
TemplatedApp &unhandled(std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||||
httpContext->onUnhandled(handler);
|
httpContext->onUnhandled(handler);
|
||||||
return *this;
|
return *this;
|
||||||
|
|||||||
+9
-1
@@ -111,7 +111,7 @@ private:
|
|||||||
typename uWS::HttpContextData<SSL>::UserData userData = {
|
typename uWS::HttpContextData<SSL>::UserData userData = {
|
||||||
(HttpResponse<SSL> *) s, httpRequest
|
(HttpResponse<SSL> *) s, httpRequest
|
||||||
};
|
};
|
||||||
httpContextData->router.route("get", 3, httpRequest->getUrl().data(), httpRequest->getUrl().length(), &userData);
|
httpContextData->router.route(httpRequest->getMethod().data(), httpRequest->getMethod().length(), httpRequest->getUrl().data(), httpRequest->getUrl().length(), &userData);
|
||||||
|
|
||||||
// here we can be closed and in shutdown?
|
// here we can be closed and in shutdown?
|
||||||
|
|
||||||
@@ -220,6 +220,14 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void onPost(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||||
|
HttpContextData<SSL> *httpContextData = getSocketContextData();
|
||||||
|
|
||||||
|
httpContextData->router.add("post", pattern.c_str(), [handler](typename HttpContextData<SSL>::UserData *user, auto *args) {
|
||||||
|
handler(user->httpResponse, user->httpRequest);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ public:
|
|||||||
return std::string_view(headers->value.data(), querySeparator);
|
return std::string_view(headers->value.data(), querySeparator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string_view getMethod() {
|
||||||
|
return std::string_view(headers->key.data(), headers->key.length());
|
||||||
|
}
|
||||||
|
|
||||||
std::string_view getQuery() {
|
std::string_view getQuery() {
|
||||||
return std::string_view(headers->value.data() + querySeparator, headers->value.length() - querySeparator);
|
return std::string_view(headers->value.data() + querySeparator, headers->value.length() - querySeparator);
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-16
@@ -24,14 +24,13 @@ private:
|
|||||||
std::string name;
|
std::string name;
|
||||||
std::map<std::string, Node *> children;
|
std::map<std::string, Node *> children;
|
||||||
short handler;
|
short handler;
|
||||||
};
|
} tree;
|
||||||
|
|
||||||
Node *tree = new Node({"GET", {}, -1});
|
|
||||||
std::string compiled_tree;
|
std::string compiled_tree;
|
||||||
|
|
||||||
void add(std::vector<std::string> route, short handler) {
|
void add(std::vector<std::string> route, short handler) {
|
||||||
//std::cout << "add" << std::endl;
|
//std::cout << "add" << std::endl;
|
||||||
Node *parent = tree;
|
Node *parent = &tree;
|
||||||
for (std::string node : route) {
|
for (std::string node : route) {
|
||||||
//std::cout << "Node: <" << node << ">" << std::endl;
|
//std::cout << "Node: <" << node << ">" << std::endl;
|
||||||
|
|
||||||
@@ -42,6 +41,7 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serialize tree
|
||||||
unsigned short compile_tree(Node *n) {
|
unsigned short compile_tree(Node *n) {
|
||||||
unsigned short nodeLength = 6 + n->name.length();
|
unsigned short nodeLength = 6 + n->name.length();
|
||||||
for (auto c : n->children) {
|
for (auto c : n->children) {
|
||||||
@@ -99,18 +99,24 @@ private:
|
|||||||
return stop ? stop : end;
|
return stop ? stop : end;
|
||||||
}
|
}
|
||||||
|
|
||||||
// should take method also!
|
inline int lookup(const char *method, int method_length, const char *url, int length) {
|
||||||
inline int lookup(const char *url, int length) {
|
|
||||||
// all urls start with /
|
// all urls start with /
|
||||||
url++;
|
url++;
|
||||||
length--;
|
length--;
|
||||||
|
|
||||||
const char *treeStart = (char *) compiled_tree.data();
|
const char *treeStart = (char *) compiled_tree.data();
|
||||||
|
|
||||||
bool foundWildcard = false;
|
bool foundWildcard = false;
|
||||||
|
|
||||||
|
// step1: lookup this method (we lookup treeStart)
|
||||||
|
treeStart = find_node(treeStart, method, method_length, &foundWildcard);
|
||||||
|
if (treeStart == 0) {
|
||||||
|
//std::cout << "We do not even have this method!" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
const char *stop, *start = url, *end_ptr = url + length;
|
const char *stop, *start = url, *end_ptr = url + length;
|
||||||
do {
|
do {
|
||||||
|
// start and stop are pointers in the URL we are getting, end_ptr is the end of url
|
||||||
stop = getNextSegment(start, end_ptr);
|
stop = getNextSegment(start, end_ptr);
|
||||||
|
|
||||||
//std::cout << "Matching(" << std::string(start, stop - start) << ")" << std::endl;
|
//std::cout << "Matching(" << std::string(start, stop - start) << ")" << std::endl;
|
||||||
@@ -148,22 +154,17 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> nodes;
|
std::vector<std::string> nodes;
|
||||||
//nodes.push_back(method);
|
nodes.push_back(method);
|
||||||
|
|
||||||
const char *stop, *start = pattern, *end_ptr = pattern + strlen(pattern);
|
const char *stop, *start = pattern, *end_ptr = pattern + strlen(pattern);
|
||||||
do {
|
do {
|
||||||
stop = getNextSegment(start, end_ptr);
|
stop = getNextSegment(start, end_ptr);
|
||||||
|
|
||||||
//std::cout << "Segment(" << std::string(start, stop - start) << ")" << std::endl;
|
//std::cout << "Segment(" << std::string(start, stop - start) << ")" << std::endl;
|
||||||
|
|
||||||
nodes.push_back(std::string(start, stop - start));
|
nodes.push_back(std::string(start, stop - start));
|
||||||
|
|
||||||
start = stop + 1;
|
start = stop + 1;
|
||||||
} while (stop != end_ptr);
|
} while (stop != end_ptr);
|
||||||
|
|
||||||
|
// add this path to the tree
|
||||||
// if pattern starts with / then move 1+ and run inline slash parser
|
|
||||||
|
|
||||||
add(nodes, handlers.size());
|
add(nodes, handlers.size());
|
||||||
handlers.push_back(handler);
|
handlers.push_back(handler);
|
||||||
|
|
||||||
@@ -173,19 +174,20 @@ public:
|
|||||||
|
|
||||||
void compile() {
|
void compile() {
|
||||||
compiled_tree.clear();
|
compiled_tree.clear();
|
||||||
compile_tree(tree);
|
compile_tree(&tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
void route(const char *method, unsigned int method_length, const char *url, unsigned int url_length, USERDATA userData) {
|
void route(const char *method, unsigned int method_length, const char *url, unsigned int url_length, USERDATA userData) {
|
||||||
|
|
||||||
int index = lookup(url, url_length);
|
// todo: simplify so that unhandled is 0!
|
||||||
|
int index = lookup(method, method_length, url, url_length);
|
||||||
if (index != -1) {
|
if (index != -1) {
|
||||||
handlers[index](userData, ¶ms);
|
handlers[index](userData, ¶ms);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Did not find route for URL: " << std::string_view(url, url_length) << std::endl;
|
|
||||||
unhandledHandler(userData, ¶ms);
|
unhandledHandler(userData, ¶ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// will this counter the reserve?
|
||||||
params.clear();
|
params.clear();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user