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