|
| server (unsigned short port=0, unsigned int maxconn=0) |
| Constructor.
|
|
| ~server () |
| Destructor.
|
|
void | add_ohdr (const std::string &hdr, const std::string &value) |
| Add or modify a server response header.
|
|
void | remove_ohdr (const std::string &hdr) |
| Remove a server response header.
|
|
void | add_handler (const std::string &uri, uri_handler func, void *info=0) |
| Add or modify an URI handler function.
|
|
void | add_post_handler (const std::string &uri, uri_handler func, void *info=0) |
| Add or modify an POST handler function.
|
|
void | add_user (const std::string &realm, const std::string &username, const std::string &pwd) |
| Add a new user to a protection realm.
|
|
void | remove_user (const std::string &realm, const std::string &username) |
| Remove an allowed user from an protection realm.
|
|
void | add_secured_path (const std::string &realm, const std::string &uri) |
| Add an URI to a protection realm.
|
|
template<typename T> |
void | add_var (const std::string &name, const T *addr, const char *fmt=nullptr) |
|
template<typename T> |
std::enable_if_t< std::is_floating_point_v< T > > | add_var (const std::string &name, const T *addr, const char *fmt=nullptr, double multiplier=1.) |
|
const std::string | get_var (const std::string &name) |
| Return the current string representation of a variable.
|
|
void | aquire_varlock () |
| Acquire lock on server's variables.
|
|
void | release_varlock () |
| Release lock on server's variables.
|
|
bool | try_varlock () |
| Try to acquire lock on server's variables.
|
|
void | name (const std::string &name_) |
| Change server name.
|
|
void | docroot (const std::filesystem::path &path) |
| Set server root path.
|
|
const std::filesystem::path & | docroot () const |
| Return current root path as absolute path.
|
|
void | add_alias (const std::string &uri, const std::string &path) |
| Maps a local file path to an URI resource path.
|
|
void | default_uri (const std::string &name) |
| Set default file name (initially index.html )
|
|
const std::string & | default_uri () const |
| Return default file name (initially index.html )
|
|
void | keep_alive (unsigned int seconds) |
| Set timeout value for keep-alive connections.
|
|
unsigned int | keep_alive () const |
| Return timeout value for keep-alive connections.
|
|
bool | is_protected (const std::string &uri, std::string &realm) |
| Find if URI is covered by a protection realm.
|
|
virtual bool | verify_authorization (const std::string &realm, const std::string &user, const std::string &password) |
|
| tcpserver (unsigned short port, const std::string &name=std::string(), unsigned int max_conn=0) |
| Opens the socket and initializes the connections table.
|
|
| ~tcpserver () |
| Terminate any connection that still exists.
|
|
sock & | socket () |
| Provides access to server listening socket.
|
|
void | foreach (conn_iter_func f, void *param) |
| Invoke an iteration function for each active connection.
|
|
thread * | get_connection_thread (const sock &conn_sock) |
| Return the thread servicing a connection.
|
|
void | close_connection (const sock &conn_sock) |
| Closes a connection.
|
|
void | terminate () |
| Terminate the tcp server.
|
|
size_t | numconn () const |
| Return number of active connections.
|
|
unsigned int | timeout () const |
| Return max interval to wait for an incoming connection (in milliseconds)
|
|
void | timeout (DWORD msec) |
| Set maximum timeout interval (in milliseconds)
|
|
void | maxconn (unsigned int new_max) |
| Set maximum number of connections accepted.
|
|
unsigned int | maxconn () const |
| Return maximum number of connections accepted.
|
|
void | set_connfunc (std::function< int(const sock &)> f) |
| Set function object that becomes the body of the thread serving a new connection.
|
|
| thread (std::function< unsigned int()> func) |
| Make a thread with the given function body.
|
|
virtual | ~thread () |
| Destructor.
|
|
virtual void | start () |
| Begin execution of a newly created thread.
|
|
void | fork () |
| Another name for start () function.
|
|
void | join () |
| Another name for wait () function.
|
|
DWORD | wait (DWORD time_limit=INFINITE) |
| Wait for thread to finish execution.
|
|
DWORD | wait_alertable (DWORD time_limit=INFINITE) |
| Wait for thread to finish or an APC, or IO completion routine to occur.
|
|
DWORD | wait_msg (DWORD time_limit=INFINITE, DWORD mask=QS_ALLINPUT) |
| Wait for thread to finish or a message to be queued.
|
|
void | rethrow_exception () const |
| Rethrow an exception usually in the context of another thread.
|
|
DWORD | id () const |
| Return thread's ID.
|
|
UINT | result () const |
| Return thread's exit code.
|
|
bool | is_running () const |
| Return true if thread is running.
|
|
state | get_state () const |
| Return thread's execution status.
|
|
int | priority () const |
| Return thread's priority.
|
|
void | priority (int pri) |
| Set thread's priority.
|
|
virtual const std::string & | name () const |
| Return object's name.
|
|
| syncbase () |
| Default constructor.
|
|
| syncbase (const syncbase &e) |
| Copy constructor.
|
|
| syncbase (syncbase &&e) noexcept |
| Move constructor.
|
|
virtual | ~syncbase () |
| Destructor.
|
|
syncbase & | operator= (const syncbase &rhs) |
| Assignment operator.
|
|
syncbase & | operator= (syncbase &&rhs) noexcept |
| Move assignment operator.
|
|
int | operator== (const syncbase &rhs) const |
| Equality operator.
|
|
virtual void | wait () |
| Wait for object to become signaled.
|
|
virtual DWORD | wait (std::chrono::milliseconds limit) |
| Wait a number of milliseconds for the object to become signaled.
|
|
| operator bool () |
| Check if object is signaled.
|
|
virtual bool | is_signaled () |
| Try to wait on the object.
|
|
HANDLE | handle () const |
| Return OS handle of this object.
|
|
|
int | invoke_handler (connection &client) |
| Invoke a user defined URI handler.
|
|
int | invoke_post_handler (connection &client) |
| Invoke a user defined handler in response to a POST or PUT request.
|
|
bool | find_alias (const std::string &res, std::filesystem::path &path) |
| Retrieve the local file path mapped to a resource.
|
|
virtual bool | locate_resource (const std::string &res, std::filesystem::path &path) |
| Try to map a resource to a local file.
|
|
const std::string & | guess_mimetype (const std::filesystem::path &fn, bool &shtml) |
| Guess MIME type of a file and if SSI replacement should be enabled based on file extension.
|
|
connection * | make_thread (sock &connection) |
| Create an new mlib::http::connection object in response to a new client connection request.
|
|
void | add_var (const std::string &name, vtype t, const void *addr, const char *fmt=nullptr, double multiplier=1.) |
| Add or modify a user variable.
|
|
bool | init () override |
| Binds the server socket to listening address and places it in listen mode.
|
|
void | run () override |
| Run loop.
|
|
virtual bool | idle_action () |
| Called periodically from run loop.
|
|
virtual void | initconn (sock &conn_sock, thread *thread) |
| Initializes a connection.
|
|
virtual void | termconn (sock &conn_sock, thread *thread) |
| Finalizes a connection.
|
|
| thread (const std::string &name=std::string(), DWORD stack_size=0, PSECURITY_DESCRIPTOR sd=NULL, bool inherit=false) |
| Protected constructor for use of thread-derived objects.
|
|
virtual void | term () |
| Finalization function called after run.
|
|
| syncbase (const std::string &name) |
| Protected constructor.
|
|
void | set_handle (HANDLE h) |
| Change object's handle. Closes the previous one.
|
|
Small multi-threaded HTTP server.
This class is derived from mlib::tcpserver class and implements a basic HTTP server.
After construction, the main server thread has to be started by calling start() function. When started, the server binds to the listening socket and creates new http::connection objects for each incoming client. All the protocol is then handled by the connection (or derived) object.
A HTTP server can be integrated to an application by adding specific handlers and/or user variables. Handlers are user functions called in response to client requests. There are two types of handlers:
- global handlers registered using the add_handler() function. These are called by the client connection thread immediately after validating a client request.
- post handlers registered using the add_post_handler() function. These are called only for POST or PUT requests.
User variables can be added by calling the add_var() function. The content of those variables is then returned in response to SSI echo directives.