Clean up router
This commit is contained in:
+14
-18
@@ -24,9 +24,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <iostream>
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -37,12 +35,13 @@ namespace uWS {
|
|||||||
|
|
||||||
template <class USERDATA>
|
template <class USERDATA>
|
||||||
struct HttpRouter {
|
struct HttpRouter {
|
||||||
|
/* These are public for now */
|
||||||
USERDATA userData;
|
|
||||||
|
|
||||||
std::vector<std::string> methods = {"get", "post", "head", "put", "delete", "connect", "options", "trace", "patch"};
|
std::vector<std::string> methods = {"get", "post", "head", "put", "delete", "connect", "options", "trace", "patch"};
|
||||||
|
|
||||||
/* 32-bit */
|
private:
|
||||||
|
USERDATA userData;
|
||||||
|
|
||||||
|
/* 32-bit handler id */
|
||||||
const static uint32_t HANDLER_MASK = 0x0fffffff;
|
const static uint32_t HANDLER_MASK = 0x0fffffff;
|
||||||
const static uint32_t HIGH_PRIORITY = 0xd0000000;
|
const static uint32_t HIGH_PRIORITY = 0xd0000000;
|
||||||
const static uint32_t MEDIUM_PRIORITY = 0xe0000000;
|
const static uint32_t MEDIUM_PRIORITY = 0xe0000000;
|
||||||
@@ -50,19 +49,24 @@ struct HttpRouter {
|
|||||||
|
|
||||||
static const unsigned int MAX_URL_SEGMENTS = 100;
|
static const unsigned int MAX_URL_SEGMENTS = 100;
|
||||||
|
|
||||||
private:
|
|
||||||
/* Methods and their respective priority */
|
/* Methods and their respective priority */
|
||||||
std::map<std::string, int> priority;
|
std::map<std::string, int> priority;
|
||||||
|
|
||||||
|
/* List of handlers */
|
||||||
|
std::vector<fu2::unique_function<bool(HttpRouter *)>> handlers;
|
||||||
|
|
||||||
|
/* Current URL cache */
|
||||||
|
std::string_view currentUrl;
|
||||||
|
std::string_view urlSegmentVector[MAX_URL_SEGMENTS];
|
||||||
|
int urlSegmentTop;
|
||||||
|
|
||||||
|
/* The matching tree */
|
||||||
struct Node {
|
struct Node {
|
||||||
std::string name;
|
std::string name;
|
||||||
std::vector<std::unique_ptr<Node>> children;
|
std::vector<std::unique_ptr<Node>> children;
|
||||||
std::vector<uint32_t> handlers;
|
std::vector<uint32_t> handlers;
|
||||||
} root = {"rootNode"};
|
} root = {"rootNode"};
|
||||||
|
|
||||||
/* List of handlers */
|
|
||||||
std::vector<fu2::unique_function<bool(HttpRouter *)>> handlers;
|
|
||||||
|
|
||||||
/* Advance from parent to child, adding child if necessary */
|
/* Advance from parent to child, adding child if necessary */
|
||||||
Node *getNode(Node *parent, std::string child) {
|
Node *getNode(Node *parent, std::string child) {
|
||||||
for (std::unique_ptr<Node> &node : parent->children) {
|
for (std::unique_ptr<Node> &node : parent->children) {
|
||||||
@@ -100,10 +104,6 @@ private:
|
|||||||
}
|
}
|
||||||
} routeParameters;
|
} routeParameters;
|
||||||
|
|
||||||
std::string_view currentUrl;
|
|
||||||
std::string_view urlSegmentVector[MAX_URL_SEGMENTS];
|
|
||||||
int urlSegmentTop;
|
|
||||||
|
|
||||||
/* Set URL for router. Will reset any URL cache */
|
/* Set URL for router. Will reset any URL cache */
|
||||||
inline void setUrl(std::string_view url) {
|
inline void setUrl(std::string_view url) {
|
||||||
/* Remove / from input URL */
|
/* Remove / from input URL */
|
||||||
@@ -189,10 +189,6 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void print() {
|
|
||||||
printNode(&root, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<int, std::string_view *> getParameters() {
|
std::pair<int, std::string_view *> getParameters() {
|
||||||
return {routeParameters.paramsTop, routeParameters.params};
|
return {routeParameters.paramsTop, routeParameters.params};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user