-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.cpp
More file actions
360 lines (344 loc) · 11.3 KB
/
Copy pathhttp_server.cpp
File metadata and controls
360 lines (344 loc) · 11.3 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "http_server.h"
#include <chrono>
#include <sstream>
#include <cstring>
#include <evhttp.h>
#include <fcntl.h>
#ifdef _WIN32
#include <io.h>
#define open _open
#define lseek _lseek
#define close _close
#else
#include <unistd.h>
#define open open
#define lseek lseek
#define close close
#endif
namespace Network
{
namespace
{
class HttpRequest final
: private Common::NonCopyable
, public IHttpRequest
{
public:
HttpRequest(evhttp_request *request)
: Request(request)
{
}
int GetResponseCode() const
{
return ResponseCode;
}
private:
evhttp_request *Request;
evkeyvalq *InputHeaders = nullptr;
evkeyvalq *OutputHeaders = nullptr;
evbuffer *InputBuf = nullptr;
evbuffer *OutputBuf = nullptr;
evhttp_uri const *Uri = nullptr;
int ResponseCode = HTTP_OK;
void GetUri() const
{
if (Uri)
return;
auto *This = const_cast<HttpRequest *>(this);
if (!(This->Uri = evhttp_request_get_evhttp_uri(Request)))
throw HttpRequestException("Failed to get uri.");
}
void GetOutputBuf() const
{
if (OutputBuf)
return;
auto *This = const_cast<HttpRequest *>(this);
if (!(This->OutputBuf = evhttp_request_get_output_buffer(Request)))
throw HttpRequestException("Failed to get output buffer.");
}
void GetInputHeaders() const
{
if (InputHeaders != nullptr)
return;
auto *This = const_cast<HttpRequest *>(this);
if (!(This->InputHeaders = evhttp_request_get_input_headers(Request)))
throw HttpRequestException("Failed to get http input headers.");
}
void GetOutputHeaders() const
{
if (OutputHeaders != nullptr)
return;
auto *This = const_cast<HttpRequest *>(this);
if (!(This->OutputHeaders = evhttp_request_get_output_headers(Request)))
throw HttpRequestException("Failed to get http output headers.");
}
static void FreeBuf(void const *data, std::size_t datalen, void *extra)
{
::operator delete (const_cast<void *>(data));
}
virtual Type GetRequestType() const
{
switch (evhttp_request_get_command(Request))
{
case EVHTTP_REQ_GET :
return Type::GET;
case EVHTTP_REQ_POST :
return Type::POST;
case EVHTTP_REQ_HEAD :
return Type::HEAD;
case EVHTTP_REQ_PUT :
return Type::PUT;
default :
break;
}
throw HttpRequestException("Unknown request type.");
}
virtual std::string const GetHeaderAttr(char const *attrName) const
{
GetInputHeaders();
char const *Ret = evhttp_find_header(InputHeaders, attrName);
return Ret ? Ret : "";
}
virtual std::size_t GetContentSize() const
{
if (InputBuf == nullptr)
{
auto *This = const_cast<HttpRequest *>(this);
if (!(This->InputBuf = evhttp_request_get_input_buffer(Request)))
throw HttpRequestException("Failed to get input buffer.");
}
return evbuffer_get_length(InputBuf);
}
virtual void GetContent(void *buf, std::size_t len, bool remove) const
{
if (len > GetContentSize())
throw HttpRequestException("Required length of data buffer more than exists.");
if (remove)
{
if (evbuffer_remove(InputBuf, buf, len) == -1)
throw HttpRequestException("Failed to get input data.");
return;
}
if (evbuffer_copyout(InputBuf, buf, len) == -1)
throw HttpRequestException("Failed to get input data.");
}
virtual std::string const GetPath() const
{
GetUri();
char const *Ret = evhttp_uri_get_path(Uri);
return Ret ? Ret : "";
}
virtual RequestParams const GetParams() const
{
GetUri();
RequestParams Params;
char const *Query = evhttp_uri_get_query(Uri);
if (!Query)
return std::move(Params);
std::stringstream Io;
Io << Query;
for (std::string s ; Io ; )
{
std::getline(Io, s, '&');
auto Pos = s.find('=');
if (Pos != std::string::npos)
Params[s.substr(0, Pos)] = s.substr(Pos + 1);
else
Params[s] = "";
}
return std::move(Params);
}
virtual void SetResponseAttr(std::string const &name, std::string const &val)
{
GetOutputHeaders();
if (evhttp_add_header(OutputHeaders, name.c_str(), val.c_str()) == -1)
throw HttpRequestException("Failed to set response header attribute.");
}
virtual void SetResponseCode(int code)
{
ResponseCode = code;
}
virtual void SetResponseString(std::string const &str)
{
GetOutputBuf();
if (evbuffer_add_printf(OutputBuf, str.c_str()) == -1)
throw HttpRequestException("Failed to make response.");
}
virtual void SetResponseBuf(void const *data, std::size_t bytes)
{
GetOutputBuf();
void *Data = ::operator new (bytes);
std::memcpy(Data, data, bytes);
if (evbuffer_add_reference(OutputBuf, Data, bytes, &HttpRequest::FreeBuf, nullptr) == -1)
{
::operator delete (Data);
throw HttpRequestException("Failed to make response.");
}
}
virtual void SetResponseFile(std::string const &fileName)
{
GetOutputBuf();
auto FileDeleter = [] (int *f) { if (*f != -1) close(*f); delete f; };
std::unique_ptr<int, decltype(FileDeleter)> File(new int(open(fileName.c_str(), 0)), FileDeleter);
if (*File == -1)
throw HttpRequestException(HTTP_NOTFOUND, "Could not find content for uri.");
ev_off_t Length = lseek(*File, 0, SEEK_END);
if (Length == -1 || lseek(*File, 0, SEEK_SET) == -1)
throw HttpRequestException("Failed to calc file size.");
if (evbuffer_add_file(OutputBuf, *File, 0, Length) == -1)
throw HttpRequestException("Failed to make response.");
*File.get() = -1;
}
};
struct RequestParams
{
HttpServer::OnRequestFunc Func;
bool volatile *Process = nullptr;
};
void OnRawRequest(evhttp_request *request, void *prm)
{
try
{
auto Request = std::make_shared<HttpRequest>(request);
auto *ReqPrm = reinterpret_cast<RequestParams *>(prm);
Common::BoolFlagInvertor FlagInvertor(ReqPrm->Process);
ReqPrm->Func(Request);
auto *OutBuf = evhttp_request_get_output_buffer(request);
if (!OutBuf)
throw HttpRequestException("Failed to get output buffer.");
evhttp_send_reply(request, Request->GetResponseCode(), "", OutBuf);
}
catch (HttpRequestException const &e)
{
if (e.GetCode())
{
std::stringstream Io;
Io << "<html><body>"
"<hr/><center><h1>"
<< e.GetCode()
<< ". "
<< e.what()
<< "</center><hr/></h1>"
<< "</body></html>";
evhttp_send_error(request, e.GetCode(), Io.str().c_str());
}
else
{
evhttp_send_error(request, HTTP_INTERNAL,
"<html><body>"
"<hr/><center><h1>500. Internal error.</center><hr/></h1>"
"</body></html>");
}
}
catch (std::exception const &e)
{
evhttp_send_error(request, HTTP_INTERNAL,
"<html><body>"
"<hr/><center><h1>500. Internal error.</center><hr/></h1>"
"</body></html>");
}
}
int HttpRequestTypeToAllowedMethod(IHttpRequest::Type const &type)
{
switch (type)
{
case IHttpRequest::Type::GET :
return EVHTTP_REQ_GET;
case IHttpRequest::Type::HEAD :
return EVHTTP_REQ_HEAD;
case IHttpRequest::Type::PUT :
return EVHTTP_REQ_PUT;
case IHttpRequest::Type::POST :
return EVHTTP_REQ_POST;
default :
break;
}
throw HttpRequestException("Method not allowed.");
}
}
HttpServer::HttpServer(std::string const &address, std::uint16_t port,
std::uint16_t threadCount, OnRequestFunc const &onRequest,
MethodPool const &allowedMethods,
std::size_t maxHeadersSize, std::size_t maxBodySize)
: RunFlag(&IsRun)
{
int AllowedMethods = -1;
for (auto const i : allowedMethods)
AllowedMethods |= HttpRequestTypeToAllowedMethod(i);
bool volatile DoneInitThread = false;
std::exception_ptr Except;
evutil_socket_t Socket = -1;
auto ThreadFunc = [&] ()
{
try
{
bool volatile ProcessRequest = false;
RequestParams ReqPrm;
ReqPrm.Func = onRequest;
ReqPrm.Process = &ProcessRequest;
typedef std::unique_ptr<event_base, decltype(&event_base_free)> EventBasePtr;
EventBasePtr EventBase(event_base_new(), &event_base_free);
if (!EventBase)
throw HttpServerException("Failed to create new base_event.");
typedef std::unique_ptr<evhttp, decltype(&evhttp_free)> EvHttpPtr;
EvHttpPtr EvHttp(evhttp_new(EventBase.get()), &evhttp_free);
if (!EvHttp)
throw HttpServerException("Failed to create new evhttp.");
evhttp_set_allowed_methods(EvHttp.get(), AllowedMethods);
if (maxHeadersSize != MaxHeaderSize)
evhttp_set_max_headers_size(EvHttp.get(), maxHeadersSize);
if (maxBodySize != MaxBodySize)
evhttp_set_max_body_size(EvHttp.get(), maxBodySize);
evhttp_set_gencb(EvHttp.get(), &OnRawRequest, &ReqPrm);
if (Socket == -1)
{
auto *BoundSock = evhttp_bind_socket_with_handle(EvHttp.get(), address.c_str(), port);
if (!BoundSock)
throw HttpServerException("Failed to bind server socket.");
if ((Socket = evhttp_bound_socket_get_fd(BoundSock)) == -1)
throw HttpServerException("Failed to get server socket for next instance.");
}
else
{
if (evhttp_accept_socket(EvHttp.get(), Socket) == -1)
throw HttpServerException("Failed to bind server socket for new instance.");
}
DoneInitThread = true;
for ( ; IsRun ; )
{
ProcessRequest = false;
if (event_base_loop(EventBase.get(), EVLOOP_NONBLOCK) == -1)
{
// TODO: to log
//std::cerr << "Loop error." << std::endl;
}
if (!ProcessRequest)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
catch (...)
{
Except = std::current_exception();
}
};
ThreadPool NewThreads;
for (int i = 0 ; i < threadCount ; ++i)
{
DoneInitThread = false;
ThreadPtr Thread(new std::thread(ThreadFunc), ThreadDeleter);
NewThreads.push_back(std::move(Thread));
for ( ; ; )
{
if (Except != std::exception_ptr())
{
IsRun = false;
std::rethrow_exception(Except);
}
if (DoneInitThread)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
Threads = std::move(NewThreads);
}
}