31 #ifndef MAIN_WEBSERVER_HPP_ 32 #define MAIN_WEBSERVER_HPP_ 34 #include "../Data/Compression/Gzip.hpp" 35 #include "../Helper/FileSystem.hpp" 36 #include "../Helper/Memory.hpp" 37 #include "../Helper/Strings.hpp" 38 #include "../Main/Exception.hpp" 41 #include "../_extern/mongoose/mongoose.h" 54 #include <string_view> 60 using std::string_view_literals::operator
""sv;
144 using ConnectionPtr = mg_connection *;
145 using ConstConnectionPtr =
const mg_connection *;
146 using StringString = std::pair<std::string, std::string>;
148 using AcceptCallback =
152 using LogCallback = std::function<void(const std::string&)>;
153 using RequestCallback =
165 explicit WebServer(std::string_view fileCacheDirectory);
176 void initHTTP(
const std::string& port);
190 void poll(
int timeOut);
192 ConnectionPtr connection,
194 const std::string& type,
195 const std::string& content
198 ConnectionPtr connection,
199 const std::string& fileName,
204 ConnectionPtr connection,
205 const std::string& error
208 ConnectionPtr connection,
216 static std::string
getIP(ConstConnectionPtr connection);
243 const std::string fileCache;
244 mg_mgr eventManager{};
245 bool isShutdown{
false};
248 AcceptCallback onAccept;
250 RequestCallback onRequest;
253 static void eventHandler(
254 ConnectionPtr connection,
259 void eventHandlerInClass(
260 ConnectionPtr connection,
264 void uploadHandler(ConnectionPtr connection, mg_http_message * msg);
265 void requestHandler(ConnectionPtr connection, mg_http_message * msg,
void * data);
268 void fileReceived(ConnectionPtr from,
const std::string& name,
const std::string& content);
271 static void parseHttpHeaders(
272 const std::array<mg_http_header, MG_MAX_HTTP_HEADERS>& headers,
273 std::string& contentEncodingTo
275 [[nodiscard]]
static bool parseHttpHeaders(
276 const std::array<mg_http_header, MG_MAX_HTTP_HEADERS>& headers,
277 std::string& boundaryTo,
278 std::uint64_t& sizeTo,
279 std::string& contentEncodingTo
281 [[nodiscard]]
static bool getLine(
struct mg_str& str, std::size_t& pos, std::string& to);
282 [[nodiscard]]
static bool isBoundary(
const std::string& line,
const std::string& boundary);
283 [[nodiscard]]
static bool isFinalBoundary(
const std::string& line,
const std::string& boundary);
284 [[nodiscard]]
static bool getUploadHeaders(
struct mg_str& str, std::size_t& pos, std::vector<StringString>& to);
285 [[nodiscard]]
static bool getUploadHeader(
const std::string& from, StringString& to);
287 [[nodiscard]]
static bool parseContentType(
288 const std::string& headerName,
289 const struct mg_str& headerValue,
290 std::string& boundaryTo,
291 bool& isBoundaryFoundTo
293 [[nodiscard]]
static bool parseContentSize(
294 const std::string& headerName,
295 const struct mg_str& headerValue,
296 std::uint64_t& sizeTo,
299 static void parseContentEncoding(
300 const std::string& headerName,
301 const struct mg_str& headerValue,
302 std::string& contentEncodingTo
305 [[nodiscard]]
static bool parseContentTypeHeader(
const std::string& value, std::string& boundaryTo);
306 [[nodiscard]]
static bool parseUploadHeaders(
const std::vector<StringString>& uploadHeaders, std::string& fileNameTo);
307 [[nodiscard]]
static bool parseNextHeaderPart(
const std::string& value, std::size_t& pos, std::string& to);
309 [[nodiscard]]
static bool checkFileName(
bool inFile,
const std::string& currentFile, std::string& fileName);
311 [[nodiscard]]
static std::string getDefaultHeaders();
312 [[nodiscard]]
static std::string getCorsHeaders();
313 [[nodiscard]]
static std::string toString(
const struct mg_str& str);
314 static void removeQuotes(std::string& str);
316 [[nodiscard]]
static const char * statusCodeToString(
int status_code);
void setRequestCallback(RequestCallback callback)
Sets callback function for HTTP requests.
Definition: WebServer.cpp:135
constexpr auto headerContentType
The name of a (lower-case) content type header.
Definition: WebServer.hpp:76
constexpr auto filePartBoundaryBegin
Required beginning of a HTTP multipart boundary.
Definition: WebServer.hpp:97
void initHTTP(const std::string &port)
Initializes the web server for usage over HTTP.
Definition: WebServer.cpp:83
void send(ConnectionPtr connection, uint16_t code, const std::string &type, const std::string &content)
Sends a HTTP reply to a previously established connection.
Definition: WebServer.cpp:170
constexpr auto filePartUploadField
The (lower-case) name of the content containing file content to upload.
Definition: WebServer.hpp:112
constexpr auto headerBoundaryBegin
The beginning of the header part that contains the boundary.
Definition: WebServer.hpp:88
constexpr auto quotesLength
The length of two encapsulating quotes, in bytes.
Definition: WebServer.hpp:118
static void close(ConnectionPtr connection, bool immediately)
Closes a connection immediately.
Definition: WebServer.cpp:370
#define MAIN_EXCEPTION_CLASS()
Macro used to easily define classes for general exceptions.
Definition: Exception.hpp:50
constexpr auto filePartUploadHeader
The name of the upload header containing content information.
Definition: WebServer.hpp:103
static void sendError(ConnectionPtr connection, const std::string &error)
Sends an internal server error (HTTP code 500) with a custom message and closes the connection...
Definition: WebServer.cpp:332
constexpr auto filePartUploadFileName
The name of the content containing the original name of the file to upload.
Definition: WebServer.hpp:109
constexpr auto filePartHeaderBegin
Required beginning of (lower-case) file part header.
Definition: WebServer.hpp:94
void poll(int timeOut)
Polls the web server.
Definition: WebServer.cpp:149
constexpr auto filePartBoundaryFinalEnd
The end of the final HTTP multipart boundary.
Definition: WebServer.hpp:100
constexpr auto headerContentEncoding
The name of a (lower-case) content encoding header.
Definition: WebServer.hpp:82
virtual ~WebServer()
Destructor freeing the web server.
Definition: WebServer.cpp:61
constexpr auto listenToAddress
The address at which to listen for incoming connections.
Definition: WebServer.hpp:73
Embedded web server class using the mongoose library.
Definition: WebServer.hpp:142
void setAcceptCallback(AcceptCallback callback)
Sets callback function for accepted connections.
Definition: WebServer.cpp:115
WebServer(std::string_view fileCacheDirectory)
Constructor setting the file cache and initializing the web server.
Definition: WebServer.cpp:48
constexpr auto filePartUploadName
The beginning of the field in the content information containing the name of the content.
Definition: WebServer.hpp:106
void setLogCallback(LogCallback callback)
Sets callback function for logging.
Definition: WebServer.cpp:125
WebServer & operator=(WebServer &)=delete
Deleted copy assignment operator.
void sendFile(ConnectionPtr connection, const std::string &fileName, bool isGzipped, void *data)
Sends a file located in the file cache.
Definition: WebServer.cpp:251
static std::string getIP(ConstConnectionPtr connection)
Static helper function retrieving the client IP from a connection.
Definition: WebServer.cpp:404
Namespace for the main classes of the program.
Definition: App.cpp:34
constexpr auto httpOk
HTTP OK response code.
Definition: WebServer.hpp:91
constexpr auto gzipMinBytes
The number of minimum bytes for gzip to be used.
Definition: WebServer.hpp:121
constexpr auto headerContentSize
The name of a (lower-case) content size header.
Definition: WebServer.hpp:79
constexpr auto headerContentTypeValue
The expected content type for HTTP multipart requests.
Definition: WebServer.hpp:85
constexpr auto randFileNameLength
The length of randomly generated file names.
Definition: WebServer.hpp:115