class http_basic_server - 5cript/attender GitHub Wiki

class http_basic_server

class http_basic_server
  : public attender::http_server_interface

The server base, that combines common functionality of all server types.

Summary

Members Descriptions
public http_basic_server(asio::io_service * service,error_callback on_error,settings setting) Creates a http_basic_server.
public ~http_basic_server() Destructor.
public virtual boost::asio::ip::tcp::endpoint get_local_endpoint() const Returns the local endpoint (port and network interface).
public virtual void start(std::string const & port,std::string const & host) Starts the server on the port (0-65536 or "http", abides boost asio behaviour) on the interface "host".
public virtual void stop() Stops the server and terminates all connections.
public virtual settings get_settings() const Returns a copy of the server settings.
public void install_session_control(SessionControlParam && controlParam) After calling this function, every single request is checked for an active authorized session.
public bool authenticate_session(request_handler * req,response_handler * res) Will try to perform authentication, if a session control is installed.
public std::weak_ptr< session_manager > get_installed_session_manager() Retrieve a weak ptr to the session manager.
public void get(std::string const & path_template,connected_callback const & on_connect) Will add a routing for get requests.
public void put(std::string const & path_template,connected_callback const & on_connect) Will add a routing for put requests.
public void post(std::string const & path_template,connected_callback const & on_connect) Will add a routing for post requests.
public void head(std::string const & path_template,connected_callback const & on_connect) Will add a routing for head requests.
public void delete_(std::string const & path_template,connected_callback const & on_connect) Will add a routing for delete_ requests.
public void options(std::string const & path_template,connected_callback const & on_connect) Will add a routing for options requests.
public void connect(std::string const & path_template,connected_callback const & on_connect) Will add a routing for connect requests.
public void route(std::string const & route_name,std::string const & path_template,connected_callback const & on_connect) Will add a routing for a custom requests method string (they cannot contain spaces).
public virtual connection_manager * get_connections() Returns a pointer to the connection manager.
public session_manager * get_session_manager() Returns session manager, if one was installed.
public void mount(std::string const & root_path,std::string const & path_template,mount_callback_2 const & on_connect,mount_option_set const & supported_methods,int priority) mounts a path on the local system, so it can be accessed by http requests.
public void mount(std::string const & root_path,std::string const & path_template,mount_callback const & on_connect,mount_option_set const & supported_methods)
protected asio::io_service * service_
protected boost::asio::ip::tcp::acceptor acceptor_
protected boost::asio::ip::tcp::endpoint local_endpoint_
protected connection_manager connections_
protected request_router router_
protected settings settings_
protected error_callback on_error_
protected missing_handler_callback on_missing_handler_
protected SessionControl sessionControl_
protected void do_accept()
protected bool handle_session(request_handler * req,response_handler * res) Returns false if session is unauthorized to proceed.
protected void header_read_handler(request_handler * req,response_handler * res,http_connection_interface * connection,boost::system::error_code ec,std::exception const & exc)

Members


public http_basic_server(asio::io_service * service,error_callback on_error,settings setting)

Creates a http_basic_server.

Parameters

  • the boost::asio::io_service. Every server needs to know it's io_service (see boost asio documentation)

  • error_callback The error callback is called whenever an exception is thrown somewhere inside the server or connection internals. The affected connection will be closed, or the server stopped, if thrown outside of a connection context.

  • settings The settings of the server.

See also: attender::settings


public ~http_basic_server()

Destructor.


public virtual boost::asio::ip::tcp::endpoint get_local_endpoint() const

Returns the local endpoint (port and network interface).


public virtual void start(std::string const & port,std::string const & host)

Starts the server on the port (0-65536 or "http", abides boost asio behaviour) on the interface "host".

Parameters

  • port The port to bind to.

  • host The target interface. "0.0.0.0" is the default value for ipv4 and "::" for ipv6.


public virtual void stop()

Stops the server and terminates all connections.

What happens to connections amidst transactions is undefined. Operations will be aborted and streams closed.


public virtual settings get_settings() const

Returns a copy of the server settings.


public void install_session_control(SessionControlParam && controlParam)

After calling this function, every single request is checked for an active authorized session.

Authorization can be performed on any request.

Parameters

  • session_storage A session storage to take ownership of

  • authorizer An authorization handler

  • id_cookie_key cookie key to use for authorization

  • allowOptionsUnauthorized allow OPTIONS requests without authorization.

  • authorization_conditioner A function applied during authorization. Can be used to allow CORS.


public bool authenticate_session(request_handler * req,response_handler * res)

Will try to perform authentication, if a session control is installed.

Returns false if session is unauthorized to proceed.


public std::weak_ptr< session_manager > get_installed_session_manager()

Retrieve a weak ptr to the session manager.


public void get(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for get requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void put(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for put requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void post(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for post requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void head(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for head requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void delete_(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for delete_ requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void options(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for options requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void connect(std::string const & path_template,connected_callback const & on_connect)

Will add a routing for connect requests.

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public void route(std::string const & route_name,std::string const & path_template,connected_callback const & on_connect)

Will add a routing for a custom requests method string (they cannot contain spaces).

Parameters

  • path_template A template for paths. These templates will be parsed and if a match occurs in a request, the routing will be used.

  • connect_callback A callback which gets called upon a request is received, that matches the path_template.


public virtual connection_manager * get_connections()

Returns a pointer to the connection manager.

It holds all active connections.

Returns: Returns a pointer to the connection_manager.


public session_manager * get_session_manager()

Returns session manager, if one was installed.

Do note, that installing one isn't required.


public void mount(std::string const & root_path,std::string const & path_template,mount_callback_2 const & on_connect,mount_option_set const & supported_methods,int priority)

mounts a path on the local system, so it can be accessed by http requests.

Any requests on that path will result in corresponding actions that can be enabled or disabled.

BY DEFAULT, ONLY GET, HEAD AND OPTIONS ARE ENABLED. A get request will load and transfer the file if it exists, 404 is returned otherwise. A put/post will create a file and fill it with the sent data. A delete request will delete the file specified A head request will only read file properties, but not transfer the file. An options request will reply with "get, put, post, delete, head" A connect request, or any other custom request, will not be routed.

The on_connect handler will be called prior to the execution, so the option exists to deny or abort request. After the handler finished, the return value will be checked. A return value of false will mean, that the connection is to be aborted. Do not close the connection on your or do a send operation on it.

All requests will be jailed to the path, but include subdirectories, except if disabled.

Parameters

  • root_path The path to jail to.

  • path_template Presume root_path is /home/user/bla and path_template is /home, then requests to /home will be redirected to /home/user/bla

  • on_connect A handler called before executing operations. The handler may return false, if e.g. dubious / unauthorized.

  • priority A priority for the mount route. Its usually beneficial to have one below 0 so that non-mount routes get preferred.


public void mount(std::string const & root_path,std::string const & path_template,mount_callback const & on_connect,mount_option_set const & supported_methods)


protected asio::io_service * service_


protected boost::asio::ip::tcp::acceptor acceptor_


protected boost::asio::ip::tcp::endpoint local_endpoint_


protected connection_manager connections_


protected request_router router_


protected settings settings_


protected error_callback on_error_


protected missing_handler_callback on_missing_handler_


protected SessionControl sessionControl_


protected void do_accept()


protected bool handle_session(request_handler * req,response_handler * res)

Returns false if session is unauthorized to proceed.


protected void header_read_handler(request_handler * req,response_handler * res,http_connection_interface * connection,boost::system::error_code ec,std::exception const & exc)