-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpphtgt.cpp
More file actions
455 lines (420 loc) · 9.28 KB
/
cpphtgt.cpp
File metadata and controls
455 lines (420 loc) · 9.28 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//
// Copyright (c) 2025 cppfx.xyz
//
// 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)
//
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <iostream>
#include <concepts>
#include <fstream>
#include <filesystem>
namespace asio = boost::asio;
namespace beast = boost::beast;
namespace http = beast::http;
using std::string_literals::operator""s;
namespace cpphtgt
{
class host_info
{
public:
const std::string host;
const unsigned short port;
const std::string domain;
const std::string webroot;
const unsigned long long file_size_limit; // file size limit in bytes
const std::string __404__;
};
class uri_file_handler:
virtual public std::enable_shared_from_this<cpphtgt::uri_file_handler>
{
private:
cpphtgt::host_info __host_info;
const beast::string_view __uri;
std::string __real_filename;
bool __is_bad_uri_file = false;
bool __is_plain_file = false;
public:
uri_file_handler(
const cpphtgt::host_info host_info__,
const beast::string_view uri__
):
__host_info{host_info__},
__uri{uri__}
{
this->check_uri_file();
}
public:
void check_uri_file()
{
if (
__uri.empty()
||
__uri[0] != '/'
||
__uri.find("..") != beast::string_view::npos
)
{
__is_bad_uri_file = true;
return;
}
__real_filename = __host_info.webroot + __uri;
if (! std::filesystem::exists(__real_filename))
{
__is_bad_uri_file = true;
return;
}
if (std::filesystem::is_directory(__real_filename))
__real_filename += "/index.html";
if (! std::filesystem::exists(__real_filename))
{
__is_bad_uri_file = true;
return;
}
if (std::filesystem::file_size(__real_filename) > __host_info.file_size_limit)
{
__is_bad_uri_file = true;
return;
}
{
std::ifstream file{__real_filename};
if (! file)
__is_bad_uri_file = true;
else
__is_bad_uri_file = false;
file.close();
return;
}
}
bool is_bad_uri_file() const
{
return __is_bad_uri_file;
}
public:
asio::awaitable<http::status> get_status() const
{
if (this->is_bad_uri_file())
{
co_return http::status::not_found;
}
else
{
co_return http::status::ok;
}
}
asio::awaitable<std::string> get_mime_type()
{
std::string mtinfo;
try
{
const beast::string_view bfn = __real_filename;
auto str1 = bfn.substr(bfn.rfind("/"));
auto pos = str1.rfind(".");
auto ext = str1.substr(pos);
mtinfo = [&, this] -> std::string
{
if (
beast::iequals(ext, ".html")
||
beast::iequals(ext, ".htm")
)
{
this->__is_plain_file = true;
return "text/html";
}
if (
beast::iequals(ext, ".css")
)
{
this->__is_plain_file = true;
return "text/css";
}
if (
beast::iequals(ext, ".txt")
||
beast::iequals(ext, ".text")
)
{
this->__is_plain_file = true;
return "text/plain";
}
if (
beast::iequals(ext, ".png")
)
return "image/png";
if (
beast::iequals(ext, ".jpg")
||
beast::iequals(ext, ".jpeg")
)
return "image/jpeg";
if (
beast::iequals(ext, ".mp3")
)
return "audio/mpeg";
if (
beast::iequals(ext, ".wav")
)
return "audio/wav";
if (
beast::iequals(ext, ".mp4")
)
return "video/mp4";
if (
beast::iequals(ext, ".cpp")
||
beast::iequals(ext, ".hpp")
)
{
this->__is_plain_file = true;
return "text/cpp";
}
return "text/html";
}
();
}
catch (...)
{
mtinfo = "text/html";
}
co_return mtinfo;
}
public:
asio::awaitable<std::string> get_html()
{
if (this->is_bad_uri_file())
{
co_return __host_info.__404__;
}
std::ifstream file;
if (this->__is_plain_file)
file.open(__real_filename);
else
file.open(__real_filename, std::ios::binary);
std::string all;
std::string one;
while (std::getline(file, one))
{
all += one + "\n";
}
co_return all;
}
};
class session: virtual public std::enable_shared_from_this<cpphtgt::session>
{
private:
const cpphtgt::host_info __host_info;
beast::tcp_stream __tcp_stream;
private:
http::request<http::string_body> __request;
http::response<http::string_body> __response;
beast::flat_buffer __buffer;
public:
session(
const cpphtgt::host_info host_info__,
asio::ip::tcp::socket && socket__
):
__host_info{host_info__},
__tcp_stream{std::move(socket__)}
{
}
virtual ~session()
{
std::cout << "A session is closed!" << std::endl << std::endl << "\n\n\n\n";
}
public:
asio::awaitable<void> start()
{
std::cout << "A session is started!" << std::endl;
co_await this->response(co_await this->get_request());
co_return;
}
private:
asio::awaitable<unsigned long> get_request()
{
unsigned long bytes = co_await http::async_read(
__tcp_stream,
__buffer,
__request,
asio::use_awaitable
);
std::cout << "--------------------------------------------------\n";
std::cout << "--------------------------------------------------\n";
std::cout << __request << '\n';
std::cout << "--------------------------------------------------\n";
std::cout << "--------------------------------------------------\n";
co_return bytes;
}
private:
asio::awaitable<void> response(unsigned long req_bytes)
{
auto uri_file_handler = std::make_shared<cpphtgt::uri_file_handler>(
__host_info,
__request.target()
);
__response = http::response<http::string_body>{
co_await uri_file_handler->get_status(),
__request.version()
};
__response.keep_alive(__request.keep_alive());
__response.set(http::field::server, "cpphtgt");
__response.set(
http::field::content_type,
co_await uri_file_handler->get_mime_type()
);
__response.body() = co_await uri_file_handler->get_html();
__response.content_length(__response.body().size());
co_await http::async_write(
__tcp_stream,
__response,
asio::use_awaitable
);
co_return;
}
};
class server: virtual public std::enable_shared_from_this<cpphtgt::server>
{
private:
const cpphtgt::host_info __host_info;
asio::ip::tcp::acceptor __acceptor;
public:
server(
const cpphtgt::host_info host_info__,
asio::any_io_executor executor__
):
__host_info{host_info__},
__acceptor{
executor__,
asio::ip::tcp::endpoint{
asio::ip::make_address(__host_info.host),
__host_info.port
}
}
{
}
virtual ~server()
{
}
public:
asio::awaitable<void> start()
{
std::cout << "A server is started and listening ..." << std::endl << std::endl;
std::cout << "Bind Address: " << __host_info.host << '\n';
std::cout << "Bind Port: " << __host_info.port << '\n';
std::cout << "Bind Domain: " << __host_info.domain << '\n';
std::cout << "webroot: " << __host_info.webroot << '\n';
std::cout << "File Size Limit in bytes: " << __host_info.file_size_limit << std::endl;
std::cout << std::endl;
co_await this->accept();
co_return;
}
private:
asio::awaitable<void> accept()
{
{
asio::ip::tcp::socket socket = co_await __acceptor.async_accept(
asio::use_awaitable
);
co_await std::make_shared<cpphtgt::session>(
__host_info,
std::move(socket)
)
->start();
// socket is moved to session, remove socket object in this context now
}
co_await this->accept();
co_return;
}
};
class server_manager:
virtual public std::enable_shared_from_this<cpphtgt::server_manager>
{
private:
const cpphtgt::host_info __host_info;
bool __should_quit = false;
unsigned long long __tried = 0;
std::shared_ptr<asio::io_context> __io_context;
public:
server_manager(const cpphtgt::host_info & host_info__):
__host_info{host_info__}
{
}
virtual ~server_manager()
{
}
public:
void auto_restart_server()
{
while (! __should_quit)
{
try
{
++__tried;
__io_context = std::make_shared<asio::io_context>();
asio::co_spawn(
*__io_context,
[self=this->shared_from_this()] -> asio::awaitable<void>
{
co_await std::make_shared<cpphtgt::server>(
self->__host_info,
co_await asio::this_coro::executor
)
->start();
},
[] (std::exception_ptr eptr)
{
if (eptr)
std::rethrow_exception(eptr);
}
);
__io_context->run();
}
catch (const std::exception & e)
{
std::cout << "Server Error: " << e.what() << std::endl;
std::cout
<< "Try restarting."
<< std::endl;
}
std::cout << "Tried: " << __tried << " times" << std::endl;
}
}
};
}
int main()
{
try
{
cpphtgt::host_info host_info{
"127.0.0.1",
9988u,
"example.com",
"/path/cpphtgt/webroot",
200 * 1024 * 1024, // File size limit 200M
R"(
<!DOCTYPE html>
<html>
<head>
<title>404</title>
</head>
<body>
<center>
<h1>404</h1>
<p>
<a href="http://example.com">example.com</a>
</p>
</center>
</body>
</html>
)"
};
std::make_shared<cpphtgt::server_manager>(host_info)
->auto_restart_server();
}
catch (const std::exception & e)
{
std::cout << "General c++ exception, I can not figure out reason."
<< std::endl;
}
}