diff options
| author | Rutger Broekhoff | 2026-08-28 21:12:55 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 21:12:55 +0200 |
| commit | a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac (patch) | |
| tree | b4e0ec57c3799e9f649c6bfe27cb6c3313b97364 /server/src/http_common.cppm | |
| parent | 973aec43ea54bbf95b64fbcb636403401d1ca60e (diff) | |
| download | routemon-a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac.tar.gz routemon-a5d95afb96eb9a3b65d82f5f84bf8e4a4fb4c8ac.zip | |
clang-format C++ sources
Diffstat (limited to 'server/src/http_common.cppm')
| -rw-r--r-- | server/src/http_common.cppm | 228 |
1 files changed, 129 insertions, 99 deletions
diff --git a/server/src/http_common.cppm b/server/src/http_common.cppm index 3c38da7..606a5d5 100644 --- a/server/src/http_common.cppm +++ b/server/src/http_common.cppm | |||
| @@ -10,130 +10,160 @@ export namespace bhttp = beast::http; | |||
| 10 | 10 | ||
| 11 | namespace boost::beast { | 11 | namespace boost::beast { |
| 12 | 12 | ||
| 13 | namespace concepts { | 13 | namespace concepts { |
| 14 | 14 | ||
| 15 | template<class T> | 15 | template <class T> |
| 16 | concept buffers_generator = beast::is_buffers_generator<T>::value; | 16 | concept buffers_generator = beast::is_buffers_generator<T>::value; |
| 17 | 17 | ||
| 18 | template<class T> | 18 | template <class T> |
| 19 | concept const_buffer_sequence = beast::is_const_buffer_sequence<T>::value; | 19 | concept const_buffer_sequence = beast::is_const_buffer_sequence<T>::value; |
| 20 | 20 | ||
| 21 | } // namespace concepts | 21 | } // namespace concepts |
| 22 | 22 | ||
| 23 | namespace http::concepts { | 23 | namespace http::concepts { |
| 24 | 24 | ||
| 25 | template<class T> | 25 | template <class T> |
| 26 | concept fields = is_fields<T>::value; | 26 | concept fields = is_fields<T>::value; |
| 27 | 27 | ||
| 28 | template<class T> | 28 | template <class T> |
| 29 | concept body = is_body<T>::value; | 29 | concept body = is_body<T>::value; |
| 30 | 30 | ||
| 31 | template<class T> | 31 | template <class T> |
| 32 | concept body_reader = is_body_reader<T>::value; | 32 | concept body_reader = is_body_reader<T>::value; |
| 33 | 33 | ||
| 34 | } // namespace http::concepts | 34 | } // namespace http::concepts |
| 35 | 35 | ||
| 36 | } // namespace boost::beast | 36 | } // namespace boost::beast |
| 37 | 37 | ||
| 38 | namespace routemon::http { | 38 | namespace routemon::http { |
| 39 | 39 | ||
| 40 | struct supported_verb { | 40 | struct supported_verb |
| 41 | enum supported_verb_t : std::uint8_t { | 41 | { |
| 42 | options, | 42 | enum supported_verb_t : std::uint8_t |
| 43 | delete_, | 43 | { |
| 44 | get, | 44 | options, |
| 45 | head, | 45 | delete_, |
| 46 | post, | 46 | get, |
| 47 | put, | 47 | head, |
| 48 | }; | 48 | post, |
| 49 | put, | ||
| 50 | }; | ||
| 49 | 51 | ||
| 50 | supported_verb_t value; | 52 | supported_verb_t value; |
| 51 | 53 | ||
| 52 | supported_verb(supported_verb_t value) : value{value} {} | 54 | supported_verb(supported_verb_t value) : value{value} {} |
| 53 | 55 | ||
| 54 | static auto from(bhttp::verb v) -> std::optional<supported_verb> { | 56 | static auto from(bhttp::verb v) -> std::optional<supported_verb> |
| 55 | switch (v) { | 57 | { |
| 56 | case bhttp::verb::options: return supported_verb::options; | 58 | switch (v) |
| 57 | case bhttp::verb::delete_: return supported_verb::delete_; | 59 | { |
| 58 | case bhttp::verb::get: return supported_verb::get; | 60 | case bhttp::verb::options: |
| 59 | case bhttp::verb::head: return supported_verb::head; | 61 | return supported_verb::options; |
| 60 | case bhttp::verb::post: return supported_verb::post; | 62 | case bhttp::verb::delete_: |
| 61 | case bhttp::verb::put: return supported_verb::put; | 63 | return supported_verb::delete_; |
| 62 | default: return std::nullopt; | 64 | case bhttp::verb::get: |
| 63 | } | 65 | return supported_verb::get; |
| 66 | case bhttp::verb::head: | ||
| 67 | return supported_verb::head; | ||
| 68 | case bhttp::verb::post: | ||
| 69 | return supported_verb::post; | ||
| 70 | case bhttp::verb::put: | ||
| 71 | return supported_verb::put; | ||
| 72 | default: | ||
| 73 | return std::nullopt; | ||
| 64 | } | 74 | } |
| 75 | } | ||
| 65 | 76 | ||
| 66 | operator bhttp::verb() const { | 77 | operator bhttp::verb() const |
| 67 | switch (value) { | 78 | { |
| 68 | case supported_verb::options: return bhttp::verb::options; | 79 | switch (value) |
| 69 | case supported_verb::delete_: return bhttp::verb::delete_; | 80 | { |
| 70 | case supported_verb::get: return bhttp::verb::get; | 81 | case supported_verb::options: |
| 71 | case supported_verb::head: return bhttp::verb::head; | 82 | return bhttp::verb::options; |
| 72 | case supported_verb::post: return bhttp::verb::post; | 83 | case supported_verb::delete_: |
| 73 | case supported_verb::put: return bhttp::verb::put; | 84 | return bhttp::verb::delete_; |
| 74 | } | 85 | case supported_verb::get: |
| 86 | return bhttp::verb::get; | ||
| 87 | case supported_verb::head: | ||
| 88 | return bhttp::verb::head; | ||
| 89 | case supported_verb::post: | ||
| 90 | return bhttp::verb::post; | ||
| 91 | case supported_verb::put: | ||
| 92 | return bhttp::verb::put; | ||
| 75 | } | 93 | } |
| 76 | }; | 94 | } |
| 95 | }; | ||
| 77 | 96 | ||
| 78 | export struct verb_set { | 97 | export struct verb_set |
| 79 | bool delete_ : 1 = false; | 98 | { |
| 80 | bool get : 1 = false; | 99 | bool delete_ : 1 = false; |
| 81 | bool head : 1 = false; | 100 | bool get : 1 = false; |
| 82 | bool post : 1 = false; | 101 | bool head : 1 = false; |
| 83 | bool put : 1 = false; | 102 | bool post : 1 = false; |
| 84 | bool options : 1 = false; | 103 | bool put : 1 = false; |
| 104 | bool options : 1 = false; | ||
| 85 | 105 | ||
| 86 | auto enable(supported_verb v) -> void { | 106 | auto enable(supported_verb v) -> void |
| 87 | switch (v.value) { | 107 | { |
| 88 | case supported_verb::delete_: | 108 | switch (v.value) |
| 89 | delete_ = true; | 109 | { |
| 90 | break; | 110 | case supported_verb::delete_: |
| 91 | case supported_verb::get: | 111 | delete_ = true; |
| 92 | get = true; | 112 | break; |
| 93 | break; | 113 | case supported_verb::get: |
| 94 | case supported_verb::head: | 114 | get = true; |
| 95 | head = true; | 115 | break; |
| 96 | break; | 116 | case supported_verb::head: |
| 97 | case supported_verb::post: | 117 | head = true; |
| 98 | post = true; | 118 | break; |
| 99 | break; | 119 | case supported_verb::post: |
| 100 | case supported_verb::put: | 120 | post = true; |
| 101 | put = true; | 121 | break; |
| 102 | break; | 122 | case supported_verb::put: |
| 103 | case supported_verb::options: | 123 | put = true; |
| 104 | options = true; | 124 | break; |
| 105 | break; | 125 | case supported_verb::options: |
| 106 | default:; | 126 | options = true; |
| 107 | } | 127 | break; |
| 128 | default:; | ||
| 108 | } | 129 | } |
| 130 | } | ||
| 109 | 131 | ||
| 110 | auto operator==(verb_set const& rhs) const noexcept -> bool = default; | 132 | auto operator==(verb_set const& rhs) const noexcept -> bool = default; |
| 111 | 133 | ||
| 112 | auto empty() const -> bool { | 134 | auto empty() const -> bool { return *this == verb_set{}; } |
| 113 | return *this == verb_set{}; | ||
| 114 | } | ||
| 115 | 135 | ||
| 116 | verb_set(std::initializer_list<supported_verb> vs) { | 136 | verb_set(std::initializer_list<supported_verb> vs) |
| 117 | for (auto const v : vs) enable(v); | 137 | { |
| 118 | } | 138 | for (auto const v : vs) |
| 139 | enable(v); | ||
| 140 | } | ||
| 119 | 141 | ||
| 120 | auto to_string() const -> std::string { | 142 | auto to_string() const -> std::string |
| 121 | std::ostringstream ss; | 143 | { |
| 122 | bool wrote = false; | 144 | std::ostringstream ss; |
| 123 | auto write = [&](bhttp::verb v) { | 145 | bool wrote = false; |
| 124 | if (wrote) | 146 | auto write = [&](bhttp::verb v) |
| 125 | ss << ", "; | 147 | { |
| 126 | ss << v; | 148 | if (wrote) |
| 127 | wrote = true; | 149 | ss << ", "; |
| 128 | }; | 150 | ss << v; |
| 129 | if (delete_) write(bhttp::verb::delete_); | 151 | wrote = true; |
| 130 | if (get) write(bhttp::verb::get); | 152 | }; |
| 131 | if (head) write(bhttp::verb::head); | 153 | if (delete_) |
| 132 | if (post) write(bhttp::verb::post); | 154 | write(bhttp::verb::delete_); |
| 133 | if (put) write(bhttp::verb::put); | 155 | if (get) |
| 134 | if (options) write(bhttp::verb::options); | 156 | write(bhttp::verb::get); |
| 135 | return ss.str(); | 157 | if (head) |
| 136 | } | 158 | write(bhttp::verb::head); |
| 137 | }; | 159 | if (post) |
| 160 | write(bhttp::verb::post); | ||
| 161 | if (put) | ||
| 162 | write(bhttp::verb::put); | ||
| 163 | if (options) | ||
| 164 | write(bhttp::verb::options); | ||
| 165 | return ss.str(); | ||
| 166 | } | ||
| 167 | }; | ||
| 138 | 168 | ||
| 139 | } // namespace routemon::http | 169 | } // namespace routemon::http |