summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext/json.cppm
blob: 4097253aca7ed151e9e0ed9db53571f4ba849ed2 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module;

#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/json/stream_parser.hpp>

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<value_type*> v_;

  public:
    template <bool isRequest, bhttp::concepts::fields Fields>
    explicit reader(bhttp::header<isRequest, Fields>&, value_type& v)
      : v_{util::not_null{&v}}
    {
    }

    auto
    init(boost::optional<std::uint64_t> /* 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<char const*>(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<body>);
static_assert(bhttp::concepts::body_reader<body>);

} // namespace routemon::http::ext::json