crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
crawlservpp::Main::WebServer Class Referencefinal

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

The class is neither copyable, nor movable.

 WebServer (WebServer &)=delete
 Deleted copy constructor. More...
 
WebServeroperator= (WebServer &)=delete
 Deleted copy assignment operator. More...
 
 WebServer (WebServer &&)=delete
 Deleted move constructor. More...
 
WebServeroperator= (WebServer &&)=delete
 Deleted move assignment operator. More...
 

Detailed Description

Embedded web server class using the mongoose library.

For more information about the mongoose library, see its GitHub repository.

Warning
The web server does one final poll on destruction. When used inside classes, it should therefore declared last, i.e. destroyed first, in case it uses other member data when polled.

Constructor & Destructor Documentation

◆ WebServer() [1/3]

crawlservpp::Main::WebServer::WebServer ( std::string_view  fileCacheDirectory)
explicit

Constructor setting the file cache and initializing the web server.

Parameters
fileCacheDirectoryView of the directory in which to temporarily save files uploaded to the server.

◆ ~WebServer()

crawlservpp::Main::WebServer::~WebServer ( )
virtual

Destructor freeing the web server.

Warning
The web server does one final poll on destruction. When used inside classes, it should therefore declared last, i.e. destroyed first, in case it uses other member data when polled.

◆ WebServer() [2/3]

crawlservpp::Main::WebServer::WebServer ( WebServer )
delete

Deleted copy constructor.

◆ WebServer() [3/3]

crawlservpp::Main::WebServer::WebServer ( WebServer &&  )
delete

Deleted move constructor.

Member Function Documentation

◆ close()

void crawlservpp::Main::WebServer::close ( ConnectionPtr  connection,
bool  immediately 
)
static

Closes a connection immediately.

Parameters
connectionPointer to the connection that will closed immediately.
immediatelySet to true to close the connection immediately, without sending any remaining data.
Exceptions
WebServer::Connectionif no connection has been specified, i.e. the pointer to the connection is nullptr.

Referenced by sendError(), and crawlservpp::Main::Server::tick().

◆ getIP()

◆ initHTTP()

void crawlservpp::Main::WebServer::initHTTP ( const std::string &  port)

Initializes the web server for usage over HTTP.

Todo:
The HTTPS protocol is not supported yet.

Binds the web server to its port, sets user data and the protocol.

Parameters
portConstant reference to a string containing the port to be used by the web server.
Exceptions
WebServer::Exceptionif the server could not be bound to the given port.

References crawlservpp::Main::listenToAddress.

Referenced by crawlservpp::Main::Server::tick().

◆ operator=() [1/2]

WebServer& crawlservpp::Main::WebServer::operator= ( WebServer )
delete

Deleted copy assignment operator.

◆ operator=() [2/2]

WebServer& crawlservpp::Main::WebServer::operator= ( WebServer &&  )
delete

Deleted move assignment operator.

◆ poll()

void crawlservpp::Main::WebServer::poll ( int  timeOut)

Polls the web server.

Parameters
timeOutThe number of milliseconds after which polling the web server times out.

Referenced by crawlservpp::Main::Server::tick().

◆ send()

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.

Parameters
connectionPointer to the connection which will be used for sending the HTTP reply.
codeHTTP status code to be send at the beginning of the response.
typeConstant reference to a string containing the content type to be sent.
contentConstant reference to a string containing the content to be sent.
Exceptions
WebServer::Connectionif 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().

◆ sendError()

void crawlservpp::Main::WebServer::sendError ( ConnectionPtr  connection,
const std::string &  error 
)
static

Sends an internal server error (HTTP code 500) with a custom message and closes the connection.

Parameters
connectionPointer to the connection which will be used for sending the error and which will closed afterwards.
errorConstant reference to a string containing the custom error message sent via the given connection before closing it.
Exceptions
WebServer::Connectionif no connection has been specified, i.e. the pointer to the connection is nullptr.

References close().

Referenced by getIP(), and sendFile().

◆ 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.

Parameters
connectionPointer to the connection which will be used for sending the file.
fileNameConstant reference to a string containing name of the file to be sent.
isGzippedIndicates whether the file to be sent is compressed with gzip. Merely adds the 'Content-Encoding: gzip' header to the HTTP response.
dataPointer to the HTTP message requesting the file, as retrieved by the callback function set via setRequestCallback.
Exceptions
WebServer::Connectionif no connection has been specified, i.e. the pointer to the connection is nullptr.
See also
setRequestCallback

References crawlservpp::Helper::FileSystem::contains(), getIP(), crawlservpp::Helper::FileSystem::getPathSeparator(), sendError(), and crawlservpp::Main::Exception::view().

Referenced by crawlservpp::Main::Server::tick().

◆ setAcceptCallback()

void crawlservpp::Main::WebServer::setAcceptCallback ( AcceptCallback  callback)

Sets callback function for accepted connections.

Parameters
callbackA callback function that will be called when a connection got accepted.

Referenced by crawlservpp::Main::Server::tick().

◆ setLogCallback()

void crawlservpp::Main::WebServer::setLogCallback ( LogCallback  callback)

Sets callback function for logging.

Parameters
callbackA callback function that will be called when the web server wants to log something.

Referenced by crawlservpp::Main::Server::tick().

◆ setRequestCallback()

void crawlservpp::Main::WebServer::setRequestCallback ( RequestCallback  callback)

Sets callback function for HTTP requests.

Parameters
callbackA callback function that will be called when a HTTP request has been received.

Referenced by crawlservpp::Main::Server::tick().


The documentation for this class was generated from the following files: