From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/http/ext/gpx.cpp | 139 +++++++++++++++++++++++++++ server/src/http/ext/gpx.cppm | 128 +++++++++++++++++++++++++ server/src/http/ext/hmac_openssl.cppm | 173 +++++++++++++++++++++++++++++++++- server/src/http/ext/json.cpp | 34 +++++++ server/src/http/ext/json.cppm | 65 +++++++++++++ server/src/http/ext/raw_deflate.cppm | 134 ++++++++++++++++++++++++++ 6 files changed, 671 insertions(+), 2 deletions(-) create mode 100644 server/src/http/ext/gpx.cpp create mode 100644 server/src/http/ext/gpx.cppm create mode 100644 server/src/http/ext/json.cpp create mode 100644 server/src/http/ext/raw_deflate.cppm (limited to 'server/src/http/ext') 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 @@ +module; + +#include + +module routemon:http.ext.gpx$impl; + +import :http.ext.gpx; + +namespace chrono = std::chrono; + +namespace routemon::http::ext::gpx { + +auto parse_error_category_impl::name() const noexcept -> char const* +{ + return "gpx_parse"; +} + +auto parse_error_category_impl::message(int condition) const noexcept + -> std::string +{ + std::ignore = condition; + return "failed to parse GPX file"; +} + +auto parse_error_category() noexcept -> parse_error_category_impl const& +{ + static auto const inst = parse_error_category_impl{}; + return inst; +} + +auto parse_error() noexcept -> std::error_code +{ + return std::error_code{1, parse_error_category()}; +} + +auto parse_result::set_exception(std::exception_ptr ex) noexcept -> void +{ + res_ = ex; +} + +auto parse_result::set_gpx_file(routemon::gpx::file&& f) noexcept -> void +{ + res_ = std::move(f); +} + +auto parse_result::init_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + init_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::init_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return init_parse_dur_; +} + +auto parse_result::put_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + put_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::put_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return put_parse_dur_; +} + +auto parse_result::finish_parse_duration( + chrono::steady_clock::duration dur) noexcept -> void +{ + finish_parse_dur_ = dur; +} + +[[nodiscard]] auto parse_result::finish_parse_duration() const noexcept + -> chrono::steady_clock::duration +{ + return finish_parse_dur_; +} + +auto parse_result::unwrap() -> routemon::gpx::file&& +{ + return std::visit( + util::overloaded{ + [](std::exception_ptr ex) -> routemon::gpx::file&& + { + if (ex) + std::rethrow_exception(ex); + else + throw std::runtime_error{"no GPX file parse result available"}; + }, + [](routemon::gpx::file&& f) -> routemon::gpx::file&& + { return std::move(f); }, + }, + std::move(res_)); +} + +auto body::reader::init( + boost::optional /* n */, beast::error_code& ec) noexcept + -> void +{ + auto const init_start = chrono::steady_clock::now(); + try + { + r_.init(); + ec = {}; + } + catch (std::exception& ex) + { + res_->set_exception(std::current_exception()); + ec = parse_error(); + } + res_->init_parse_duration(chrono::steady_clock::now() - init_start); +} + +auto body::reader::finish(beast::error_code& ec) noexcept -> void +{ + auto const finish_start = chrono::steady_clock::now(); + try + { + if (buf_.size() > 0) + { + r_.put(std::string_view{buf_}); + buf_.clear(); + } + + res_->set_gpx_file(r_.finish()); + ec = {}; + } + catch (std::exception& ex) + { + res_->set_exception(std::current_exception()); + ec = parse_error(); + } + res_->finish_parse_duration(chrono::steady_clock::now() - finish_start); +} + +} // 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 @@ +module; + +#include +#include + +export module routemon:http.ext.gpx; + +import std; +import :gpx; +import :http.core.common; + +namespace chrono = std::chrono; + +namespace routemon::http::ext::gpx { + +class parse_error_category_impl : public std::error_category +{ +public: + auto name() const noexcept -> char const* override; + auto message(int condition) const noexcept -> std::string override; +}; + +auto parse_error_category() noexcept -> parse_error_category_impl const&; + +auto parse_error() noexcept -> std::error_code; + +class parse_result +{ + chrono::steady_clock::duration init_parse_dur_; + chrono::steady_clock::duration put_parse_dur_; + chrono::steady_clock::duration finish_parse_dur_; + std::variant res_; + +public: + auto set_exception(std::exception_ptr ex) noexcept -> void; + auto set_gpx_file(routemon::gpx::file&& f) noexcept -> void; + + auto init_parse_duration(chrono::steady_clock::duration dur) noexcept -> void; + [[nodiscard]] auto init_parse_duration() const noexcept + -> chrono::steady_clock::duration; + auto put_parse_duration(chrono::steady_clock::duration dur) noexcept -> void; + [[nodiscard]] auto put_parse_duration() const noexcept + -> chrono::steady_clock::duration; + auto finish_parse_duration(chrono::steady_clock::duration dur) noexcept + -> void; + [[nodiscard]] auto finish_parse_duration() const noexcept + -> chrono::steady_clock::duration; + + auto unwrap() -> routemon::gpx::file&&; +}; + +struct body +{ + using value_type = parse_result; + + class reader + { + routemon::gpx::reader r_; + util::not_null res_; + std::inplace_vector buf_; + + public: + template + explicit reader(bhttp::header&, value_type& v) : res_{&v} + { + res_->init_parse_duration({}); + res_->put_parse_duration({}); + res_->finish_parse_duration({}); + } + + // The following methods (which are called by Beast) are marked + // noexcept, since Beast does not ensure that exceptions thrown + // here are appropriately directed to the caller of + // (async_)read(_some), so throwing here might cause the program + // to crash. + + auto + init(boost::optional /* n */, beast::error_code& ec) noexcept + -> void; + + auto + put(beast::concepts::const_buffer_sequence auto b, + beast::error_code& ec) noexcept -> std::size_t + { + auto const put_start = chrono::steady_clock::now(); + auto total = 0uz; + try + { + for (auto cur_in_buf : beast::buffers_range(b)) + { + while (cur_in_buf.size() > 0) + { + auto to_read = + std::min(cur_in_buf.size(), buf_.max_size() - buf_.size()); + buf_.append_range( + std::span{ + static_cast(cur_in_buf.data()), to_read + }); + cur_in_buf += to_read; + total += to_read; + + if (buf_.size() == buf_.max_size()) + { + r_.put(std::string_view{buf_}); + buf_.clear(); + } + } + } + ec = {}; + } + catch (std::exception& ex) + { + res_->set_exception(std::current_exception()); + ec = parse_error(); + } + res_->put_parse_duration( + res_->put_parse_duration() + + (chrono::steady_clock::now() - put_start)); + return total; + } + + auto finish(beast::error_code& ec) noexcept -> void; + }; +}; +static_assert(bhttp::concepts::body); +static_assert(bhttp::concepts::body_reader); + +} // 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 @@ module; +#include +#include +#include + #include #include -export module routemon:hmac_openssl; +export module routemon:http.ext.hmac_openssl; + +import std; +import :http.core.common; +import :util; + +namespace { -namespace routemon::hmac_openssl { +namespace net = boost::asio; } + +namespace routemon::http::ext::hmac_openssl { + +// TODO: constrain T? What are appropriate constraints? +template +class readable +{ + std::string_view key_; + std::optional> mac_; + T res_; + + template + friend struct body_reader; + +public: + template + explicit readable(std::string_view key, Args&&... args) + : key_{key}, res_{std::forward(args)...} + { + } + + template + constexpr auto get(this Self&& self) -> auto&& + { + return std::forward(self).res_; + } + + auto mac() const -> std::optional> + { + return mac_; + } +}; + +template +class body_reader +{ + EVP_MAC* mac_ = nullptr; + EVP_MAC_CTX* ctx_ = nullptr; + + util::not_null*> v_; + Sub::reader subr_; + beast::error_code ec_; + +public: + template + explicit body_reader( + bhttp::header& h, + readable& v) + : v_{util::not_null{&v}}, + subr_{h, v.get()} // we are not supposed to use v yet, but this just + // computes an address, so it should be fine + { + } + + ~body_reader() + { + ::EVP_MAC_CTX_free(ctx_); + ::EVP_MAC_free(mac_); + } + + // TODO: move to impl. unit. + auto init(boost::optional n, beast::error_code& ec) noexcept + -> void + { + // https://docs.openssl.org/master/man3/EVP_MAC/#examples + mac_ = ::EVP_MAC_fetch(nullptr, "HMAC", nullptr); + if (!mac_) + { + ec_ = ec = make_error_code(std::errc::not_enough_memory); + return; + } + + ctx_ = ::EVP_MAC_CTX_new(mac_); + if (!ctx_) + { + ec_ = ec = make_error_code(std::errc::not_enough_memory); + return; + } + + char sha256[] = "SHA256"; + ::OSSL_PARAM const params[] = { + OSSL_PARAM_utf8_string("digest", sha256, sizeof(sha256) - 1), + OSSL_PARAM_END + }; + if (!::EVP_MAC_init( + ctx_, reinterpret_cast(v_->key_.data()), + v_->key_.size(), params)) + { + ec_ = ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return; + } + + subr_.init(n, ec); + } + + auto + put(beast::concepts::const_buffer_sequence auto b, + beast::error_code& ec) noexcept -> std::size_t + { + if ((ec = ec_)) + return 0uz; + + auto const total_read = subr_.put(b, ec); + if (total_read > 0) + { + auto remaining = total_read; + for (auto const buf : beast::buffers_range(b)) + { + auto const to_hash_from_this_buf = std::min(remaining, buf.size()); + if (!::EVP_MAC_update( + ctx_, static_cast(buf.data()), + to_hash_from_this_buf)) + { + ec_ = ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return total_read; + } + remaining -= to_hash_from_this_buf; + } + } + return total_read; + } + + auto finish(beast::error_code& ec) noexcept -> void + { + auto got_mac_len = 0uz; + auto got_mac = std::array{}; + if (!::EVP_MAC_final(ctx_, got_mac.data(), &got_mac_len, got_mac.size())) + { + ec = beast::error_code{ + static_cast(::ERR_get_error()), net::error::get_ssl_category() + }; + return; + } + assert(got_mac_len == got_mac.size()); + v_->mac_ = got_mac; + subr_.finish(ec); + } +}; + +template +struct body +{ + using value_type = readable; +}; + +template + requires bhttp::concepts::body_reader +struct body +{ + using value_type = readable; + + using reader = body_reader; +}; + +} // 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 @@ +module; + +#include +#include + +module routemon:http.ext.json$impl; + +import :http.ext.json; + +namespace routemon::http::ext::json { + +namespace { + +namespace json = boost::json; + +} // namespace + +auto body::reader::init( + boost::optional /* n */, beast::error_code& ec) noexcept + -> void +{ + p_.reset(); + ec = {}; +} + +auto body::reader::finish(beast::error_code& ec) noexcept -> void +{ + p_.finish(ec); + if (ec) + return; + *v_ = p_.release(); +} + +} // 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 @@ +module; + +#include +#include +#include + +export module routemon:http.ext.json; + +import std; +import :http.core.common; +import :util; + +namespace routemon::http::ext::json { + +namespace { + +namespace json = boost::json; + +} // namespace + +struct body +{ + using value_type = json::value; + + class reader + { + json::stream_parser p_; + util::not_null v_; + + public: + template + explicit reader(bhttp::header&, value_type& v) + : v_{util::not_null{&v}} + { + } + + auto + init(boost::optional /* n */, beast::error_code& ec) noexcept + -> void; + + auto + put(beast::concepts::const_buffer_sequence auto b, + beast::error_code& ec) noexcept -> std::size_t + { + ec = {}; + + auto wrote = 0uz; + for (auto const buf : beast::buffers_range(b)) + { + p_.write(static_cast(buf.data()), buf.size(), ec); + wrote += buf.size(); + if (ec) + break; + } + return wrote; + } + + auto finish(beast::error_code& ec) noexcept -> void; + }; +}; + +static_assert(bhttp::concepts::body); +static_assert(bhttp::concepts::body_reader); + +} // 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 @@ +module; + +#include +#include +#include +#include + +export module routemon:http.ext.raw_deflate; + +import :http.core.common; + +namespace routemon::http::ext::raw_deflate { + +namespace { + +namespace net = boost::asio; + +} + +template +struct body +{ + using value_type = Inner::value_type; + + class reader + { + static constexpr auto const chunk_size = 16384; + + Inner::reader r_; + bool stream_ended_ = false; + beast::zlib::z_params zs_ = {}; + beast::zlib::inflate_stream inflate_strm_ = {}; + std::unique_ptr> in_buf_ = nullptr; + std::unique_ptr> out_buf_ = nullptr; + + auto inflate_chunk(beast::error_code& ec) -> void + { + assert(zs_.avail_in > 0); + zs_.next_in = in_buf_->data(); + + do + { + zs_.avail_out = out_buf_->size(); + zs_.next_out = out_buf_->data(); + + inflate_strm_.write(zs_, beast::zlib::Flush::none, ec); + assert(ec != beast::zlib::error::stream_error); + if (ec == beast::zlib::error::need_buffers) + ec = {}; + if (ec == beast::zlib::error::end_of_stream) + { + ec = {}; + stream_ended_ = true; + } + if (ec) + break; + + auto have = chunk_size - zs_.avail_out; + auto cbuf = + net::buffer(static_cast(out_buf_->data()), have); + + auto inner_ec = beast::error_code{}; + do + cbuf += r_.put(cbuf, inner_ec); + while (cbuf.size() > 0 && !inner_ec); + if (inner_ec) + { + ec = inner_ec; + break; + } + } + while (zs_.avail_out == 0); + + // In the last case (stream_ended_), the user has sent too much data + assert(zs_.avail_in == 0 || ec || stream_ended_); + } + + public: + template + explicit reader(bhttp::header& hdr, value_type& v) + : r_{hdr, v} + { + } + + auto + init(boost::optional /* n */, beast::error_code& ec) noexcept + -> void + { + in_buf_.reset(new std::array{}); + out_buf_.reset(new std::array{}); + ec = {}; + } + + auto + put(beast::concepts::const_buffer_sequence auto b, + beast::error_code& ec) noexcept -> std::size_t + { + auto in_buf = net::buffer(in_buf_->data(), in_buf_->size()); + in_buf += zs_.avail_in; + + auto consumed_from_b = net::buffer_copy(in_buf, b); + zs_.avail_in += consumed_from_b; + + if (zs_.avail_in == in_buf_->size()) + inflate_chunk(ec); + else + ec = {}; + + return consumed_from_b; + } + + auto finish(beast::error_code& ec) noexcept -> void + { + ec = {}; + if (zs_.avail_in > 0) + inflate_chunk(ec); + if (!ec && !stream_ended_) + ec = beast::zlib::error::stream_error; + if (stream_ended_ && zs_.avail_in > 0) + // client sent data past deflate stream end + ec = beast::zlib::error::stream_error; + + in_buf_.reset(); + out_buf_.reset(); + inflate_strm_.reset(); + if (!ec) + { + r_.finish(ec); + } + } + }; +}; + +} // namespace routemon::http::ext::raw_deflate -- cgit v1.3.1