Project Alice
Loading...
Searching...
No Matches
httplib::Server Class Reference

#include <httplib.h>

Public Types

enum class  HandlerResponse { Handled , Unhandled }
 
using Handler = std::function< void(const Request &, Response &)>
 
using ExceptionHandler = std::function< void(const Request &, Response &, std::exception_ptr ep)>
 
using HandlerWithResponse = std::function< HandlerResponse(const Request &, Response &)>
 
using HandlerWithContentReader = std::function< void(const Request &, Response &, const ContentReader &content_reader)>
 
using Expect100ContinueHandler = std::function< int(const Request &, Response &)>
 

Public Member Functions

 Server ()
 
virtual ~Server ()
 
virtual bool is_valid () const
 
ServerGet (const std::string &pattern, Handler handler)
 
ServerPost (const std::string &pattern, Handler handler)
 
ServerPost (const std::string &pattern, HandlerWithContentReader handler)
 
ServerPut (const std::string &pattern, Handler handler)
 
ServerPut (const std::string &pattern, HandlerWithContentReader handler)
 
ServerPatch (const std::string &pattern, Handler handler)
 
ServerPatch (const std::string &pattern, HandlerWithContentReader handler)
 
ServerDelete (const std::string &pattern, Handler handler)
 
ServerDelete (const std::string &pattern, HandlerWithContentReader handler)
 
ServerOptions (const std::string &pattern, Handler handler)
 
bool set_base_dir (const std::string &dir, const std::string &mount_point=std::string())
 
bool set_mount_point (const std::string &mount_point, const std::string &dir, Headers headers=Headers())
 
bool remove_mount_point (const std::string &mount_point)
 
Serverset_file_extension_and_mimetype_mapping (const std::string &ext, const std::string &mime)
 
Serverset_default_file_mimetype (const std::string &mime)
 
Serverset_file_request_handler (Handler handler)
 
template<class ErrorHandlerFunc >
Serverset_error_handler (ErrorHandlerFunc &&handler)
 
Serverset_exception_handler (ExceptionHandler handler)
 
Serverset_pre_routing_handler (HandlerWithResponse handler)
 
Serverset_post_routing_handler (Handler handler)
 
Serverset_expect_100_continue_handler (Expect100ContinueHandler handler)
 
Serverset_logger (Logger logger)
 
Serverset_address_family (int family)
 
Serverset_tcp_nodelay (bool on)
 
Serverset_ipv6_v6only (bool on)
 
Serverset_socket_options (SocketOptions socket_options)
 
Serverset_default_headers (Headers headers)
 
Serverset_header_writer (std::function< ssize_t(Stream &, Headers &)> const &writer)
 
Serverset_keep_alive_max_count (size_t count)
 
Serverset_keep_alive_timeout (time_t sec)
 
Serverset_read_timeout (time_t sec, time_t usec=0)
 
template<class Rep , class Period >
Serverset_read_timeout (const std::chrono::duration< Rep, Period > &duration)
 
Serverset_write_timeout (time_t sec, time_t usec=0)
 
template<class Rep , class Period >
Serverset_write_timeout (const std::chrono::duration< Rep, Period > &duration)
 
Serverset_idle_interval (time_t sec, time_t usec=0)
 
template<class Rep , class Period >
Serverset_idle_interval (const std::chrono::duration< Rep, Period > &duration)
 
Serverset_payload_max_length (size_t length)
 
bool bind_to_port (const std::string &host, int port, int socket_flags=0)
 
int bind_to_any_port (const std::string &host, int socket_flags=0)
 
bool listen_after_bind ()
 
bool listen (const std::string &host, int port, int socket_flags=0)
 
bool start_server (const std::string &host, int port, int socket_flags=0)
 
bool is_running () const
 
void wait_until_ready () const
 
void stop ()
 
void decommission ()
 

Public Attributes

std::function< TaskQueue *(void)> new_task_queue
 

Protected Member Functions

bool process_request (Stream &strm, const std::string &remote_addr, int remote_port, const std::string &local_addr, int local_port, bool close_connection, bool &connection_closed, const std::function< void(Request &)> &setup_request)
 

Protected Attributes

std::atomic< socket_tsvr_sock_ {INVALID_SOCKET}
 
size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT
 
time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND
 
time_t read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND
 
time_t read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND
 
time_t write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND
 
time_t write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND
 
time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND
 
time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND
 
size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH
 

Detailed Description

Definition at line 919 of file httplib.h.

Member Typedef Documentation

◆ ExceptionHandler

using httplib::Server::ExceptionHandler = std::function<void(const Request &, Response &, std::exception_ptr ep)>

Definition at line 923 of file httplib.h.

◆ Expect100ContinueHandler

using httplib::Server::Expect100ContinueHandler = std::function<int(const Request &, Response &)>

Definition at line 936 of file httplib.h.

◆ Handler

using httplib::Server::Handler = std::function<void(const Request &, Response &)>

Definition at line 921 of file httplib.h.

◆ HandlerWithContentReader

using httplib::Server::HandlerWithContentReader = std::function<void( const Request &, Response &, const ContentReader &content_reader)>

Definition at line 933 of file httplib.h.

◆ HandlerWithResponse

Definition at line 930 of file httplib.h.

Member Enumeration Documentation

◆ HandlerResponse

Enumerator
Handled 
Unhandled 

Definition at line 926 of file httplib.h.

Constructor & Destructor Documentation

◆ Server()

httplib::Server::Server ( )
inline

Definition at line 6085 of file httplib.h.

◆ ~Server()

httplib::Server::~Server ( )
inlinevirtualdefault

Member Function Documentation

◆ bind_to_any_port()

int httplib::Server::bind_to_any_port ( const std::string &  host,
int  socket_flags = 0 
)
inline

Definition at line 6318 of file httplib.h.

◆ bind_to_port()

bool httplib::Server::bind_to_port ( const std::string &  host,
int  port,
int  socket_flags = 0 
)
inline

Definition at line 6312 of file httplib.h.

Here is the caller graph for this function:

◆ decommission()

void httplib::Server::decommission ( )
inline

Definition at line 6354 of file httplib.h.

◆ Delete() [1/2]

Server & httplib::Server::Delete ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6145 of file httplib.h.

◆ Delete() [2/2]

Server & httplib::Server::Delete ( const std::string &  pattern,
HandlerWithContentReader  handler 
)
inline

Definition at line 6150 of file httplib.h.

◆ Get()

Server & httplib::Server::Get ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6104 of file httplib.h.

Here is the caller graph for this function:

◆ is_running()

bool httplib::Server::is_running ( ) const
inline

Definition at line 6336 of file httplib.h.

◆ is_valid()

bool httplib::Server::is_valid ( ) const
inlinevirtual

Definition at line 7322 of file httplib.h.

◆ listen()

bool httplib::Server::listen ( const std::string &  host,
int  port,
int  socket_flags = 0 
)
inline

Definition at line 6326 of file httplib.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ listen_after_bind()

bool httplib::Server::listen_after_bind ( )
inline

Definition at line 6324 of file httplib.h.

◆ Options()

Server & httplib::Server::Options ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6157 of file httplib.h.

◆ Patch() [1/2]

Server & httplib::Server::Patch ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6133 of file httplib.h.

◆ Patch() [2/2]

Server & httplib::Server::Patch ( const std::string &  pattern,
HandlerWithContentReader  handler 
)
inline

Definition at line 6138 of file httplib.h.

◆ Post() [1/2]

Server & httplib::Server::Post ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6109 of file httplib.h.

◆ Post() [2/2]

Server & httplib::Server::Post ( const std::string &  pattern,
HandlerWithContentReader  handler 
)
inline

Definition at line 6114 of file httplib.h.

◆ process_request()

bool httplib::Server::process_request ( Stream strm,
const std::string &  remote_addr,
int  remote_port,
const std::string &  local_addr,
int  local_port,
bool  close_connection,
bool &  connection_closed,
const std::function< void(Request &)> &  setup_request 
)
inlineprotected

Definition at line 7145 of file httplib.h.

Here is the call graph for this function:

◆ Put() [1/2]

Server & httplib::Server::Put ( const std::string &  pattern,
Handler  handler 
)
inline

Definition at line 6121 of file httplib.h.

◆ Put() [2/2]

Server & httplib::Server::Put ( const std::string &  pattern,
HandlerWithContentReader  handler 
)
inline

Definition at line 6126 of file httplib.h.

◆ remove_mount_point()

bool httplib::Server::remove_mount_point ( const std::string &  mount_point)
inline

Definition at line 6180 of file httplib.h.

◆ set_address_family()

Server & httplib::Server::set_address_family ( int  family)
inline

Definition at line 6248 of file httplib.h.

◆ set_base_dir()

bool httplib::Server::set_base_dir ( const std::string &  dir,
const std::string &  mount_point = std::string() 
)
inline

Definition at line 6162 of file httplib.h.

Here is the call graph for this function:

◆ set_default_file_mimetype()

Server & httplib::Server::set_default_file_mimetype ( const std::string &  mime)
inline

Definition at line 6197 of file httplib.h.

◆ set_default_headers()

Server & httplib::Server::set_default_headers ( Headers  headers)
inline

Definition at line 6268 of file httplib.h.

◆ set_error_handler()

template<class ErrorHandlerFunc >
Server & httplib::Server::set_error_handler ( ErrorHandlerFunc &&  handler)
inline

Definition at line 967 of file httplib.h.

◆ set_exception_handler()

Server & httplib::Server::set_exception_handler ( ExceptionHandler  handler)
inline

Definition at line 6222 of file httplib.h.

◆ set_expect_100_continue_handler()

Server & httplib::Server::set_expect_100_continue_handler ( Expect100ContinueHandler  handler)
inline

Definition at line 6243 of file httplib.h.

◆ set_file_extension_and_mimetype_mapping()

Server & httplib::Server::set_file_extension_and_mimetype_mapping ( const std::string &  ext,
const std::string &  mime 
)
inline

Definition at line 6191 of file httplib.h.

◆ set_file_request_handler()

Server & httplib::Server::set_file_request_handler ( Handler  handler)
inline

Definition at line 6202 of file httplib.h.

◆ set_header_writer()

Server & httplib::Server::set_header_writer ( std::function< ssize_t(Stream &, Headers &)> const &  writer)
inline

Definition at line 6273 of file httplib.h.

◆ set_idle_interval() [1/2]

template<class Rep , class Period >
Server & httplib::Server::set_idle_interval ( const std::chrono::duration< Rep, Period > &  duration)
inline

Definition at line 2167 of file httplib.h.

Here is the call graph for this function:

◆ set_idle_interval() [2/2]

Server & httplib::Server::set_idle_interval ( time_t  sec,
time_t  usec = 0 
)
inline

Definition at line 6301 of file httplib.h.

Here is the caller graph for this function:

◆ set_ipv6_v6only()

Server & httplib::Server::set_ipv6_v6only ( bool  on)
inline

Definition at line 6258 of file httplib.h.

◆ set_keep_alive_max_count()

Server & httplib::Server::set_keep_alive_max_count ( size_t  count)
inline

Definition at line 6279 of file httplib.h.

◆ set_keep_alive_timeout()

Server & httplib::Server::set_keep_alive_timeout ( time_t  sec)
inline

Definition at line 6284 of file httplib.h.

◆ set_logger()

Server & httplib::Server::set_logger ( Logger  logger)
inline

Definition at line 6237 of file httplib.h.

◆ set_mount_point()

bool httplib::Server::set_mount_point ( const std::string &  mount_point,
const std::string &  dir,
Headers  headers = Headers() 
)
inline

Definition at line 6167 of file httplib.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_payload_max_length()

Server & httplib::Server::set_payload_max_length ( size_t  length)
inline

Definition at line 6307 of file httplib.h.

◆ set_post_routing_handler()

Server & httplib::Server::set_post_routing_handler ( Handler  handler)
inline

Definition at line 6232 of file httplib.h.

◆ set_pre_routing_handler()

Server & httplib::Server::set_pre_routing_handler ( HandlerWithResponse  handler)
inline

Definition at line 6227 of file httplib.h.

◆ set_read_timeout() [1/2]

template<class Rep , class Period >
Server & httplib::Server::set_read_timeout ( const std::chrono::duration< Rep, Period > &  duration)
inline

Definition at line 2151 of file httplib.h.

Here is the call graph for this function:

◆ set_read_timeout() [2/2]

Server & httplib::Server::set_read_timeout ( time_t  sec,
time_t  usec = 0 
)
inline

Definition at line 6289 of file httplib.h.

Here is the caller graph for this function:

◆ set_socket_options()

Server & httplib::Server::set_socket_options ( SocketOptions  socket_options)
inline

Definition at line 6263 of file httplib.h.

◆ set_tcp_nodelay()

Server & httplib::Server::set_tcp_nodelay ( bool  on)
inline

Definition at line 6253 of file httplib.h.

◆ set_write_timeout() [1/2]

template<class Rep , class Period >
Server & httplib::Server::set_write_timeout ( const std::chrono::duration< Rep, Period > &  duration)
inline

Definition at line 2159 of file httplib.h.

Here is the call graph for this function:

◆ set_write_timeout() [2/2]

Server & httplib::Server::set_write_timeout ( time_t  sec,
time_t  usec = 0 
)
inline

Definition at line 6295 of file httplib.h.

Here is the caller graph for this function:

◆ start_server()

bool httplib::Server::start_server ( const std::string &  host,
int  port,
int  socket_flags = 0 
)
inline

Definition at line 6331 of file httplib.h.

Here is the call graph for this function:

◆ stop()

void httplib::Server::stop ( )
inline

Definition at line 6344 of file httplib.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ wait_until_ready()

void httplib::Server::wait_until_ready ( ) const
inline

Definition at line 6338 of file httplib.h.

Member Data Documentation

◆ idle_interval_sec_

time_t httplib::Server::idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND
protected

Definition at line 1035 of file httplib.h.

◆ idle_interval_usec_

time_t httplib::Server::idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND
protected

Definition at line 1036 of file httplib.h.

◆ keep_alive_max_count_

size_t httplib::Server::keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT
protected

Definition at line 1029 of file httplib.h.

◆ keep_alive_timeout_sec_

time_t httplib::Server::keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND
protected

Definition at line 1030 of file httplib.h.

◆ new_task_queue

std::function<TaskQueue *(void)> httplib::Server::new_task_queue

Definition at line 1019 of file httplib.h.

◆ payload_max_length_

size_t httplib::Server::payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH
protected

Definition at line 1037 of file httplib.h.

◆ read_timeout_sec_

time_t httplib::Server::read_timeout_sec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_SECOND
protected

Definition at line 1031 of file httplib.h.

◆ read_timeout_usec_

time_t httplib::Server::read_timeout_usec_ = CPPHTTPLIB_SERVER_READ_TIMEOUT_USECOND
protected

Definition at line 1032 of file httplib.h.

◆ svr_sock_

std::atomic<socket_t> httplib::Server::svr_sock_ {INVALID_SOCKET}
protected

Definition at line 1028 of file httplib.h.

◆ write_timeout_sec_

time_t httplib::Server::write_timeout_sec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_SECOND
protected

Definition at line 1033 of file httplib.h.

◆ write_timeout_usec_

time_t httplib::Server::write_timeout_usec_ = CPPHTTPLIB_SERVER_WRITE_TIMEOUT_USECOND
protected

Definition at line 1034 of file httplib.h.


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