|
| 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.
|
|
bool | add_user (const char *realm, const char *username, const char *pwd) |
| Add a new user to a relm or modifies password for an existing user.
|
|
bool | remove_user (const char *realm, const char *username) |
|
void | add_realm (const char *realm, const char *uri) |
| Add a new access 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 char *nam) |
| Change server name.
|
|
void | docroot (const std::string &path) |
| Set server root path.
|
|
const std::filesystem::path & | docroot () const |
| Return current file origin.
|
|
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) |
|
const std::string & | default_uri () |
|
bool | is_protected (const std::string &uri, std::string &realm) |
| Find the realm with longest matching path that covers an URI.
|
|
bool | authenticate (const std::string &realm, const std::string &user, const std::string &pwd) |
| Verify user credentials for a realm.
|
|
| 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 void | name (const std::string &nam) |
| Set thread's name.
|
|
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.
|
|
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 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.