summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext
diff options
context:
space:
mode:
authorRutger Broekhoff2026-11-30 21:27:55 +0100
committerRutger Broekhoff2026-11-30 21:27:55 +0100
commit7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch)
treea9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/http/ext
parent02475e05ccd1aa15c6a6163834f28bfebbef796f (diff)
downloadroutemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz
routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip
Cleanups and updates
Diffstat (limited to 'server/src/http/ext')
-rw-r--r--server/src/http/ext/gpx.cpp139
-rw-r--r--server/src/http/ext/gpx.cppm128
-rw-r--r--server/src/http/ext/hmac_openssl.cppm173
-rw-r--r--server/src/http/ext/json.cpp34
-rw-r--r--server/src/http/ext/json.cppm65
-rw-r--r--server/src/http/ext/raw_deflate.cppm134
6 files changed, 671 insertions, 2 deletions
diff --git a/server/src/http/ext/gpx.cpp b/server/src/http/ext/gpx.cpp
new file mode 100644
index 0000000..c1f565f
--- /dev/null
+++ b/server/src/http/ext/gpx.cpp
@@ -0,0 +1,139 @@
1module;
2
3#include <boost/beast/core.hpp>
4
5module routemon:http.ext.gpx$impl;
6
7import :http.ext.gpx;
8
9namespace chrono = std::chrono;
10
11namespace routemon::http::ext::gpx {
12
13auto parse_error_category_impl::name() const noexcept -> char const*
14{
15 return "gpx_parse";
16}
17
18auto parse_error_category_impl::message(int condition) const noexcept
19 -> std::string
20{
21 std::ignore = condition;
22 return "failed to parse GPX file";
23}
24
25auto parse_error_category() noexcept -> parse_error_category_impl const&
26{
27 static auto const inst = parse_error_category_impl{};
28 return inst;
29}
30
31auto parse_error() noexcept -> std::error_code
32{
33 return std::error_code{1, parse_error_category()};
34}
35
36auto parse_result::set_exception(std::exception_ptr ex) noexcept -> void
37{
38 res_ = ex;
39}
40
41auto parse_result::set_gpx_file(routemon::gpx::file&& f) noexcept -> void
42{
43 res_ = std::move(f);
44}
45
46auto parse_result::init_parse_duration(
47 chrono::steady_clock::duration dur) noexcept -> void
48{
49 init_parse_dur_ = dur;
50}
51
52[[nodiscard]] auto parse_result::init_parse_duration() const noexcept
53 -> chrono::steady_clock::duration
54{
55 return init_parse_dur_;
56}
57
58auto parse_result::put_parse_duration(
59 chrono::steady_clock::duration dur) noexcept -> void
60{
61 put_parse_dur_ = dur;
62}
63
64[[nodiscard]] auto parse_result::put_parse_duration() const noexcept
65 -> chrono::steady_clock::duration
66{
67 return put_parse_dur_;
68}
69
70auto parse_result::finish_parse_duration(
71 chrono::steady_clock::duration dur) noexcept -> void
72{
73 finish_parse_dur_ = dur;
74}
75
76[[nodiscard]] auto parse_result::finish_parse_duration() const noexcept
77 -> chrono::steady_clock::duration
78{
79 return finish_parse_dur_;
80}
81
82auto parse_result::unwrap() -> routemon::gpx::file&&
83{
84 return std::visit(
85 util::overloaded{
86 [](std::exception_ptr ex) -> routemon::gpx::file&&
87 {
88 if (ex)
89 std::rethrow_exception(ex);
90 else
91 throw std::runtime_error{"no GPX file parse result available"};
92 },
93 [](routemon::gpx::file&& f) -> routemon::gpx::file&&
94 { return std::move(f); },
95 },
96 std::move(res_));
97}
98
99auto body::reader::init(
100 boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
101 -> void
102{
103 auto const init_start = chrono::steady_clock::now();
104 try
105 {
106 r_.init();
107 ec = {};
108 }
109 catch (std::exception& ex)
110 {
111 res_->set_exception(std::current_exception());
112 ec = parse_error();
113 }
114 res_->init_parse_duration(chrono::steady_clock::now() - init_start);
115}
116
117auto body::reader::finish(beast::error_code& ec) noexcept -> void
118{
119 auto const finish_start = chrono::steady_clock::now();
120 try
121 {
122 if (buf_.size() > 0)
123 {
124 r_.put(std::string_view{buf_});
125 buf_.clear();
126 }
127
128 res_->set_gpx_file(r_.finish());
129 ec = {};
130 }
131 catch (std::exception& ex)
132 {
133 res_->set_exception(std::current_exception());
134 ec = parse_error();
135 }
136 res_->finish_parse_duration(chrono::steady_clock::now() - finish_start);
137}
138
139} // namespace routemon::http::ext::gpx
diff --git a/server/src/http/ext/gpx.cppm b/server/src/http/ext/gpx.cppm
new file mode 100644
index 0000000..69763d8
--- /dev/null
+++ b/server/src/http/ext/gpx.cppm
@@ -0,0 +1,128 @@
1module;
2
3#include <boost/beast/core.hpp>
4#include <boost/beast/http.hpp>
5
6export module routemon:http.ext.gpx;
7
8import std;
9import :gpx;
10import :http.core.common;
11
12namespace chrono = std::chrono;
13
14namespace routemon::http::ext::gpx {
15
16class parse_error_category_impl : public std::error_category
17{
18public:
19 auto name() const noexcept -> char const* override;
20 auto message(int condition) const noexcept -> std::string override;
21};
22
23auto parse_error_category() noexcept -> parse_error_category_impl const&;
24
25auto parse_error() noexcept -> std::error_code;
26
27class parse_result
28{
29 chrono::steady_clock::duration init_parse_dur_;
30 chrono::steady_clock::duration put_parse_dur_;
31 chrono::steady_clock::duration finish_parse_dur_;
32 std::variant<std::exception_ptr, routemon::gpx::file> res_;
33
34public:
35 auto set_exception(std::exception_ptr ex) noexcept -> void;
36 auto set_gpx_file(routemon::gpx::file&& f) noexcept -> void;
37
38 auto init_parse_duration(chrono::steady_clock::duration dur) noexcept -> void;
39 [[nodiscard]] auto init_parse_duration() const noexcept
40 -> chrono::steady_clock::duration;
41 auto put_parse_duration(chrono::steady_clock::duration dur) noexcept -> void;
42 [[nodiscard]] auto put_parse_duration() const noexcept
43 -> chrono::steady_clock::duration;
44 auto finish_parse_duration(chrono::steady_clock::duration dur) noexcept
45 -> void;
46 [[nodiscard]] auto finish_parse_duration() const noexcept
47 -> chrono::steady_clock::duration;
48
49 auto unwrap() -> routemon::gpx::file&&;
50};
51
52struct body
53{
54 using value_type = parse_result;
55
56 class reader
57 {
58 routemon::gpx::reader r_;
59 util::not_null<value_type*> res_;
60 std::inplace_vector<char, 4096> buf_;
61
62 public:
63 template <bool isRequest, bhttp::concepts::fields Fields>
64 explicit reader(bhttp::header<isRequest, Fields>&, value_type& v) : res_{&v}
65 {
66 res_->init_parse_duration({});
67 res_->put_parse_duration({});
68 res_->finish_parse_duration({});
69 }
70
71 // The following methods (which are called by Beast) are marked
72 // noexcept, since Beast does not ensure that exceptions thrown
73 // here are appropriately directed to the caller of
74 // (async_)read(_some), so throwing here might cause the program
75 // to crash.
76
77 auto
78 init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
79 -> void;
80
81 auto
82 put(beast::concepts::const_buffer_sequence auto b,
83 beast::error_code& ec) noexcept -> std::size_t
84 {
85 auto const put_start = chrono::steady_clock::now();
86 auto total = 0uz;
87 try
88 {
89 for (auto cur_in_buf : beast::buffers_range(b))
90 {
91 while (cur_in_buf.size() > 0)
92 {
93 auto to_read =
94 std::min(cur_in_buf.size(), buf_.max_size() - buf_.size());
95 buf_.append_range(
96 std::span{
97 static_cast<char const*>(cur_in_buf.data()), to_read
98 });
99 cur_in_buf += to_read;
100 total += to_read;
101
102 if (buf_.size() == buf_.max_size())
103 {
104 r_.put(std::string_view{buf_});
105 buf_.clear();
106 }
107 }
108 }
109 ec = {};
110 }
111 catch (std::exception& ex)
112 {
113 res_->set_exception(std::current_exception());
114 ec = parse_error();
115 }
116 res_->put_parse_duration(
117 res_->put_parse_duration()
118 + (chrono::steady_clock::now() - put_start));
119 return total;
120 }
121
122 auto finish(beast::error_code& ec) noexcept -> void;
123 };
124};
125static_assert(bhttp::concepts::body<body>);
126static_assert(bhttp::concepts::body_reader<body>);
127
128} // namespace routemon::http::ext::gpx
diff --git a/server/src/http/ext/hmac_openssl.cppm b/server/src/http/ext/hmac_openssl.cppm
index 52ccb86..b196094 100644
--- a/server/src/http/ext/hmac_openssl.cppm
+++ b/server/src/http/ext/hmac_openssl.cppm
@@ -1,10 +1,179 @@
1module; 1module;
2 2
3#include <boost/asio/ssl/error.hpp>
4#include <boost/beast/core.hpp>
5#include <boost/beast/http.hpp>
6
3#include <openssl/evp.h> 7#include <openssl/evp.h>
4#include <openssl/params.h> 8#include <openssl/params.h>
5 9
6export module routemon:hmac_openssl; 10export module routemon:http.ext.hmac_openssl;
11
12import std;
13import :http.core.common;
14import :util;
15
16namespace {
7 17
8namespace routemon::hmac_openssl { 18namespace net = boost::asio;
9 19
10} 20}
21
22namespace routemon::http::ext::hmac_openssl {
23
24// TODO: constrain T? What are appropriate constraints?
25template <typename T>
26class readable
27{
28 std::string_view key_;
29 std::optional<std::array<std::uint8_t, 32>> mac_;
30 T res_;
31
32 template <bhttp::concepts::body_reader Sub>
33 friend struct body_reader;
34
35public:
36 template <typename... Args>
37 explicit readable(std::string_view key, Args&&... args)
38 : key_{key}, res_{std::forward<Args>(args)...}
39 {
40 }
41
42 template <typename Self>
43 constexpr auto get(this Self&& self) -> auto&&
44 {
45 return std::forward<Self>(self).res_;
46 }
47
48 auto mac() const -> std::optional<std::array<std::uint8_t, 32>>
49 {
50 return mac_;
51 }
52};
53
54template <bhttp::concepts::body_reader Sub>
55class body_reader
56{
57 EVP_MAC* mac_ = nullptr;
58 EVP_MAC_CTX* ctx_ = nullptr;
59
60 util::not_null<readable<typename Sub::value_type>*> v_;
61 Sub::reader subr_;
62 beast::error_code ec_;
63
64public:
65 template <bool isRequest, bhttp::concepts::fields Fields>
66 explicit body_reader(
67 bhttp::header<isRequest, Fields>& h,
68 readable<typename Sub::value_type>& v)
69 : v_{util::not_null{&v}},
70 subr_{h, v.get()} // we are not supposed to use v yet, but this just
71 // computes an address, so it should be fine
72 {
73 }
74
75 ~body_reader()
76 {
77 ::EVP_MAC_CTX_free(ctx_);
78 ::EVP_MAC_free(mac_);
79 }
80
81 // TODO: move to impl. unit.
82 auto init(boost::optional<std::uint64_t> n, beast::error_code& ec) noexcept
83 -> void
84 {
85 // https://docs.openssl.org/master/man3/EVP_MAC/#examples
86 mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr);
87 if (!mac_)
88 {
89 ec_ = ec = make_error_code(std::errc::not_enough_memory);
90 return;
91 }
92
93 ctx_ = ::EVP_MAC_CTX_new(mac_);
94 if (!ctx_)
95 {
96 ec_ = ec = make_error_code(std::errc::not_enough_memory);
97 return;
98 }
99
100 char sha256[] = "SHA256";
101 ::OSSL_PARAM const params[] = {
102 OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1),
103 OSSL_PARAM_END
104 };
105 if (!::EVP_MAC_init(
106 ctx_, reinterpret_cast<unsigned char const*>(v_->key_.data()),
107 v_->key_.size(), params))
108 {
109 ec_ = ec = beast::error_code{
110 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
111 };
112 return;
113 }
114
115 subr_.init(n, ec);
116 }
117
118 auto
119 put(beast::concepts::const_buffer_sequence auto b,
120 beast::error_code& ec) noexcept -> std::size_t
121 {
122 if ((ec = ec_))
123 return 0uz;
124
125 auto const total_read = subr_.put(b, ec);
126 if (total_read > 0)
127 {
128 auto remaining = total_read;
129 for (auto const buf : beast::buffers_range(b))
130 {
131 auto const to_hash_from_this_buf = std::min(remaining, buf.size());
132 if (!::EVP_MAC_update(
133 ctx_, static_cast<unsigned char const*>(buf.data()),
134 to_hash_from_this_buf))
135 {
136 ec_ = ec = beast::error_code{
137 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
138 };
139 return total_read;
140 }
141 remaining -= to_hash_from_this_buf;
142 }
143 }
144 return total_read;
145 }
146
147 auto finish(beast::error_code& ec) noexcept -> void
148 {
149 auto got_mac_len = 0uz;
150 auto got_mac = std::array<std::uint8_t, 32>{};
151 if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size()))
152 {
153 ec = beast::error_code{
154 static_cast<int>(::ERR_get_error()), net::error::get_ssl_category()
155 };
156 return;
157 }
158 assert(got_mac_len == got_mac.size());
159 v_->mac_ = got_mac;
160 subr_.finish(ec);
161 }
162};
163
164template <bhttp::concepts::body Sub>
165struct body
166{
167 using value_type = readable<typename Sub::value_type>;
168};
169
170template <bhttp::concepts::body Sub>
171 requires bhttp::concepts::body_reader<Sub>
172struct body<Sub>
173{
174 using value_type = readable<typename Sub::value_type>;
175
176 using reader = body_reader<Sub>;
177};
178
179} // namespace routemon::http::ext::hmac_openssl
diff --git a/server/src/http/ext/json.cpp b/server/src/http/ext/json.cpp
new file mode 100644
index 0000000..f03d336
--- /dev/null
+++ b/server/src/http/ext/json.cpp
@@ -0,0 +1,34 @@
1module;
2
3#include <boost/beast/core.hpp>
4#include <boost/json/stream_parser.hpp>
5
6module routemon:http.ext.json$impl;
7
8import :http.ext.json;
9
10namespace routemon::http::ext::json {
11
12namespace {
13
14namespace json = boost::json;
15
16} // namespace
17
18auto body::reader::init(
19 boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
20 -> void
21{
22 p_.reset();
23 ec = {};
24}
25
26auto body::reader::finish(beast::error_code& ec) noexcept -> void
27{
28 p_.finish(ec);
29 if (ec)
30 return;
31 *v_ = p_.release();
32}
33
34} // namespace routemon::http::ext::json
diff --git a/server/src/http/ext/json.cppm b/server/src/http/ext/json.cppm
index e69de29..4097253 100644
--- a/server/src/http/ext/json.cppm
+++ b/server/src/http/ext/json.cppm
@@ -0,0 +1,65 @@
1module;
2
3#include <boost/beast/core.hpp>
4#include <boost/beast/http.hpp>
5#include <boost/json/stream_parser.hpp>
6
7export module routemon:http.ext.json;
8
9import std;
10import :http.core.common;
11import :util;
12
13namespace routemon::http::ext::json {
14
15namespace {
16
17namespace json = boost::json;
18
19} // namespace
20
21struct body
22{
23 using value_type = json::value;
24
25 class reader
26 {
27 json::stream_parser p_;
28 util::not_null<value_type*> v_;
29
30 public:
31 template <bool isRequest, bhttp::concepts::fields Fields>
32 explicit reader(bhttp::header<isRequest, Fields>&, value_type& v)
33 : v_{util::not_null{&v}}
34 {
35 }
36
37 auto
38 init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
39 -> void;
40
41 auto
42 put(beast::concepts::const_buffer_sequence auto b,
43 beast::error_code& ec) noexcept -> std::size_t
44 {
45 ec = {};
46
47 auto wrote = 0uz;
48 for (auto const buf : beast::buffers_range(b))
49 {
50 p_.write(static_cast<char const*>(buf.data()), buf.size(), ec);
51 wrote += buf.size();
52 if (ec)
53 break;
54 }
55 return wrote;
56 }
57
58 auto finish(beast::error_code& ec) noexcept -> void;
59 };
60};
61
62static_assert(bhttp::concepts::body<body>);
63static_assert(bhttp::concepts::body_reader<body>);
64
65} // namespace routemon::http::ext::json
diff --git a/server/src/http/ext/raw_deflate.cppm b/server/src/http/ext/raw_deflate.cppm
new file mode 100644
index 0000000..ee7da2f
--- /dev/null
+++ b/server/src/http/ext/raw_deflate.cppm
@@ -0,0 +1,134 @@
1module;
2
3#include <boost/asio/buffer.hpp>
4#include <boost/beast/core.hpp>
5#include <boost/beast/http.hpp>
6#include <boost/beast/zlib.hpp>
7
8export module routemon:http.ext.raw_deflate;
9
10import :http.core.common;
11
12namespace routemon::http::ext::raw_deflate {
13
14namespace {
15
16namespace net = boost::asio;
17
18}
19
20template <bhttp::concepts::body_reader Inner>
21struct body
22{
23 using value_type = Inner::value_type;
24
25 class reader
26 {
27 static constexpr auto const chunk_size = 16384;
28
29 Inner::reader r_;
30 bool stream_ended_ = false;
31 beast::zlib::z_params zs_ = {};
32 beast::zlib::inflate_stream inflate_strm_ = {};
33 std::unique_ptr<std::array<char, chunk_size>> in_buf_ = nullptr;
34 std::unique_ptr<std::array<char, chunk_size>> out_buf_ = nullptr;
35
36 auto inflate_chunk(beast::error_code& ec) -> void
37 {
38 assert(zs_.avail_in > 0);
39 zs_.next_in = in_buf_->data();
40
41 do
42 {
43 zs_.avail_out = out_buf_->size();
44 zs_.next_out = out_buf_->data();
45
46 inflate_strm_.write(zs_, beast::zlib::Flush::none, ec);
47 assert(ec != beast::zlib::error::stream_error);
48 if (ec == beast::zlib::error::need_buffers)
49 ec = {};
50 if (ec == beast::zlib::error::end_of_stream)
51 {
52 ec = {};
53 stream_ended_ = true;
54 }
55 if (ec)
56 break;
57
58 auto have = chunk_size - zs_.avail_out;
59 auto cbuf =
60 net::buffer(static_cast<void const*>(out_buf_->data()), have);
61
62 auto inner_ec = beast::error_code{};
63 do
64 cbuf += r_.put(cbuf, inner_ec);
65 while (cbuf.size() > 0 && !inner_ec);
66 if (inner_ec)
67 {
68 ec = inner_ec;
69 break;
70 }
71 }
72 while (zs_.avail_out == 0);
73
74 // In the last case (stream_ended_), the user has sent too much data
75 assert(zs_.avail_in == 0 || ec || stream_ended_);
76 }
77
78 public:
79 template <bool isRequest, bhttp::concepts::fields Fields>
80 explicit reader(bhttp::header<isRequest, Fields>& hdr, value_type& v)
81 : r_{hdr, v}
82 {
83 }
84
85 auto
86 init(boost::optional<std::uint64_t> /* n */, beast::error_code& ec) noexcept
87 -> void
88 {
89 in_buf_.reset(new std::array<char, chunk_size>{});
90 out_buf_.reset(new std::array<char, chunk_size>{});
91 ec = {};
92 }
93
94 auto
95 put(beast::concepts::const_buffer_sequence auto b,
96 beast::error_code& ec) noexcept -> std::size_t
97 {
98 auto in_buf = net::buffer(in_buf_->data(), in_buf_->size());
99 in_buf += zs_.avail_in;
100
101 auto consumed_from_b = net::buffer_copy(in_buf, b);
102 zs_.avail_in += consumed_from_b;
103
104 if (zs_.avail_in == in_buf_->size())
105 inflate_chunk(ec);
106 else
107 ec = {};
108
109 return consumed_from_b;
110 }
111
112 auto finish(beast::error_code& ec) noexcept -> void
113 {
114 ec = {};
115 if (zs_.avail_in > 0)
116 inflate_chunk(ec);
117 if (!ec && !stream_ended_)
118 ec = beast::zlib::error::stream_error;
119 if (stream_ended_ && zs_.avail_in > 0)
120 // client sent data past deflate stream end
121 ec = beast::zlib::error::stream_error;
122
123 in_buf_.reset();
124 out_buf_.reset();
125 inflate_strm_.reset();
126 if (!ec)
127 {
128 r_.finish(ec);
129 }
130 }
131 };
132};
133
134} // namespace routemon::http::ext::raw_deflate