diff options
Diffstat (limited to 'ext/yahttp/yahttp/router.hpp')
-rw-r--r-- | ext/yahttp/yahttp/router.hpp | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/ext/yahttp/yahttp/router.hpp b/ext/yahttp/yahttp/router.hpp index 205119c..3cb13ed 100644 --- a/ext/yahttp/yahttp/router.hpp +++ b/ext/yahttp/yahttp/router.hpp @@ -25,9 +25,16 @@ namespace funcptr = boost; #include <utility> namespace YaHTTP { + enum RoutingResult { + RouteFound = 1, + RouteNotFound = 0, + RouteNoMethod = -1, + }; + typedef funcptr::function <void(Request* req, Response* resp)> THandlerFunction; //!< Handler function pointer typedef funcptr::tuple<std::string, std::string, THandlerFunction, std::string> TRoute; //!< Route tuple (method, urlmask, handler, name) typedef std::vector<TRoute> TRouteList; //!< List of routes in order of evaluation + typedef funcptr::tuple<int,int> TDelim; /*! Implements simple router. @@ -44,26 +51,28 @@ is consumed but not stored. Note that only path is matched, scheme, host and url static Router router; //<! Singleton instance of Router public: void map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name); //<! Instance method for mapping urls - bool route(Request *req, THandlerFunction& handler); //<! Instance method for performing routing + RoutingResult route(Request *req, THandlerFunction& handler); //<! Instance method for performing routing void printRoutes(std::ostream &os); //<! Instance method for printing routes std::pair<std::string, std::string> urlFor(const std::string &name, const strstr_map_t& arguments); //<! Instance method for generating paths + static bool match(const std::string& route, const URL& requrl, std::map<std::string, TDelim>& params); //<! Instance method for matching a route /*! Map an URL. -If method is left empty, it will match any method. Name is also optional, but needed if you want to find it for making URLs +If method is left empty, it will match any method. Name is also optional, but needed if you want to find it for making URLs */ - static void Map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map(method, url, handler, name); }; - static void Get(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("GET", url, handler, name); }; //<! Helper for mapping GET - static void Post(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("POST", url, handler, name); }; //<! Helper for mapping POST - static void Put(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PUT", url, handler, name); }; //<! Helper for mapping PUT - static void Patch(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PATCH", url, handler, name); }; //<! Helper for mapping PATCH - static void Delete(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("DELETE", url, handler, name); }; //<! Helper for mapping DELETE - static void Any(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("", url, handler, name); }; //<! Helper for mapping any method + static void Map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map(method, url, std::move(handler), name); }; + static void Get(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("GET", url, std::move(handler), name); }; //<! Helper for mapping GET + static void Post(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("POST", url, std::move(handler), name); }; //<! Helper for mapping POST + static void Put(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PUT", url, std::move(handler), name); }; //<! Helper for mapping PUT + static void Patch(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PATCH", url, std::move(handler), name); }; //<! Helper for mapping PATCH + static void Delete(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("DELETE", url, std::move(handler), name); }; //<! Helper for mapping DELETE + static void Any(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("", url, std::move(handler), name); }; //<! Helper for mapping any method - static bool Route(Request *req, THandlerFunction& handler) { return router.route(req, handler); }; //<! Performs routing based on req->url.path + static bool Match(const std::string& route, const URL& requrl, std::map<std::string, TDelim>& params) { return router.match(route, requrl, params); }; + static RoutingResult Route(Request *req, THandlerFunction& handler) { return router.route(req, handler); }; //<! Performs routing based on req->url.path, returns RouteFound if route is found and method matches, RouteNoMethod if route is seen but method did match, and RouteNotFound if not found. static void PrintRoutes(std::ostream &os) { router.printRoutes(os); }; //<! Prints all known routes to given output stream static std::pair<std::string, std::string> URLFor(const std::string &name, const strstr_map_t& arguments) { return router.urlFor(name,arguments); }; //<! Generates url from named route and arguments. Missing arguments are assumed empty - static const TRouteList& GetRoutes() { return router.routes; } //<! Reference to route list + static const TRouteList& GetRoutes() { return router.routes; } //<! Reference to route list static void Clear() { router.routes.clear(); } //<! Clear all routes TRouteList routes; //<! Instance variable for routes |