Yet Another HTTP Library: /home/cmouse/projects/libyahttp/yahttp/router.hpp Source File

Yet Another HTTP Library yahttp

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
router.hpp
Go to the documentation of this file.
1 #ifndef _YAHTTP_ROUTER_HPP
2 #define _YAHTTP_ROUTER_HPP 1
3 /* @file
4 * @brief Defines router class and support structures
5 */
6 #ifdef HAVE_CXX11
7 #include <functional>
8 #include <tuple>
9 #define HAVE_CPP_FUNC_PTR
10 #define IGNORE std::ignore
11 namespace funcptr = std;
12 #else
13 #ifdef HAVE_BOOST
14 #include <boost/function.hpp>
15 #include <boost/tuple/tuple.hpp>
16 #define IGNORE boost::tuples::ignore
17 namespace funcptr = boost;
18 #define HAVE_CPP_FUNC_PTR
19 #else
20 #warn "You need to configure with boost or have C++11 capable compiler for router"
21 #endif
22 #endif
23
24 #ifdef HAVE_CPP_FUNC_PTR
25 #include <vector>
26 #include <utility>
27
28 namespace YaHTTP {
29 typedef funcptr::function <void(Request* req, Response* resp)> THandlerFunction;
30 typedef funcptr::tuple<std::string, std::string, THandlerFunction, std::string> TRoute;
31 typedef std::vector<TRoute> TRouteList;
32
42 class Router {
43 private:
44 Router() {};
45 static Router router; //<! Singleton instance of Router
46 public:
47 void map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name); //<! Instance method for mapping urls
48 bool route(Request *req, THandlerFunction& handler); //<! Instance method for performing routing
49 void printRoutes(std::ostream &os); //<! Instance method for printing routes
50 std::pair<std::string, std::string> urlFor(const std::string &name, const strstr_map_t& arguments); //<! Instance method for generating paths
51
55 static void Map(const std::string& method, const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map(method, url, handler, name); };
56 static void Get(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("GET", url, handler, name); }; //<! Helper for mapping GET
57 static void Post(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("POST", url, handler, name); }; //<! Helper for mapping POST
58 static void Put(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PUT", url, handler, name); }; //<! Helper for mapping PUT
59 static void Patch(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("PATCH", url, handler, name); }; //<! Helper for mapping PATCH
60 static void Delete(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("DELETE", url, handler, name); }; //<! Helper for mapping DELETE
61 static void Any(const std::string& url, THandlerFunction handler, const std::string& name = "") { router.map("", url, handler, name); }; //<! Helper for mapping any method
62
63 static bool Route(Request *req, THandlerFunction& handler) { return router.route(req, handler); }; //<! Performs routing based on req->url.path
64 static void PrintRoutes(std::ostream &os) { router.printRoutes(os); }; //<! Prints all known routes to given output stream
65
66 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
67 static const TRouteList& GetRoutes() { return router.routes; } //<! Reference to route list
68
69 TRouteList routes; //<! Instance variable for routes
70 };
71 };
72 #endif
73
74 #endif
YaHTTP::strstr_map_t
std::map< std::string, std::string, ASCIICINullSafeComparator > strstr_map_t
Definition: utility.hpp:22
YaHTTP::TRouteList
std::vector< TRoute > TRouteList
List of routes in order of evaluation.
Definition: router.hpp:31
YaHTTP::THandlerFunction
funcptr::function< void(Request *req, Response *resp)> THandlerFunction
Handler function pointer.
Definition: router.hpp:29
YaHTTP::Router::GetRoutes
static const TRouteList & GetRoutes()
Definition: router.hpp:67
YaHTTP::Router::PrintRoutes
static void PrintRoutes(std::ostream &os)
Definition: router.hpp:64
YaHTTP::Router::Patch
static void Patch(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:59
YaHTTP::Router::Map
static void Map(const std::string &method, const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:55
YaHTTP::Router::Get
static void Get(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:56
YaHTTP::Router::route
bool route(Request *req, THandlerFunction &handler)
Definition: router.cpp:27
YaHTTP::Router::Router
Router()
Definition: router.hpp:44
YaHTTP::Router
Definition: router.hpp:42
YaHTTP::Router::Any
static void Any(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:61
YaHTTP::Request
Definition: reqresp.hpp:215
YaHTTP::TRoute
funcptr::tuple< std::string, std::string, THandlerFunction, std::string > TRoute
Route tuple (method, urlmask, handler, name)
Definition: router.hpp:30
YaHTTP::Router::printRoutes
void printRoutes(std::ostream &os)
Definition: router.cpp:101
YaHTTP::Router::Post
static void Post(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:57
YaHTTP::Router::Delete
static void Delete(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:60
YaHTTP::Router::map
void map(const std::string &method, const std::string &url, THandlerFunction handler, const std::string &name)
Definition: router.cpp:13
YaHTTP::Router::Put
static void Put(const std::string &url, THandlerFunction handler, const std::string &name="")
Definition: router.hpp:58
YaHTTP::Router::routes
TRouteList routes
Definition: router.hpp:69
YaHTTP::Router::urlFor
std::pair< std::string, std::string > urlFor(const std::string &name, const strstr_map_t &arguments)
Definition: router.cpp:120
YaHTTP::Router::router
static Router router
Definition: router.hpp:44
YaHTTP::Router::URLFor
static std::pair< std::string, std::string > URLFor(const std::string &name, const strstr_map_t &arguments)
Definition: router.hpp:66
YaHTTP::Router::Route
static bool Route(Request *req, THandlerFunction &handler)
Definition: router.hpp:63