|
crawlserv++
[under development]
Application for crawling and analyzing textual content of websites.
|
Embedded web server class using the mongoose library.
More...
#include <WebServer.hpp>
Classes | |
| class | Exception |
| Class for web server exceptions. More... | |
Construction and Destruction | |
| WebServer (std::string_view fileCacheDirectory) | |
| Constructor setting the file cache and initializing the web server. More... | |
| virtual | ~WebServer () |
| Destructor freeing the web server. More... | |
@name Initialization | |
| void | initHTTP (const std::string &port) |
| Initializes the web server for usage over HTTP. More... | |
Setters | |
| void | setAcceptCallback (AcceptCallback callback) |
| Sets callback function for accepted connections. More... | |
| void | setLogCallback (LogCallback callback) |
| Sets callback function for logging. More... | |
| void | setRequestCallback (RequestCallback callback) |
| Sets callback function for HTTP requests. More... | |
Networking | |
| void | poll (int timeOut) |
| Polls the web server. More... | |
| void | send (ConnectionPtr connection, uint16_t code, const std::string &type, const std::string &content) |
| Sends a HTTP reply to a previously established connection. More... | |
| void | sendFile (ConnectionPtr connection, const std::string &fileName, bool isGzipped, void *data) |
| Sends a file located in the file cache. More... | |
| 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. More... | |
| static void | close (ConnectionPtr connection, bool immediately) |
| Closes a connection immediately. More... | |
Static Helper Function | |
| static std::string | getIP (ConstConnectionPtr connection) |
| Static helper function retrieving the client IP from a connection. More... | |
Copy and Move | |
| WebServer (WebServer &)=delete | |
| Deleted copy constructor. More... | |
| WebServer & | operator= (WebServer &)=delete |
| Deleted copy assignment operator. More... | |
| WebServer (WebServer &&)=delete | |
| Deleted move constructor. More... | |
| WebServer & | operator= (WebServer &&)=delete |
| Deleted move assignment operator. More... | |
Embedded web server class using the mongoose library.
For more information about the mongoose library, see its GitHub repository.
|
explicit |
Constructor setting the file cache and initializing the web server.
| fileCacheDirectory | View of the directory in which to temporarily save files uploaded to the server. |
|
virtual |
Destructor freeing the web server.
|
delete |
Deleted copy constructor.
|
delete |
Deleted move constructor.
|
static |
Closes a connection immediately.
| connection | Pointer to the connection that will closed immediately. |
| immediately | Set to true to close the connection immediately, without sending any remaining data. |
| WebServer::Connection | if no connection has been specified, i.e. the pointer to the connection is nullptr. |
Referenced by sendError(), and crawlservpp::Main::Server::tick().
|
static |
Static helper function retrieving the client IP from a connection.
| connection | Pointer to the connection from which to retrieve the IP address of the connected client. |
| WebServer::Connection | if no connection has been specified, i.e. the pointer to the connection is nullptr. |
References crawlservpp::Data::Compression::Gzip::decompress(), crawlservpp::Helper::FileSystem::exists(), crawlservpp::Main::filePartBoundaryBegin, crawlservpp::Main::filePartBoundaryFinalEnd, crawlservpp::Main::filePartHeaderBegin, crawlservpp::Main::filePartUploadField, crawlservpp::Main::filePartUploadFileName, crawlservpp::Main::filePartUploadHeader, crawlservpp::Main::filePartUploadName, crawlservpp::Helper::Memory::free(), crawlservpp::Helper::Strings::generateRandom(), crawlservpp::Helper::FileSystem::getPathSeparator(), crawlservpp::Main::headerBoundaryBegin, crawlservpp::Main::headerContentEncoding, crawlservpp::Main::headerContentSize, crawlservpp::Main::headerContentType, crawlservpp::Main::headerContentTypeValue, crawlservpp::Main::httpOk, crawlservpp::Main::quotesLength, crawlservpp::Main::randFileNameLength, sendError(), and crawlservpp::Helper::Strings::trim().
Referenced by sendFile(), and crawlservpp::Main::Server::tick().
| void crawlservpp::Main::WebServer::initHTTP | ( | const std::string & | port | ) |
Initializes the web server for usage over HTTP.
Binds the web server to its port, sets user data and the protocol.
| port | Constant reference to a string containing the port to be used by the web server. |
| WebServer::Exception | if the server could not be bound to the given port. |
References crawlservpp::Main::listenToAddress.
Referenced by crawlservpp::Main::Server::tick().
Deleted copy assignment operator.
Deleted move assignment operator.
| void crawlservpp::Main::WebServer::poll | ( | int | timeOut | ) |
Polls the web server.
| timeOut | The number of milliseconds after which polling the web server times out. |
Referenced by crawlservpp::Main::Server::tick().
| void crawlservpp::Main::WebServer::send | ( | ConnectionPtr | connection, |
| uint16_t | code, | ||
| const std::string & | type, | ||
| const std::string & | content | ||
| ) |
Sends a HTTP reply to a previously established connection.
| connection | Pointer to the connection which will be used for sending the HTTP reply. |
| code | HTTP status code to be send at the beginning of the response. |
| type | Constant reference to a string containing the content type to be sent. |
| content | Constant reference to a string containing the content to be sent. |
| WebServer::Connection | if no connection has been specified, i.e. the pointer to the connection is nullptr. |
References crawlservpp::Data::Compression::Gzip::compress(), and crawlservpp::Main::gzipMinBytes.
Referenced by crawlservpp::Main::Server::tick().
|
static |
Sends an internal server error (HTTP code 500) with a custom message and closes the connection.
| connection | Pointer to the connection which will be used for sending the error and which will closed afterwards. |
| error | Constant reference to a string containing the custom error message sent via the given connection before closing it. |
| WebServer::Connection | if no connection has been specified, i.e. the pointer to the connection is nullptr. |
References close().
Referenced by getIP(), and sendFile().
| void crawlservpp::Main::WebServer::sendFile | ( | ConnectionPtr | connection, |
| const std::string & | fileName, | ||
| bool | isGzipped, | ||
| void * | data | ||
| ) |
Sends a file located in the file cache.
The file might be a relative path, but must be located in the file cache. If the path to the file is invalid, an internal server error (HTTP code 500) will be sent instead of the file.
Files can only be sent in response to a HTTP message received by the callback function set via setRequestCallback.
| connection | Pointer to the connection which will be used for sending the file. |
| fileName | Constant reference to a string containing name of the file to be sent. |
| isGzipped | Indicates whether the file to be sent is compressed with gzip. Merely adds the 'Content-Encoding: gzip' header to the HTTP response. |
| data | Pointer to the HTTP message requesting the file, as retrieved by the callback function set via setRequestCallback. |
| WebServer::Connection | if no connection has been specified, i.e. the pointer to the connection is nullptr. |
References crawlservpp::Helper::FileSystem::contains(), getIP(), crawlservpp::Helper::FileSystem::getPathSeparator(), sendError(), and crawlservpp::Main::Exception::view().
Referenced by crawlservpp::Main::Server::tick().
| void crawlservpp::Main::WebServer::setAcceptCallback | ( | AcceptCallback | callback | ) |
Sets callback function for accepted connections.
| callback | A callback function that will be called when a connection got accepted. |
Referenced by crawlservpp::Main::Server::tick().
| void crawlservpp::Main::WebServer::setLogCallback | ( | LogCallback | callback | ) |
Sets callback function for logging.
| callback | A callback function that will be called when the web server wants to log something. |
Referenced by crawlservpp::Main::Server::tick().
| void crawlservpp::Main::WebServer::setRequestCallback | ( | RequestCallback | callback | ) |
Sets callback function for HTTP requests.
| callback | A callback function that will be called when a HTTP request has been received. |
Referenced by crawlservpp::Main::Server::tick().