diff options
Diffstat (limited to 'server/src/http/ext/gpx.cppm')
| -rw-r--r-- | server/src/http/ext/gpx.cppm | 128 |
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 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | #include <boost/beast/core.hpp> | ||
| 4 | #include <boost/beast/http.hpp> | ||
| 5 | |||
| 6 | export module routemon:http.ext.gpx; | ||
| 7 | |||
| 8 | import std; | ||
| 9 | import :gpx; | ||
| 10 | import :http.core.common; | ||
| 11 | |||
| 12 | namespace chrono = std::chrono; | ||
| 13 | |||
| 14 | namespace routemon::http::ext::gpx { | ||
| 15 | |||
| 16 | class parse_error_category_impl : public std::error_category | ||
| 17 | { | ||
| 18 | public: | ||
| 19 | auto name() const noexcept -> char const* override; | ||
| 20 | auto message(int condition) const noexcept -> std::string override; | ||
| 21 | }; | ||
| 22 | |||
| 23 | auto parse_error_category() noexcept -> parse_error_category_impl const&; | ||
| 24 | |||
| 25 | auto parse_error() noexcept -> std::error_code; | ||
| 26 | |||
| 27 | class 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 | |||
| 34 | public: | ||
| 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 | |||
| 52 | struct 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 | }; | ||
| 125 | static_assert(bhttp::concepts::body<body>); | ||
| 126 | static_assert(bhttp::concepts::body_reader<body>); | ||
| 127 | |||
| 128 | } // namespace routemon::http::ext::gpx | ||