summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext/gpx.cppm
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/gpx.cppm
parent02475e05ccd1aa15c6a6163834f28bfebbef796f (diff)
downloadroutemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz
routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip
Cleanups and updates
Diffstat (limited to 'server/src/http/ext/gpx.cppm')
-rw-r--r--server/src/http/ext/gpx.cppm128
1 files changed, 128 insertions, 0 deletions
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