-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttps_server.cpp
More file actions
150 lines (131 loc) · 4.07 KB
/
https_server.cpp
File metadata and controls
150 lines (131 loc) · 4.07 KB
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
//
// Copyright (c) 2026 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/beast2
//
#include <boost/beast2/https_server.hpp>
#include <boost/beast2/http_worker.hpp>
#include <boost/capy/task.hpp>
#include <boost/capy/cond.hpp>
#include <boost/capy/ex/strand.hpp>
#include <boost/capy/io/any_read_source.hpp>
#include <boost/capy/io/any_read_stream.hpp>
#include <boost/capy/io/any_buffer_sink.hpp>
#include <boost/corosio/openssl_stream.hpp>
#include <boost/http/request_parser.hpp>
#include <boost/http/response.hpp>
#include <boost/http/server/router.hpp>
#include <boost/http/serializer.hpp>
#include <boost/http/string_body.hpp>
#include <boost/http/error.hpp>
#include <boost/url/parse.hpp>
#include <iostream>
#include <memory>
namespace boost {
namespace beast2 {
struct https_server::impl
{
corosio::tls_context tls_ctx;
http::router<http::route_params> router;
http::shared_parser_config parser_cfg;
http::shared_serializer_config serializer_cfg;
impl(
corosio::tls_context tc,
http::router<http::route_params> r)
: tls_ctx(std::move(tc))
, router(std::move(r))
{
}
};
struct https_server::
worker
: tcp_server::worker_base
, http_worker
{
corosio::io_context& ctx;
capy::strand<corosio::io_context::executor_type> strand;
corosio::tcp_socket sock;
corosio::tls_context tls_ctx;
std::unique_ptr<corosio::openssl_stream> ssl;
worker(
corosio::io_context& ctx_,
https_server* srv_)
: http_worker(
srv_->impl_->router,
srv_->impl_->parser_cfg,
srv_->impl_->serializer_cfg)
, ctx(ctx_)
, strand(ctx_.get_executor())
, sock(ctx_)
, tls_ctx(srv_->impl_->tls_ctx)
{
sock.open();
}
corosio::tcp_socket& socket() override
{
return sock;
}
void run(launcher launch) override
{
launch(ctx.get_executor(), do_session());
}
capy::task<void>
do_session()
{
// Create TLS stream wrapping the socket
ssl = std::make_unique<corosio::openssl_stream>(&sock, tls_ctx);
// Perform TLS handshake as server
auto [hs_ec] = co_await ssl->handshake(corosio::tls_stream::server);
if(hs_ec)
{
std::cerr << "TLS handshake error: " << hs_ec.message() << "\n";
sock.shutdown(corosio::tcp_socket::shutdown_both);
ssl.reset();
co_return;
}
// Wire parser and serializer to the TLS stream
rp.req_body = capy::any_buffer_source(parser.source_for(*ssl));
rp.res_body = capy::any_buffer_sink(serializer.sink_for(*ssl));
stream = capy::any_read_stream(ssl.get());
// Process HTTP requests over TLS
co_await do_http_session();
// Perform TLS shutdown
auto [shut_ec] = co_await ssl->shutdown();
if(shut_ec)
{
// TLS shutdown errors are common (peer may close abruptly)
}
// Clean up TLS stream before TCP shutdown
ssl.reset();
sock.shutdown(corosio::tcp_socket::shutdown_both);
}
};
https_server::
~https_server()
{
delete impl_;
}
https_server::
https_server(
corosio::io_context& ctx,
std::size_t num_workers,
corosio::tls_context tls_ctx,
http::router<http::route_params> router,
http::shared_parser_config parser_cfg,
http::shared_serializer_config serializer_cfg)
: tcp_server(ctx, ctx.get_executor())
, impl_(new impl(std::move(tls_ctx), std::move(router)))
{
impl_->parser_cfg = std::move(parser_cfg);
impl_->serializer_cfg = std::move(serializer_cfg);
std::vector<std::unique_ptr<tcp_server::worker_base>> workers;
workers.reserve(num_workers);
for(std::size_t i = 0; i < num_workers; ++i)
workers.push_back(std::make_unique<worker>(ctx, this));
set_workers(std::move(workers));
}
} // beast2
} // boost