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.cppm | 128 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 server/src/http/ext/gpx.cppm (limited to 'server/src/http/ext/gpx.cppm') 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 -- cgit v1.3.1