1#include "ThreadPoolWebServer.h"
5#include "Exceptions/FileDoesNotExistException.h"
6#include "Exceptions/NotFoundException.h"
7#include "Exceptions/SSLException.h"
8#include "Utility/Singletons/HTTPSSingleton.h"
9#include "HTTPSNetwork.h"
15 ThreadPoolWebServer::Client::Client(SSL* ssl, SSL_CTX* context, SOCKET clientSocket, sockaddr address, function<
void()>&& cleanup) :
19 make_unique<web::HTTPSNetwork>(clientSocket, ssl, context) :
20 make_unique<web::HTTPNetwork>(clientSocket)
22 cleanup(move(cleanup)),
26 clientSocket(clientSocket),
28 webExceptionAcquired(false)
33 bool ThreadPoolWebServer::Client::serve
35 SessionsManager& sessionsManager,
36 web::BaseTCPServer& server,
37 interfaces::IStaticFile& staticResources,
38 interfaces::IDynamicFile& dynamicResources,
39 sqlite::SQLiteManager& databaseManager,
40 ExecutorsManager& executorsManager,
41 ResourceExecutor& resourceExecutor,
42 threading::ThreadPool& threadPool
45 if (stream.eof() || webExceptionAcquired)
57 HTTPRequest request(sessionsManager, server, staticResources, dynamicResources, databaseManager, address, stream);
58 HTTPResponse response;
67 optional<function<void(HTTPRequest&, HTTPResponse&)>> threadPoolFunction = executorsManager.service(request, response, statefulExecutors);
69 if (threadPoolFunction)
75 [
this, &resourceExecutor, request = move(request), response = move(response), threadPoolFunction = move(threadPoolFunction)]()
mutable
79 (*threadPoolFunction)(request, response);
86 catch (
const web::exceptions::WebException& e)
90 Log::error(
"Thread pool serve exception: {}",
"ThreadPool", e.what());
93 webExceptionAcquired =
true;
95 catch (
const exceptions::BadRequestException& e)
97 resourceExecutor.badRequestError(response, &e);
101 catch (
const file_manager::exceptions::FileDoesNotExistException& e)
103 resourceExecutor.notFoundError(response, &e);
107 catch (
const exceptions::NotFoundException& e)
109 resourceExecutor.notFoundError(response, &e);
113 catch (
const exceptions::BaseExecutorException& e)
115 resourceExecutor.internalServerError(response, &e);
119 catch (
const exception& e)
121 resourceExecutor.internalServerError(response, &e);
127 resourceExecutor.internalServerError(response,
nullptr);
145 catch (
const web::exceptions::WebException& e)
149 Log::error(
"Serve exception: {}",
"ThreadPool", e.what());
154 catch (
const exceptions::BadRequestException& e)
156 HTTPResponse response;
158 resourceExecutor.badRequestError(response, &e);
162 catch (
const file_manager::exceptions::FileDoesNotExistException& e)
164 HTTPResponse response;
166 resourceExecutor.notFoundError(response, &e);
170 catch (
const exceptions::NotFoundException& e)
172 HTTPResponse response;
174 resourceExecutor.notFoundError(response, &e);
178 catch (
const exceptions::BaseExecutorException& e)
180 HTTPResponse response;
182 resourceExecutor.internalServerError(response, &e);
186 catch (
const exception& e)
188 HTTPResponse response;
190 resourceExecutor.internalServerError(response, &e);
196 HTTPResponse response;
198 resourceExecutor.internalServerError(response,
nullptr);
206 ThreadPoolWebServer::Client::~Client()
214 void ThreadPoolWebServer::serveClients()
216 for (
size_t i = 0; i < clients.size();)
218 Client* client = clients[i];
220 bool finished = client->serve
236 clients.erase(clients.begin() + i);
250 void ThreadPoolWebServer::clientConnection(
const string& ip, SOCKET clientSocket, sockaddr address, function<
void()>& cleanup)
258 ssl = SSL_new(context);
262 throw web::exceptions::SSLException(__LINE__, __FILE__);
265 if (!SSL_set_fd(ssl,
static_cast<int>(clientSocket)))
269 throw web::exceptions::SSLException(__LINE__, __FILE__);
272 if (
int errorCode = SSL_accept(ssl); errorCode != 1)
274 throw web::exceptions::SSLException(__LINE__, __FILE__, ssl, errorCode);
278 clients.push_back(
new Client(ssl, context, clientSocket, address, move(cleanup)));
280 catch (
const web::exceptions::SSLException& e)
284 Log::error(
"SSL exception: {}, ip: {}",
"LogHTTPS", e.what(), ip);
288 this->serveClients();
291 void ThreadPoolWebServer::onInvalidConnectionReceive()
293 this->serveClients();
296 ThreadPoolWebServer::ThreadPoolWebServer
298 const json::JSONParser& configuration,
299 const vector<utility::JSONSettingsParser>& parsers,
300 const filesystem::path& assets,
301 const filesystem::path& pathToTemplates,
302 uint64_t cachingSize,
306 const vector<string>& pathToSources,
318 IExecutorFunctionality
327 threadPool(threadCount ? threadCount : thread::hardware_concurrency())