MLIB
|
Representation of a HTTP client connection request. More...
#include <httpd.h>
Public Member Functions | |
const std::string & | get_path () const |
Return request target of this connection. | |
const std::string & | get_method () const |
Return HTTP method (GET, POST, etc.) of the request. | |
const std::string & | get_query () const |
Return request query string (everything after '?' and before '#') | |
const std::string & | get_body () const |
Return request body. | |
void | add_ohdr (const std::string &hdr, const std::string &value) |
Add or modify a response header. | |
bool | has_ihdr (const std::string &hdr) const |
Check if request has a header. | |
const std::string & | get_ihdr (const std::string &hdr) const |
Return the value of a request header. | |
const str_pairs & | get_request_headers () const |
Return all request headers. | |
bool | has_ohdr (const std::string &hdr) const |
Check if response has a header. | |
const std::string & | get_ohdr (const std::string &hdr) const |
Return the value of a response header. | |
bool | has_qparam (const std::string &key) |
Check if query has a parameter. | |
const std::string & | get_qparam (const std::string &key) |
Return the value of a query parameter. | |
bool | has_bparam (const std::string &key) |
Return true if request contains the given parameter in the request body. | |
const std::string & | get_bparam (const std::string &key) |
Return the value of a body parameter. | |
int | get_content_length () const |
Return size of request body. | |
sockstream & | out () |
Return socket object associated with this connection. | |
void | respond (unsigned int code, const std::string &reason=std::string()) |
Send the beginning of HTTP response. | |
void | redirect (const std::string &uri, unsigned int code=303) |
Generate a HTTP redirect response to a new resource. | |
void | serve404 (const char *text=0) |
Sends a 404 (page not found) response. | |
int | serve_file (const std::filesystem::path &file) |
Send the content of a file. | |
int | serve_shtml (const std::filesystem::path &file) |
Send the content of a file, processing any SSI directives. | |
int | serve_buffer (const BYTE *buffer, size_t sz) |
Send the content of a buffer. | |
int | serve_buffer (const std::string &str) |
Send the content of a string. | |
![]() | |
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. | |
Protected Member Functions | |
connection (sock &socket, server &server) | |
Protected constructor used by mlib::http::server class to create a new connection thread. | |
void | run () override |
The thread run loop. | |
void | term () override |
Finalization function called after run. | |
![]() | |
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 bool | init () |
Initialization function called before run. | |
![]() | |
syncbase (const std::string &name) | |
Protected constructor. | |
void | set_handle (HANDLE h) |
Change object's handle. Closes the previous one. | |
Protected Attributes | |
server & | parent |
sockstream | ws |
![]() | |
unsigned int | exitcode |
exit code | |
Friends | |
class | server |
Additional Inherited Members | |
![]() | |
enum class | state { ready , starting , running , ending , finished } |
Execution state of a thread. More... | |
Representation of a HTTP client connection request.
This is the thread created by the mlib::http::server object in response to a new connection request.
The thread is started automatically by the server. It listens on the connection socket and process HTTP requests. When the HTTP client closes the connection the thread stops and the object is destructed by the server.
Users can create derived classes but the parent server must also be a class derived from http::server with an overridden http::server::make_thread function.
For details of request processing, see the run() function.
Most of the public functions are designed for the benefit of user handler functions. The handler function, if one has been registered, is called before sending any reply to the HTTP client.
Protected constructor used by mlib::http::server class to create a new connection thread.
socket | connecting socket |
server | parent server object |
As it has only a protected constructor, it is not possible for users of this class to directly create this object. Derived classes should maintain this convention.
|
inline |
Add or modify a response header.
hdr | header name |
value | header value |
To have any effect, this function should be called before calling the respond() (or serve_...) function as all headers are sent at that time.
|
inline |
Return request body.
|
inline |
Return the value of a body parameter.
Only requests with content in URL-encoded format can be parsed.
key | form parameter |
Throws an exception of type std::out_of_range
if the form doesn't have the parameter.
|
inline |
Return size of request body.
If the request is a POST or PUT request without a "Content-Length" header, the request is rejected with a response code 400.
|
inline |
Return the value of a request header.
hdr | header name |
Throws an exception of type std::out_of_range
if request doesn't have the header
|
inline |
Return HTTP method (GET, POST, etc.) of the request.
Returns the <method>
component of the request line.
The general structure of a HTTP request line is:
|
inline |
Return the value of a response header.
The header can belong either to server or to connection.
hdr | header name |
Throws an exception of type std::out_of_range
if the response doesn't have that header.
|
inline |
Return request target of this connection.
Returns the <target path>
component of the request line.
The general structure of a HTTP request line is:
Only origin-form
(see RFC9112) is accepted.
|
inline |
Return the value of a query parameter.
key | query parameter |
Throws an exception of type std::out_of_range
if the query doesn't have the parameter.
Query parameters and their values are URL-decoded before being processed.
|
inline |
Return request query string (everything after '?' and before '#')
Returns the query
component from the request line.
The general structure of a HTTP request line is:
|
inline |
Return true if request contains the given parameter in the request body.
Return true
if request body contains the parameter.
Request body is URL-decoded before being processed.
|
inline |
Check if request has a header.
Return true
if request has the header.
|
inline |
Check if response has a header.
Return ‘true’ if either the server or the connection has added the header to the request response.
|
inline |
Check if query has a parameter.
Return true
if the query contains the given parameter.
Query parameters and their values are URL-decoded before being processed.
void mlib::http::connection::redirect | ( | const std::string & | uri, |
unsigned int | code = 303 ) |
Generate a HTTP redirect response to a new resource.
uri | path for new resource |
code | redirect code |
void mlib::http::connection::respond | ( | unsigned int | code, |
const std::string & | reason = std::string() ) |
Send the beginning of HTTP response.
code | HTTP response code |
reason | reason-phrase. If empty a standard reason-phrase is used. |
First time the function sends the status line and headers of the HTTP response:
HTTP/1.1 <code> <text> ... server headers ... connection headers
In subsequent calls it sends only connection headers (to support multi-part responses)
|
overrideprotectedvirtual |
The thread run loop.
The loop has the following steps:
Reimplemented from mlib::thread.
int mlib::http::connection::serve_buffer | ( | const BYTE * | src_buff, |
size_t | sz ) |
Send the content of a buffer.
src_buff | buffer to send |
sz | buffer length |
HTTP_ERR_WRITE | - socket write failure |
The buffer is preceded by "Content-Length" header.
int mlib::http::connection::serve_buffer | ( | const std::string & | str | ) |
Send the content of a string.
str | string to send |
HTTP_ERR_WRITE | - socket write failure |
The string is preceded by "Content-Length" header.
int mlib::http::connection::serve_file | ( | const std::filesystem::path & | file | ) |
Send the content of a file.
file | file name |
HTTP_ERR_WRITE | - socket write failure |
HTTP_ERR_FOPEN | - file open failure |
HTTP_ERR_FREAD | - file read failure |
The file is preceded by "Content-Length" header.
int mlib::http::connection::serve_shtml | ( | const std::filesystem::path & | file | ) |
Send the content of a file, processing any SSI directives.
file | file name |
-1 | socket write failure |
-2 | file open error |
As we don't know the size of the response, the file is sent without a "Content-Length" header.
|
overrideprotectedvirtual |
Finalization function called after run.
Reimplemented from mlib::thread.