| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b32c53d commit 0d81342
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | #pragma once | |
| 2 | 2 | ||
| 3 | - #if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location) | ||
| 3 | + #if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location) | ||
| 4 | 4 | ||
| 5 | 5 | #include <experimental/source_location> | |
| 6 | 6 | #define EXPRESSCPP_CURRENT_SOURCE_LOCATION \ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,8 +14,7 @@ namespace expresscpp { | |||
| 14 | 14 | class Layer { | |
| 15 | 15 | public: | |
| 16 | 16 | Layer(const std::string_view path); | |
| 17 | - Layer(const std::string_view path, PathToRegExpOptions options, std::string_view parent_path, | ||
| 18 | - handler_wn_t handler); | ||
| 17 | + Layer(const std::string_view path, PathToRegExpOptions options, std::string_view parent_path, handler_wn_t handler); | ||
| 19 | 18 | ||
| 20 | 19 | void SetParentPath(const std::string_view parent_path); | |
| 21 | 20 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,15 +7,20 @@ | |||
| 7 | 7 | ||
| 8 | 8 | namespace expresscpp { | |
| 9 | 9 | ||
| 10 | + class Router; | ||
| 11 | + class Request; | ||
| 12 | + class Response; | ||
| 13 | + | ||
| 10 | 14 | class NextRouter { | |
| 11 | 15 | public: | |
| 16 | + NextRouter() = default; | ||
| 17 | + NextRouter(Router* router, std::shared_ptr<Request> req, std::shared_ptr<Response> res); | ||
| 12 | 18 | void operator()(std::shared_ptr<std::string> error = nullptr); | |
| 13 | 19 | ||
| 14 | - void setCallback(std::function<void(std::shared_ptr<std::string>)> cb); | ||
| 15 | - | ||
| 16 | 20 | private: | |
| 17 | - std::function<void(std::shared_ptr<std::string>)> cb_; | ||
| 18 | - bool callback_registered_{false}; | ||
| 21 | + std::shared_ptr<Request> req_; | ||
| 22 | + std::shared_ptr<Response> res_; | ||
| 23 | + Router* router_{nullptr}; | ||
| 19 | 24 | }; | |
| 20 | 25 | ||
| 21 | 26 | typedef NextRouter next_t; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,8 @@ class Router { | |||
| 72 | 72 | ||
| 73 | 73 | std::vector<std::shared_ptr<Layer>> stack() const; | |
| 74 | 74 | ||
| 75 | + void Next(std::shared_ptr<Request> req, std::shared_ptr<Response> res, std::shared_ptr<std::string> err = nullptr); | ||
| 76 | + | ||
| 75 | 77 | private: | |
| 76 | 78 | void Init(); | |
| 77 | 79 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,8 +11,7 @@ namespace expresscpp { | |||
| 11 | 11 | ||
| 12 | 12 | typedef std::function<void(request_t req, response_t res)> handler_t; | |
| 13 | 13 | typedef std::function<void(request_t req, response_t res, next_t next)> handler_wn_t; | |
| 14 | - typedef std::function<void(std::error_code ec, request_t req, response_t res, next_t next)> | ||
| 15 | - express_handler_wecn_t; | ||
| 14 | + typedef std::function<void(std::error_code ec, request_t req, response_t res, next_t next)> express_handler_wecn_t; | ||
| 16 | 15 | typedef std::function<void(const std::error_code)> ready_fn_cb_error_code_t; | |
| 17 | 16 | ||
| 18 | 17 | typedef std::function<void()> ready_fn_cb_void_t; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | #include "expresscpp/nextrouter.hpp" | |
| 2 | 2 | ||
| 3 | 3 | #include "expresscpp/console.hpp" | |
| 4 | + #include "expresscpp/router.hpp" | ||
| 4 | 5 | ||
| 5 | 6 | namespace expresscpp { | |
| 6 | 7 | ||
@@ -9,16 +10,15 @@ void NextRouter::operator()(std::shared_ptr<std::string> error) { | |||
| 9 | 10 | if (error != nullptr) { | |
| 10 | 11 | Console::Error(*error); | |
| 11 | 12 | } | |
| 12 | - if(!callback_registered_){ | ||
| 13 | + if (!router_) { | ||
| 13 | 14 | Console::Trace("no callback for next handler registered"); | |
| 14 | 15 | return; | |
| 15 | 16 | } | |
| 16 | - cb_(error); | ||
| 17 | + router_->Next(req_, res_, error); | ||
| 17 | 18 | } | |
| 18 | 19 | ||
| 19 | - void NextRouter::setCallback(std::function<void(std::shared_ptr<std::string>)> cb) { | ||
| 20 | - cb_ = cb; | ||
| 21 | - callback_registered_ = true; | ||
| 20 | + NextRouter::NextRouter(Router* router, std::shared_ptr<Request> req, std::shared_ptr<Response> res) | ||
| 21 | + : req_{std::move(req)}, res_{std::move(res)}, router_{router} { | ||
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | 24 | } // namespace expresscpp | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,9 +1,10 @@ | |||
| 1 | 1 | #include "expresscpp/router.hpp" | |
| 2 | 2 | ||
| 3 | + #include <functional> | ||
| 3 | 4 | #include <memory> | |
| 4 | 5 | ||
| 5 | - #include "boost/uuid/uuid_generators.hpp" | ||
| 6 | 6 | #include "boost/algorithm/string.hpp" | |
| 7 | + #include "boost/uuid/uuid_generators.hpp" | ||
| 7 | 8 | #include "boost/uuid/uuid_io.hpp" | |
| 8 | 9 | #include "expresscpp/console.hpp" | |
| 9 | 10 | #include "expresscpp/impl/matcher.hpp" | |
@@ -86,64 +87,56 @@ void Router::Use(std::string_view path, std::shared_ptr<Router> router) { | |||
| 86 | 87 | stack_.emplace_back(layer); | |
| 87 | 88 | } | |
| 88 | 89 | ||
| 90 | + void Router::Next(std::shared_ptr<Request> req, std::shared_ptr<Response> res, std::shared_ptr<std::string> err) { | ||
| 91 | + if (err != nullptr) { | ||
| 92 | + Console::Error(fmt::format("next error: {}", *err)); | ||
| 93 | + return; | ||
| 94 | + } | ||
| 95 | + while (req->match != true && req->idx < stack_.size()) { | ||
| 96 | + req->current_layer = stack_[req->idx++]; | ||
| 97 | + req->match = matchLayer(req->current_layer, req->getPath()); | ||
| 98 | + | ||
| 99 | + req->current_route = req->current_layer->getRoute(); | ||
| 100 | + | ||
| 101 | + // no match | ||
| 102 | + if (req->match == false) { | ||
| 103 | + Console::Debug(fmt::format("no match for path \"{}\", layer \"{}\"", req->getPath(), req->current_layer->path_)); | ||
| 104 | + continue; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + if (!req->current_route) { | ||
| 108 | + // process non-route handlers normally | ||
| 109 | + break; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + const auto method = req->getMethod(); | ||
| 113 | + const auto has_method = req->current_route->handles_method(method); | ||
| 114 | + | ||
| 115 | + // don't even bother matching route | ||
| 116 | + if (!has_method && method != HttpMethod::Head) { | ||
| 117 | + req->match = false; | ||
| 118 | + continue; | ||
| 119 | + } | ||
| 120 | + req->SetParams(req->current_layer->params_); | ||
| 121 | + req->SetQueryParams(req->current_layer->query_params_); | ||
| 122 | + req->SetQueryString(req->current_layer->query_string_); | ||
| 123 | + } | ||
| 124 | + } | ||
| 125 | + | ||
| 89 | 126 | void Router::HandleRequest(std::shared_ptr<Request> req, std::shared_ptr<Response> res) { | |
| 90 | 127 | assert(req != nullptr); | |
| 91 | 128 | assert(res != nullptr); | |
| 92 | 129 | ||
| 93 | 130 | Console::Debug(fmt::format("dispatching request path: \"{}\", method \"{}\"", req->getPath(), | |
| 94 | 131 | getHttpMethodName(req->getMethod()))); | |
| 95 | 132 | ||
| 96 | - auto protohost = ""s; | ||
| 97 | - auto removed = ""s; | ||
| 98 | - | ||
| 99 | 133 | // find next matching layer | |
| 100 | 134 | auto layerError = ""s; | |
| 101 | - auto stack = stack_; | ||
| 102 | 135 | req->idx = 0; | |
| 103 | - | ||
| 104 | - auto next = [req, res, stack](std::shared_ptr<std::string> err = nullptr) { | ||
| 105 | - if (err != nullptr) { | ||
| 106 | - Console::Error(fmt::format("next error: {}", *err)); | ||
| 107 | - return; | ||
| 108 | - } | ||
| 109 | - while (req->match != true && req->idx < stack.size()) { | ||
| 110 | - req->current_layer = stack[req->idx++]; | ||
| 111 | - req->match = matchLayer(req->current_layer, req->getPath()); | ||
| 112 | - | ||
| 113 | - req->current_route = req->current_layer->getRoute(); | ||
| 114 | - | ||
| 115 | - // no match | ||
| 116 | - if (req->match == false) { | ||
| 117 | - Console::Debug( | ||
| 118 | - fmt::format("no match for path \"{}\", layer \"{}\"", req->getPath(), req->current_layer->path_)); | ||
| 119 | - continue; | ||
| 120 | - } | ||
| 121 | - | ||
| 122 | - if (!req->current_route) { | ||
| 123 | - // process non-route handlers normally | ||
| 124 | - break; | ||
| 125 | - } | ||
| 126 | - | ||
| 127 | - const auto method = req->getMethod(); | ||
| 128 | - const auto has_method = req->current_route->handles_method(method); | ||
| 129 | - | ||
| 130 | - // don't even bother matching route | ||
| 131 | - if (!has_method && method != HttpMethod::Head) { | ||
| 132 | - req->match = false; | ||
| 133 | - continue; | ||
| 134 | - } | ||
| 135 | - req->SetParams(req->current_layer->params_); | ||
| 136 | - req->SetQueryParams(req->current_layer->query_params_); | ||
| 137 | - req->SetQueryString(req->current_layer->query_string_); | ||
| 138 | - } | ||
| 139 | - }; | ||
| 140 | - | ||
| 141 | - next(); | ||
| 142 | - NextRouter next_handler; | ||
| 136 | + Next(req, res); | ||
| 137 | + NextRouter next_handler(this, req, res); | ||
| 143 | 138 | while (req->match == true) { | |
| 144 | 139 | req->match = false; | |
| 145 | - | ||
| 146 | - next_handler.setCallback(next); | ||
| 147 | 140 | req->current_layer->HandleRequest(req, res, next_handler); | |
| 148 | 141 | // will be reset to true in the next() function if there is another matching layer | |
| 149 | 142 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,7 +47,7 @@ TEST(StaticFileMiddleware, ServeIndexHtml) { | |||
| 47 | 47 | assert(std::filesystem::exists(path_to_index_html)); | |
| 48 | 48 | } | |
| 49 | 49 | ||
| 50 | - // create other doc files such as json | ||
| 50 | + // create other doc files such as json | ||
| 51 | 51 | { | |
| 52 | 52 | std::filesystem::path path_to_doc = doc_root + "/doc.json"; | |
| 53 | 53 | std::ofstream index_doc_file(path_to_doc); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments