|
2 | 2 | #include "http_error.h" |
3 | 3 | #include "request_data.h" |
4 | 4 | namespace HTTP { |
5 | | -Trie::Node &Trie::Node::Move(char c) { |
6 | | - if (c == '*') { |
7 | | - any = true; |
8 | | - return *this; |
9 | | - } |
10 | | - if (!children.contains(c)) { |
11 | | - children[c] = std::make_unique<Node>(); |
12 | | - } |
13 | | - return *children[c]; |
| 5 | + |
| 6 | +size_t Trie::StringHash::operator()(std::string_view value) const noexcept { |
| 7 | + return std::hash<std::string_view>{}(value); |
14 | 8 | } |
15 | | -const Trie::Node &Trie::Node::Move(char c) const { |
16 | | - try { |
17 | | - return *children.at(c); |
18 | | - } catch (...) { |
19 | | - throw HTTPError(404, "Not found"); |
| 9 | + |
| 10 | +bool Trie::StringEqual::operator()(std::string_view lhs, |
| 11 | + std::string_view rhs) const noexcept { |
| 12 | + return lhs == rhs; |
| 13 | +} |
| 14 | + |
| 15 | +Trie::Node &Trie::Node::Move(std::string_view segment) { |
| 16 | + if (segment == "*") { |
| 17 | + if (!wildcard) { |
| 18 | + wildcard = std::make_unique<Node>(); |
| 19 | + } |
| 20 | + return *wildcard; |
| 21 | + } |
| 22 | + |
| 23 | + auto child = children.find(segment); |
| 24 | + if (child == children.end()) { |
| 25 | + auto [inserted, _] = |
| 26 | + children.emplace(std::string{segment}, std::make_unique<Node>()); |
| 27 | + child = inserted; |
20 | 28 | } |
| 29 | + return *child->second; |
21 | 30 | } |
| 31 | + |
22 | 32 | void Trie::AddRequest(Method method, RespondType respond, |
23 | 33 | std::string_view path) { |
24 | 34 | Node *current = root_.get(); |
25 | | - for (auto c : path) { |
26 | | - current = ¤t->Move(c); |
| 35 | + size_t position = path.starts_with('/') ? 1 : 0; |
| 36 | + while (position < path.size()) { |
| 37 | + const size_t next = path.find('/', position); |
| 38 | + const auto segment = |
| 39 | + next == std::string_view::npos |
| 40 | + ? path.substr(position) |
| 41 | + : path.substr(position, next - position); |
| 42 | + current = ¤t->Move(segment); |
| 43 | + if (next == std::string_view::npos) { |
| 44 | + break; |
| 45 | + } |
| 46 | + position = next + 1; |
27 | 47 | } |
28 | 48 | current->handlers[method] = respond; |
29 | 49 | } |
30 | | -const Trie::Node &Trie::GetRoot() { return *root_; } |
| 50 | + |
| 51 | +RespondType Trie::Match(Method method, std::string_view path, |
| 52 | + std::vector<std::string> &urlVariables) const { |
| 53 | + const Node *current = root_.get(); |
| 54 | + urlVariables.clear(); |
| 55 | + |
| 56 | + size_t position = path.starts_with('/') ? 1 : 0; |
| 57 | + while (position < path.size()) { |
| 58 | + const size_t next = path.find('/', position); |
| 59 | + const auto segment = |
| 60 | + next == std::string_view::npos |
| 61 | + ? path.substr(position) |
| 62 | + : path.substr(position, next - position); |
| 63 | + |
| 64 | + const auto child = current->children.find(segment); |
| 65 | + if (child != current->children.end()) { |
| 66 | + current = child->second.get(); |
| 67 | + } else if (current->wildcard) { |
| 68 | + urlVariables.emplace_back(segment); |
| 69 | + current = current->wildcard.get(); |
| 70 | + } else { |
| 71 | + throw HTTPError(404, "Not found"); |
| 72 | + } |
| 73 | + |
| 74 | + if (next == std::string_view::npos) { |
| 75 | + break; |
| 76 | + } |
| 77 | + position = next + 1; |
| 78 | + } |
| 79 | + |
| 80 | + if (!current->handlers[method]) { |
| 81 | + throw HTTPError(404, "Not found"); |
| 82 | + } |
| 83 | + return *current->handlers[method]; |
| 84 | +} |
| 85 | + |
31 | 86 | Trie::Trie(Trie &&rhs) { root_ = std::move(rhs.root_); } |
32 | 87 | Trie &Trie::operator=(Trie &&rhs) { |
33 | 88 | root_ = std::move(rhs.root_); |
|
0 commit comments