WebFramework v3.0.12
Web framework for C++.
Loading...
Searching...
No Matches
MultithreadedWebServer.cpp
1#include "MultithreadedWebServer.h"
2
3#include "Log.h"
4#include "HTTPSNetwork.h"
5
6#include "Exceptions/NotImplementedException.h"
7#include "Exceptions/FileDoesNotExistException.h"
8#include "Exceptions/NotFoundException.h"
9#include "Exceptions/CantFindFunctionException.h"
10#include "Exceptions/MissingLoadTypeException.h"
11#include "Exceptions/CantLoadSourceException.h"
12#include "Exceptions/BadRequestException.h"
13#include "Utility/RouteParameters.h"
14#include "Exceptions/SSLException.h"
15#include "Utility/Singletons/HTTPSSingleton.h"
16
17#ifndef __LINUX__
18#pragma warning(disable: 6387)
19#endif
20
21using namespace std;
22
23namespace framework
24{
25 void MultithreadedWebServer::clientConnection(const string& ip, SOCKET clientSocket, sockaddr addr, function<void()>& cleanup) //-V688
26 {
27 SSL* ssl = nullptr;
28
29 try
30 {
31 if (useHTTPS)
32 {
33 ssl = SSL_new(context);
34
35 if (!ssl)
36 {
37 throw web::exceptions::SSLException(__LINE__, __FILE__);
38 }
39
40 if (!SSL_set_fd(ssl, static_cast<int>(clientSocket)))
41 {
42 SSL_free(ssl);
43
44 throw web::exceptions::SSLException(__LINE__, __FILE__);
45 }
46
47 if (int errorCode = SSL_accept(ssl); errorCode != 1)
48 {
49 throw web::exceptions::SSLException(__LINE__, __FILE__, ssl, errorCode);
50 }
51 }
52 }
53 catch (const web::exceptions::SSLException& e)
54 {
55 if (Log::isValid())
56 {
57 Log::error("SSL exception: {}, ip: {}", "LogHTTPS", e.what(), ip);
58 }
59
60 return;
61 }
62
63 streams::IOSocketStream stream
64 (
65 useHTTPS ?
66 make_unique<web::HTTPSNetwork>(clientSocket, ssl, context) :
67 make_unique<web::HTTPNetwork>(clientSocket)
68 );
69 unordered_map<string, unique_ptr<BaseExecutor>> statefulExecutors;
70 HTTPResponse response;
71
72 while (isRunning)
73 {
74 try
75 {
76 HTTPRequest request(sessionsManager, *this, *resources, *resources, databaseManager, addr, stream);
77
78 response.setDefault();
79
80 stream >> request;
81
82 if (stream.eof())
83 {
84 break;
85 }
86
87 executorsManager.service(request, response, statefulExecutors);
88
89 if (response)
90 {
91 stream << response;
92 }
93
94 if (stream.eof())
95 {
96 break;
97 }
98 }
99 catch (const web::exceptions::WebException& e)
100 {
101 if (Log::isValid())
102 {
103 Log::error("Multithreaded serve exception: {}", "Multithreaded", e.what());
104 }
105
106 break;
107 }
108 catch (const exceptions::BadRequestException& e) // 400
109 {
110 resources->badRequestError(response, &e);
111
112 stream << response;
113 }
114 catch (const file_manager::exceptions::FileDoesNotExistException& e) // 404
115 {
116 resources->notFoundError(response, &e);
117
118 stream << response;
119 }
120 catch (const exceptions::NotFoundException& e) // 404
121 {
122 resources->notFoundError(response, &e);
123
124 stream << response;
125 }
126 catch (const exceptions::BaseExecutorException& e) // 500
127 {
128 resources->internalServerError(response, &e);
129
130 stream << response;
131 }
132 catch (const exception& e)
133 {
134 resources->internalServerError(response, &e);
135
136 stream << response;
137 }
138 catch (...) // 500
139 {
140 resources->internalServerError(response, nullptr);
141
142 stream << response;
143 }
144 }
145 }
146
147 MultithreadedWebServer::MultithreadedWebServer
148 (
149 const json::JSONParser& configuration,
150 const vector<utility::JSONSettingsParser>& parsers,
151 const filesystem::path& assets,
152 const filesystem::path& pathToTemplates,
153 uint64_t cachingSize,
154 string_view ip,
155 string_view port,
156 DWORD timeout,
157 const vector<string>& pathToSources
158 ) :
159 BaseTCPServer
160 (
161 port,
162 ip,
163 timeout,
164 true,
165 0,
166 false
167 ),
168 IExecutorFunctionality
169 (
170 configuration,
171 assets,
172 pathToTemplates,
173 cachingSize,
174 parsers,
175 pathToSources
176 )
177 {
178
179 }
180}