summaryrefslogtreecommitdiffstats
path: root/server/src/http/ext/gpx.cppm
blob: 69763d80b04028aa29aad331f20941567d80356b (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module;

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

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<std::exception_ptr, routemon::gpx::file> 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<value_type*> res_;
    std::inplace_vector<char, 4096> buf_;

  public:
    template <bool isRequest, bhttp::concepts::fields Fields>
    explicit reader(bhttp::header<isRequest, Fields>&, 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<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
    {
      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<char const*>(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<body>);
static_assert(bhttp::concepts::body_reader<body>);

} // namespace routemon::http::ext::gpx