-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpServer.cc
More file actions
235 lines (199 loc) · 6.77 KB
/
HttpServer.cc
File metadata and controls
235 lines (199 loc) · 6.77 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
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
/*
* Copyright (C) 2021 Frank Mertens.
*
* Distribution and use is allowed under the terms of the GNU General Public License version 3
* (see CoreComponents/LICENSE-gpl-3.0).
*
*/
#include <cc/HttpServer>
#include <cc/HttpServiceRegistry>
#include <cc/HttpConnectionManager>
#include <cc/HttpServiceWorker>
#include <cc/httpDebug>
#include <cc/PluginLoader>
#include <cc/TlsSessionCache>
#include <cc/ServerSocket>
#include <cc/IoMonitor>
#include <cc/Thread>
#include <cc/Channel>
#include <cc/Process>
#include <cc/UserInfo>
#include <cc/File>
#include <cc/str>
namespace cc {
CC_PLUGIN_TARGET
void HttpServer::loadPlugins()
{
static bool firstTime = true;
if (firstTime) {
firstTime = false;
PluginLoader{pluginTarget(), { "delivery", "logging" }};
}
}
struct HttpServer::State final: public Object::State
{
explicit State(const String &config):
State{HttpServerConfig{config}}
{}
explicit State(const HttpServerConfig &config):
nodeConfig_{config}
{
if (config.pidPath() != "" || config.daemon()) {
String path = config.pidPath();
if (path == "") path = "/var/run/" + config.daemonName() + ".pid";
File::save(path, str(Process::currentId()) + "\n");
}
}
void start()
{
thread_ = Thread{[this]{ run(); }};
thread_.start();
}
void wait()
{
if (thread_) thread_.wait();
}
void run()
{
while (true) {
try {
runNode();
}
catch (Signaled &ex) {
if (ex.signal() != Signal::HangUp) {
if (ex.signal() == Signal::Interrupt || ex.signal() == Signal::Terminate)
exitCode_ = 0;
else
exitCode_ = +ex.signal() + 128;
break;
}
}
catch (Exception &ex) {
CCNODE_ERROR() << ex << nl;
exitCode_ = 1;
break;
}
}
}
void runNode()
{
List<ServerSocket> listeningSockets;
for (const SocketAddress &address: nodeConfig_.address()) {
CCNODE_NOTICE() << "Start listening at " << address << nl;
listeningSockets.append(ServerSocket{address});
}
if (nodeConfig_.user() != "") {
String userName = nodeConfig_.user();
UserInfo user{userName};
if (!user) throw UsageError{"No such user: \"" + userName + "\""};
CCNODE_NOTICE() << "Dropping to user " << userName << " (uid = " << user.id() << ")" << nl;
Process::setUserId(user.id());
}
const double tlsRefreshInterval = nodeConfig_.tlsOptions().sessionResumptionKeyRefresh();
if (tlsRefreshInterval > 0) {
CCNODE_NOTICE() << "TLS session resumption key refresh interval set to " << tlsRefreshInterval << "s" << nl;
}
TlsSessionCache sessionCache{tlsRefreshInterval,
[this](const Bytes &key){
CCNODE_INFO() << "TLS session resumption key refreshed: key = 0x" << hex(key) << nl;
}
};
HttpConnectionManager connectionManager{nodeConfig_};
PendingConnections pendingConnections;
ClosedConnections closedConnections = connectionManager.closedConnections();
CCNODE_NOTICE() << "Creating worker pool (concurrency = " << nodeConfig_.concurrency() << ")" << nl;
Array<HttpServiceWorker> workerPool = Array<HttpServiceWorker>::allocate(nodeConfig_.concurrency());
for (HttpServiceWorker &worker: workerPool) {
worker = HttpServiceWorker{nodeConfig_, pendingConnections, closedConnections, sessionCache};
worker.start();
}
IoMonitor ioMonitor;
for (const StreamSocket &socket: listeningSockets) {
ioMonitor.watch(socket, IoEvent::ReadyAccept);
}
CCNODE_DEBUG() << "Accepting connections" << nl;
for (const StreamSocket &socket: listeningSockets)
startedChannel_.pushBack(socket.address());
while (true) {
ioMonitor.wait(
[&](const IoActivity &activity){
try {
ServerSocket serverSocket = activity.target().as<ServerSocket>();
StreamSocket stream = serverSocket.accept();
stream.setIncomingTimeout(nodeConfig_.connectionTimeout());
HttpClientConnection client{stream, serverSocket.address()};
if (connectionManager.accept(&client)) {
CCNODE_DEBUG() << "Accepted connection from " << client.peerAddress() << " with priority " << client.priority() << nl;
pendingConnections.pushBack(client, client.priority());
}
else {
CCNODE_DEBUG() << "Rejected connection from " << client.peerAddress() << nl;
}
}
catch (Exception &ex) {
CCNODE_ERROR() << ex << nl;
}
},
1000
);
connectionManager.cycle();
while (signals_.count() > 0) {
Signal signal { Signal::Undefined };
signals_.popFront(&signal);
if (signal == Signal::Interrupt || signal == Signal::Terminate || signal == Signal::HangUp) {
CCNODE_NOTICE() << "Received " << signal << ", shutting down ..." << nl;
workerPool.deplete();
CCNODE_NOTICE() << "Shutdown complete" << nl;
throw Signaled{signal};
}
}
}
}
const HttpLoggingServiceInstance &errorLoggingInstance() const { return nodeConfig_.errorLoggingInstance(); }
Thread thread_;
HttpServerConfig nodeConfig_;
Channel<SocketAddress> startedChannel_;
Channel<Signal> signals_;
int exitCode_ { 0 };
};
HttpServer::HttpServer(const String &config):
Object{new State{config}}
{}
HttpServer::HttpServer(const HttpServerConfig &config):
Object{new State{config}}
{}
void HttpServer::start()
{
me().start();
}
SocketAddress HttpServer::waitStarted()
{
SocketAddress address;
me().startedChannel_.popFront(&address);
return address;
}
void HttpServer::wait()
{
me().wait();
}
void HttpServer::shutdown()
{
me().signals_.pushBack(Signal::Terminate);
}
Channel<Signal> &HttpServer::signals()
{
return me().signals_;
}
int HttpServer::exitCode() const
{
return me().exitCode_;
}
HttpServer::State &HttpServer::me()
{
return Object::me.as<State>();
}
const HttpServer::State &HttpServer::me() const
{
return Object::me.as<State>();
}
}