summaryrefslogtreecommitdiffstats
path: root/server/src/http_server.cpp
diff options
context:
space:
mode:
authorRutger Broekhoff2026-11-30 21:27:55 +0100
committerRutger Broekhoff2026-11-30 21:27:55 +0100
commit7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch)
treea9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/http_server.cpp
parent02475e05ccd1aa15c6a6163834f28bfebbef796f (diff)
downloadroutemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz
routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip
Cleanups and updates
Diffstat (limited to 'server/src/http_server.cpp')
-rw-r--r--server/src/http_server.cpp209
1 files changed, 0 insertions, 209 deletions
diff --git a/server/src/http_server.cpp b/server/src/http_server.cpp
deleted file mode 100644
index ff96091..0000000
--- a/server/src/http_server.cpp
+++ /dev/null
@@ -1,209 +0,0 @@
1module;
2
3#include <boost/asio/as_tuple.hpp>
4#include <boost/asio/awaitable.hpp>
5#include <boost/asio/co_spawn.hpp>
6#include <boost/asio/ip/tcp.hpp>
7#include <boost/beast/core.hpp>
8#include <boost/beast/http.hpp>
9#include <boost/config.hpp>
10#include <boost/json/serialize.hpp>
11#include <boost/url.hpp>
12
13module routemon:http.server$impl;
14
15import :http.server;
16import :trace;
17
18namespace chrono = std::chrono;
19
20namespace routemon::http {
21
22auto presponse::header() -> bhttp::response_header<bhttp::fields>&
23{
24 return impl_->header();
25}
26
27auto presponse::header() const -> bhttp::response_header<bhttp::fields> const&
28{
29 return impl_->header();
30}
31
32auto presponse::is_done() const -> bool { return impl_->is_done(); }
33
34auto presponse::prepare(beast::error_code& ec) -> const_buffers_type
35{
36 return impl_->prepare(ec);
37}
38
39auto presponse::consume(std::size_t n) -> void { return impl_->consume(n); }
40
41auto presponse::keep_alive() const noexcept -> bool
42{
43 return impl_->keep_alive();
44}
45
46keep_alive::keep_alive(bool value) : value{value} {}
47
48auto problem_rsp(
49 base_ctx const& ctx, problem::details const& problem, keep_alive ka)
50 -> presponse
51{
52 auto rsp = make_rsp<bhttp::string_body>(problem.status, ka);
53 rsp.set(bhttp::field::content_type, "application/problem+json");
54 rsp.body() = json::serialize(json::value_from(problem, ctx.locale));
55 rsp.prepare_payload();
56 return presponse{std::move(rsp)};
57}
58
59auto make_preflight_rsp(preflight_response res, keep_alive ka)
60 -> bhttp::response<bhttp::empty_body>
61{
62 auto rsp = make_rsp<bhttp::empty_body>(bhttp::status::no_content, ka);
63 auto allow_headers_str = res.allow_headers
64 | std::views::transform(
65 [](auto const& field) -> std::string_view
66 { return bhttp::to_string(field); })
67 | std::views::join_with(std::string_view{", "})
68 | std::ranges::to<std::string>();
69 rsp.set(
70 bhttp::field::access_control_allow_methods,
71 res.allow_methods.to_string());
72 rsp.set(bhttp::field::access_control_allow_headers, allow_headers_str);
73 rsp.prepare_payload();
74 return rsp;
75}
76
77auto global_options_handler(base_ctx const& ctx, readable_request r)
78 -> net::awaitable<presponse>
79{
80 // TODO: switch to "small (4KB) discarded" body type, similar to what Go
81 // does? Same goes for default_options_handler? Not sure.
82 if (auto res = co_await read_empty_request(ctx, std::move(r)); !res)
83 co_return std::move(res.error());
84 auto req = r.p->release();
85 auto rsp = make_rsp<bhttp::empty_body>(
86 bhttp::status::no_content, keep_alive{req.keep_alive()});
87 rsp.prepare_payload();
88 co_return std::move(rsp);
89}
90
91auto router::handle_request(trace::id trace_id, readable_request r) const
92 -> net::awaitable<presponse>
93{
94 return impl_->handle_request(trace_id, r);
95}
96
97server::server(log::logger const& l, router&& r)
98 : l_{l.sub("http_server")}, r_{std::move(r)}
99{
100}
101
102auto server::do_session(beast::tcp_stream strm) -> net::awaitable<void>
103{
104 auto buf = beast::flat_buffer{};
105
106 while (true)
107 {
108 auto p0 = bhttp::request_parser<bhttp::empty_body>{};
109 p0.body_limit(boost::none);
110 auto [ec, _] =
111 co_await bhttp::async_read_header(strm, buf, p0, net::as_tuple);
112 if (ec == bhttp::error::end_of_stream)
113 break;
114 else if (ec)
115 throw boost::system::system_error{ec};
116
117 auto trace_id = trace::id{};
118 auto l = l_.sub("do_session").with("trace_id", trace_id.as_string());
119 l.debug("Read header, invoking request handler");
120 auto const before_hdl = chrono::steady_clock::now();
121
122 auto http_version = p0.get().version();
123 auto&& rsp = co_await r_.handle_request(
124 trace_id, readable_request{
125 .p = util::not_null{&p0},
126 .strm = util::not_null{&strm},
127 .buf = util::not_null{&buf},
128 });
129
130 l.debug(
131 "Request handler returned after {}, writing response",
132 chrono::duration<double, std::milli>{
133 chrono::steady_clock::now() - before_hdl
134 });
135 auto before_write_rsp = chrono::steady_clock::now();
136
137 rsp.header().version(http_version);
138 bool keep_alive = rsp.keep_alive();
139 co_await beast::async_write(strm, std::move(rsp));
140
141 l.debug(
142 "Wrote response in {}", chrono::duration<double, std::milli>{
143 chrono::steady_clock::now() - before_write_rsp
144 });
145
146 if (!keep_alive)
147 {
148 break;
149 }
150 }
151
152 strm.socket().shutdown(tcp::socket::shutdown_send);
153}
154
155auto server::do_listen(tcp::endpoint endpoint) -> net::awaitable<void>
156{
157 auto executor = co_await net::this_coro::executor;
158 auto acceptor = tcp::acceptor{executor, endpoint};
159
160 l_.with("endpoint", endpoint.address().to_string())
161 .with("port", std::to_string(endpoint.port()))
162 .info("Serving");
163 while (true)
164 {
165 net::co_spawn(
166 executor,
167 do_session(beast::tcp_stream{co_await acceptor.async_accept()}),
168 [this](std::exception_ptr e)
169 {
170 if (e)
171 {
172 try
173 {
174 std::rethrow_exception(e);
175 }
176 catch (std::exception const& e)
177 {
178 l_.error("Error in session: {}", e.what());
179 }
180 }
181 });
182 }
183}
184
185auto server::spawn(net::io_context& ioc) -> void
186{
187 auto const addr = net::ip::make_address("0.0.0.0");
188 auto const endpoint = tcp::endpoint{addr, 8284};
189
190 // TODO: make exception handling as nice as in srv.cpp
191 net::co_spawn(
192 ioc, do_listen(endpoint),
193 [this](std::exception_ptr e)
194 {
195 if (e)
196 {
197 try
198 {
199 std::rethrow_exception(e);
200 }
201 catch (std::exception const& e)
202 {
203 l_.error("Error: {}", e.what());
204 }
205 }
206 });
207}
208
209} // namespace routemon::http