summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext/json.cppm
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/http/ext/json.cppm')
-rw-r--r--server/src/http/ext/json.cppm65
1 files changed, 65 insertions, 0 deletions
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