ゴミ箱
router.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <regex>
4 #include <string>
5 #include <vector>
6 #include <utility>
7 #include <functional>
8 #include <unordered_set>
9 #include <unordered_map>
10 #include <optional>
11 
13 #include <boost/asio/ip/tcp.hpp>
14 
15 #include "beast_common.hpp"
16 
17 namespace http {
21  enum class Method {
38  };
39 
43  class Router {
44  public:
45  typedef std::unordered_map<std::string, std::string> matches;
46 
52  struct Context {
54  const boost::asio::ip::tcp::socket& socket;
60  const matches matches;
61 
62  //Since we store references here better to not let it get copied around
63  //Of course nothing prevents user from storing particular reference somewhere,
64  //but it is not my problem. I did all I could.
65  Context(const Context& that) = delete;
66  Context(Context&& other) noexcept = delete;
67  };
69  typedef std::function<void(Context&&)> router_handler;
70 
74  class Route {
75  public:
88  Route(const char* path, router_handler fn);
97  std::optional<matches> match(const char* uri) const;
103  std::optional<matches> match(boost::beast::string_view uri) const;
104 
106  router_handler callback;
107 
108  private:
110  std::regex path;
112  std::vector<std::string> keys;
114  const char* component_re = ":([^\\/]+)?";
116  const char* replace_re = "(?:([^\\/]+?))";
117 
118  };
119 
121  Router() noexcept;
122 
131  Router& add_route(Method method, const char* route, router_handler fn);
132 
142  bool dispatch(const boost::asio::ip::tcp::socket& socket, const dynamic_request& request, dynamic_response& response) const noexcept;
143 
147  bool is_method_used(boost::beast::http::verb method) const noexcept;
148 
149  private:
156  std::unordered_set<int> avail_methods;
157 
159  typedef std::vector<Route> handler_map_t;
161  handler_map_t get_handlers;
163  handler_map_t head_handlers;
165  handler_map_t post_handlers;
167  handler_map_t put_handlers;
169  handler_map_t delete_handlers;
171  handler_map_t options_handlers;
173  handler_map_t trace_handlers;
174 
176  handler_map_t* map_method(boost::beast::http::verb method) noexcept;
178  const handler_map_t* map_method(boost::beast::http::verb method) const noexcept;
179  };
180 }
dynamic_response & response
boost::beast response into which handler writes stuff
Definition: router.hpp:58
Definition: router.hpp:52
Definition: async_result.hpp:20
Definition: type_traits.hpp:25
The DELETE method deletes the specified resource.
STL namespace.
verb
Definition: verb.hpp:26
Definition: beast_common.hpp:6
Method
Definition: router.hpp:21
const boost::asio::ip::tcp::socket & socket
asio&#39;s socket can give access to useful information such as local/remote endpoint.
Definition: router.hpp:54
HTTP OPTIONS.
const dynamic_request & request
boost::beast request
Definition: router.hpp:56
Definition: router.hpp:43
HTTP CONNECT.
std::function< void(Context &&)> router_handler
Type of route&#39;s handler.
Definition: router.hpp:69
const matches matches
regex matches
Definition: router.hpp:60
boost::string_ref string_view
The type of string view used by the library.
Definition: string.hpp:36
router_handler callback
Route&#39;s handler.
Definition: router.hpp:106
boost::asio::ip::tcp tcp
Definition: router.cpp:6
std::unordered_map< std::string, std::string > matches
Definition: router.hpp:45
Definition: router.hpp:74