-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
126 lines (107 loc) · 4.6 KB
/
main2.cpp
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
#include <string>
#include <FlowUtils/FlowArgParser.h>
#include "Request.h"
#include "Response.h"
#include <thread>
#include <FlowUtils/ThreadPool.h>
#include <FlowUtils/WorkerPool.h>
#include "routes/Router.h"
#include "routes/FileNotFound.h"
#include "routes/GetRoute.h"
#include "routes/ListFiles.h"
#include "routes/ValidateRoute.h"
#include "routes/RelationalUpload.h"
#include "routes/IfModifiedSince.h"
#include "routes/InfoRoute.h"
#include <memory>
#include "routes/GetBrotliRoute.h"
#include "util/ArgParserUtil.h"
#include "webdav/Webdav.h"
int main(int argc, char *argv[]) {
FlowArgParser fap = ArgParserUtil::defaultArgParser();
fap.parse("localhost 1337 .");
fap.parse(argc, argv);
std::string address = fap.getString("address");
std::string port = fap.getString("port");
std::string path = fap.getString("path");
FlowString::replaceAll(path, "\\", "/");
LOG_INFO << "Listening on: " << address << ":" << port;
LOG_INFO << "Path: " << path;
size_t threadCount = fap.hasOption("threads") ? std::stoul(fap.getString("threads"), nullptr, 10) :
std::thread::hardware_concurrency() * 2;
WorkerPool workerPool(threadCount);
boost::asio::io_context io_context;
std::unique_ptr <boost::asio::ssl::context> ssl_context;
bool useSSL = false;
if (fap.hasOption("dh") && fap.hasOption("key") && fap.hasOption("cert")) {
useSSL = true;
ssl_context = std::make_unique<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
ssl_context->set_options(
boost::asio::ssl::context::default_workarounds
| boost::asio::ssl::context::no_sslv2);
ssl_context->use_certificate_chain_file(fap.getString("cert"));
ssl_context->use_private_key_file(fap.getString("key"),
boost::asio::ssl::context::pem);
ssl_context->use_tmp_dh_file(fap.getString("dh"));
}
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::resolver resolver(io_context);
auto resolverResult = resolver.resolve(query);
boost::asio::ip::tcp::acceptor acceptor(io_context, resolverResult.begin()->endpoint());
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
Semaphore semaphore;
Router router;
// router.addRoute(std::make_shared<FunctionRoute>(std::function<bool(Request &request, Response &response, Socket &socket)>(
// [](Request &request, Response &response, Socket &socket){
// LOG_INFO << "Test";
// return false;
// }
// )));
router.addRoute(std::make_shared<InfoRoute>());
router.addRoute(std::make_shared<ValidateRoute>());
router.addRoute(std::make_shared<Webdav>(path));
router.addRoute(std::make_shared<RelationalUpload>("./", "/upload"));
router.addRoute(std::make_shared<IfModifiedSince>(path));
router.addRoute(std::make_shared<GetBrotliRoute>(path));
router.addRoute(std::make_shared<GetRoute>(path));
router.addRoute(std::make_shared<ListFiles>(path));
router.addRoute(std::make_shared<FileNotFound>());
size_t keepRunning = 10;
while (keepRunning) {
semaphore.lock();
workerPool.addTask(std::make_shared<std::function<void()>> ([&] { // Threadfunction
boost::system::error_code error;
Socket socket(io_context);
acceptor.accept(*socket.GetSocket(), error);
if (useSSL) {
socket.SetSSL(*ssl_context);
socket.GetSSLSocket()->handshake(boost::asio::ssl::stream_base::server, error);
if (error) {
LOG_WARNING << "Bad Request";
semaphore.unlock();
return;
}
}
semaphore.unlock();
// auto req = FlowAsio::readBytes(socket);
// LOG_INFO << std::string(req.begin(), req.end());
//
// Request request;
// request << req;
bool continue_connection = true;
while (continue_connection) {
continue_connection = false;
Request request = FlowAsio::readRequest(socket);
Response response(socket.IsSSL());
router.execRoute(request, response, socket);
if (request.Header("Connection") == "keep-alive") {
continue_connection = true;
}
}
LOG_INFO << "Connection closed.";
})); // Threadfunction
workerPool.start();
}
workerPool.join();
return 0;
}