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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
module;
#include <boost/asio/as_tuple.hpp>
#include <boost/asio/awaitable.hpp>
#include <boost/asio/co_spawn.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/config.hpp>
#include <boost/json.hpp>
#include <boost/locale/generator.hpp>
#include <expat.h>
module routemon:srv$impl;
import std;
import :gpx;
import :srv;
namespace beast = boost::beast;
namespace json = boost::json;
namespace net = boost::asio;
using tcp = boost::asio::ip::tcp;
namespace routemon::srv {
class gpx_parse_error_category_impl : public std::error_category
{
public:
char const* name() const noexcept override { return "gpx_parse"; }
auto message(int condition) const noexcept -> std::string override
{
std::ignore = condition;
return "failed to parse GPX file";
}
};
auto gpx_parse_error_category() noexcept -> gpx_parse_error_category_impl const&
{
static auto const inst = gpx_parse_error_category_impl{};
return inst;
}
auto gpx_parse_error() noexcept -> std::error_code
{
return std::error_code{1, gpx_parse_error_category()};
}
class gpx_parse_result
{
std::variant<std::exception_ptr, gpx::file> res_;
public:
auto set_exception(std::exception_ptr ex) noexcept { res_ = ex; }
auto set_gpx_file(gpx::file&& f) noexcept { res_ = std::move(f); }
auto unwrap() -> gpx::file&&
{
return std::visit(
util::overloaded{
[](std::exception_ptr ex) -> gpx::file&&
{
if (ex)
std::rethrow_exception(ex);
else
throw std::runtime_error{"no GPX file parse result available"};
},
[](gpx::file&& f) -> gpx::file&& { return std::move(f); },
},
std::move(res_));
}
};
struct readable_gpx_body
{
using value_type = gpx_parse_result;
class reader
{
gpx::reader r_;
util::not_null<value_type*> res_;
public:
template <bool isRequest, bhttp::concepts::fields Fields>
explicit reader(bhttp::header<isRequest, Fields>&, value_type& v) : res_{&v}
{
}
// 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
{
try
{
r_.init();
ec = {};
}
catch (std::exception& ex)
{
res_->set_exception(std::current_exception());
ec = gpx_parse_error();
}
}
auto
put(beast::concepts::const_buffer_sequence auto b,
beast::error_code& ec) noexcept -> std::size_t
{
auto total = 0uz;
try
{
for (auto it = net::buffer_sequence_begin(b);
it != net::buffer_sequence_end(b); it++)
{
r_.put(
std::string_view{
static_cast<char const*>(it->data()), it->size()
});
total += it->size();
}
ec = {};
}
catch (std::exception& ex)
{
res_->set_exception(std::current_exception());
ec = gpx_parse_error();
}
return total;
}
auto finish(beast::error_code& ec) noexcept
{
try
{
res_->set_gpx_file(r_.finish());
ec = {};
}
catch (std::exception& ex)
{
res_->set_exception(std::current_exception());
ec = gpx_parse_error();
}
}
};
};
static_assert(bhttp::concepts::body<readable_gpx_body>);
static_assert(bhttp::concepts::body_reader<readable_gpx_body>);
auto handler::handle_process_gpx(l0_ctx ctx, http::readable_request r)
-> net::awaitable<http::presponse>
{
auto gpx_file = gpx::file{};
try
{
auto req =
co_await http::read_request<readable_gpx_body>(ctx, std::move(r));
gpx_file = std::move(req->body().unwrap());
}
catch (std::exception& ex)
{
// TODO: more detailed problem reporting
auto tpl = problem::tpl{
.status = bhttp::status::bad_request,
.title = translate("Failed to parse GPX file"),
.type_uri = "https://routemon.fautchen.eu/problems/gpx-parse-failed",
};
co_return http::problem_rsp(
ctx, tpl.instantiate(), http::keep_alive{false});
}
// TODO: catch handler exceptions and return 500 when raised?
// (keep-alive depends on whether whole request was read)
auto mres = inner_.process_gpx(std::move(gpx_file));
if (!mres)
{
auto tpl = problem::tpl{
.status = bhttp::status::internal_server_error,
.title = translate("Internal server error"),
.type_uri = "https://routemon.fautchen.eu/problems/"
"internal-server-error",
};
co_return http::problem_rsp(ctx, tpl.instantiate(), http::keep_alive{true});
}
auto rsp = http::make_rsp<bhttp::string_body>(
bhttp::status::ok, http::keep_alive{true});
rsp.set(bhttp::field::content_type, "application/json");
rsp.body() = json::serialize(json::value_from(*mres));
rsp.prepare_payload();
co_return rsp;
}
auto handler::handle_sysinfo(l0_ctx ctx, http::readable_request r)
-> net::awaitable<http::presponse>
{
auto req = co_await http::read_request<bhttp::empty_body>(ctx, std::move(r));
auto info = inner_.sysinfo();
auto rsp = http::make_rsp<bhttp::string_body>(
bhttp::status::ok, http::keep_alive{true});
rsp.set(bhttp::field::content_type, "application/json");
rsp.body() = json::serialize(json::value_from(info));
rsp.prepare_payload();
co_return rsp;
}
handler::handler(api::handler&& inner) : inner_{std::move(inner)} {}
auto handler::make_routes() -> http::route_tree<http::routed_ctx<outer_ctx>>
{
auto handler = [this]<class MemFn>(MemFn member)
{ return std::bind_front(member, this); };
return http::dtree<http::routed_ctx<outer_ctx>>{}.named_subtrees({
{"gpx", http::dtree<l0_ctx>{{
.post = handler(&handler::handle_process_gpx),
}}
.no_subtrees()},
{"sysinfo", http::dtree<l0_ctx>{
{
.get = handler(&handler::handle_sysinfo),
}
}.no_subtrees()},
});
}
auto server::make_global_middleware(config::http_server const& cfg)
-> http::middleware_t<http::base_ctx, handler::outer_ctx>
{
return http::middleware_compose<
http::base_ctx, http::trace_id_ctx<http::base_ctx>,
http::trace_id_ctx<http::base_ctx>
>(http::trace_id_middleware<http::base_ctx>,
http::cors_middleware<http::trace_id_ctx<http::base_ctx>>(
cfg.allow_origins));
}
server::server(
log::logger const& l, config::http_server const& cfg,
locale::selector&& lsel, api::handler&& inner)
: handler_{std::move(inner)},
srv_{
l, http::router{
std::move(lsel), make_global_middleware(cfg), handler_.make_routes()
}
}
{
}
auto server::spawn(net::io_context& ioc) -> void { srv_.spawn(ioc); }
} // namespace routemon::srv
|