PionNetworkLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pion::http::server Class Reference

#include <server.hpp>

Inheritance diagram for pion::http::server:
Collaboration diagram for pion::http::server:

Public Types

typedef boost::function2< void,
const http::request_ptr
&, const tcp::connection_ptr & > 
request_handler_t
 type of function that is used to handle requests More...
 
typedef boost::function3< void,
const http::request_ptr
&, const tcp::connection_ptr
&, const std::string & > 
error_handler_t
 handler for requests that result in "500 Server Error" More...
 

Public Member Functions

virtual ~server ()
 default destructor More...
 
 server (const unsigned int tcp_port=0)
 
 server (const boost::asio::ip::tcp::endpoint &endpoint)
 
 server (scheduler &sched, const unsigned int tcp_port=0)
 
 server (scheduler &sched, const boost::asio::ip::tcp::endpoint &endpoint)
 
void add_resource (const std::string &resource, request_handler_t request_handler)
 
void remove_resource (const std::string &resource)
 
void add_redirect (const std::string &requested_resource, const std::string &new_resource)
 
void set_bad_request_handler (request_handler_t h)
 sets the function that handles bad HTTP requests More...
 
void set_not_found_handler (request_handler_t h)
 sets the function that handles requests which match no other web services More...
 
void set_error_handler (error_handler_t h)
 sets the function that handles requests which match no other web services More...
 
virtual void clear (void)
 clears the collection of resources recognized by the HTTP server More...
 
void set_authentication (http::auth_ptr auth)
 
void set_max_content_length (std::size_t n)
 sets the maximum length for HTTP request payload content More...
 
- Public Member Functions inherited from pion::tcp::server
void start (void)
 starts listening for new connections More...
 
void stop (bool wait_until_finished=false)
 
void join (void)
 the calling thread will sleep until the server has stopped listening for connections More...
 
void set_ssl_key_file (const std::string &pem_key_file)
 
std::size_t get_connections (void) const
 returns the number of active tcp connections More...
 
unsigned int get_port (void) const
 returns tcp port number that the server listens for connections on More...
 
void set_port (unsigned int p)
 sets tcp port number that the server listens for connections on More...
 
boost::asio::ip::address get_address (void) const
 returns IP address that the server listens for connections on More...
 
void set_address (const boost::asio::ip::address &addr)
 sets IP address that the server listens for connections on More...
 
const
boost::asio::ip::tcp::endpoint & 
get_endpoint (void) const
 returns tcp endpoint that the server listens for connections on More...
 
void set_endpoint (const boost::asio::ip::tcp::endpoint &ep)
 sets tcp endpoint that the server listens for connections on More...
 
bool get_ssl_flag (void) const
 returns true if the server uses SSL to encrypt connections More...
 
void set_ssl_flag (bool b=true)
 sets value of SSL flag (true if the server uses SSL to encrypt connections) More...
 
connection::ssl_context_typeget_ssl_context_type (void)
 returns the SSL context for configuration More...
 
bool is_listening (void) const
 returns true if the server is listening for connections More...
 
void set_logger (logger log_ptr)
 sets the logger to be used More...
 
logger get_logger (void)
 returns the logger currently in use More...
 
boost::asio::ip::tcp::acceptor & get_acceptor (void)
 returns mutable reference to the TCP connection acceptor More...
 
const
boost::asio::ip::tcp::acceptor & 
get_acceptor (void) const
 returns const reference to the TCP connection acceptor More...
 

Static Public Member Functions

static std::string strip_trailing_slash (const std::string &str)
 
static void handle_bad_request (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn)
 
static void handle_not_found_request (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn)
 
static void handle_server_error (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn, const std::string &error_msg)
 
static void handle_forbidden_request (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn, const std::string &error_msg)
 
static void handle_method_not_allowed (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn, const std::string &allowed_methods="")
 

Protected Member Functions

virtual void handle_connection (const tcp::connection_ptr &tcp_conn)
 
virtual void handle_request (const http::request_ptr &http_request_ptr, const tcp::connection_ptr &tcp_conn, const boost::system::error_code &ec)
 
virtual bool find_request_handler (const std::string &resource, request_handler_t &request_handler) const
 
- Protected Member Functions inherited from pion::tcp::server
 server (const unsigned int tcp_port)
 
 server (const boost::asio::ip::tcp::endpoint &endpoint)
 
 server (scheduler &sched, const unsigned int tcp_port=0)
 
 server (scheduler &sched, const boost::asio::ip::tcp::endpoint &endpoint)
 
virtual void before_starting (void)
 called before the TCP server starts listening for new connections More...
 
virtual void after_stopping (void)
 called after the TCP server has stopped listing for new connections More...
 
boost::asio::io_service & get_io_service (void)
 returns an async I/O service used to schedule work More...
 

Additional Inherited Members

- Protected Attributes inherited from pion::tcp::server
logger m_logger
 primary logging interface used by this class More...
 

Detailed Description

server: a server that handles HTTP connections

Member Typedef Documentation

typedef boost::function3<void, const http::request_ptr&, const tcp::connection_ptr&, const std::string&> pion::http::server::error_handler_t

handler for requests that result in "500 Server Error"

typedef boost::function2<void, const http::request_ptr&, const tcp::connection_ptr&> pion::http::server::request_handler_t

type of function that is used to handle requests

Constructor & Destructor Documentation

virtual pion::http::server::~server ( )
inlinevirtual

default destructor

Reimplemented from pion::tcp::server.

pion::http::server::server ( const unsigned int  tcp_port = 0)
inlineexplicit

creates a new server object

Parameters
tcp_portport number used to listen for new connections (IPv4)

References PION_GET_LOGGER.

pion::http::server::server ( const boost::asio::ip::tcp::endpoint &  endpoint)
inlineexplicit

creates a new server object

Parameters
endpointTCP endpoint used to listen for new connections (see ASIO docs)

References PION_GET_LOGGER.

pion::http::server::server ( scheduler sched,
const unsigned int  tcp_port = 0 
)
inlineexplicit

creates a new server object

Parameters
schedthe scheduler that will be used to manage worker threads
tcp_portport number used to listen for new connections (IPv4)

References PION_GET_LOGGER.

pion::http::server::server ( scheduler sched,
const boost::asio::ip::tcp::endpoint &  endpoint 
)
inline

creates a new server object

Parameters
schedthe scheduler that will be used to manage worker threads
endpointTCP endpoint used to listen for new connections (see ASIO docs)

References PION_GET_LOGGER.

Member Function Documentation

void pion::http::server::add_redirect ( const std::string &  requested_resource,
const std::string &  new_resource 
)

adds a new resource redirection to the HTTP server

Parameters
requested_resourcethe resource name or uri-stem that will be redirected
new_resourcethe resource that requested_resource will be redirected to

References pion::tcp::server::m_logger, PION_LOG_INFO, and strip_trailing_slash().

void pion::http::server::add_resource ( const std::string &  resource,
request_handler_t  request_handler 
)

adds a new web service to the HTTP server

Parameters
resourcethe resource name or uri-stem to bind to the handler
request_handlerfunction used to handle requests to the resource

References pion::tcp::server::m_logger, PION_LOG_INFO, and strip_trailing_slash().

Referenced by pion::http::plugin_server::add_service(), and pion::http::plugin_server::load_service().

virtual void pion::http::server::clear ( void  )
inlinevirtual

clears the collection of resources recognized by the HTTP server

Reimplemented in pion::http::plugin_server.

Referenced by pion::http::plugin_server::clear().

bool pion::http::server::find_request_handler ( const std::string &  resource,
request_handler_t request_handler 
) const
protectedvirtual

searches for the appropriate request handler to use for a given resource

Parameters
resourcethe name of the resource to search for
request_handlerfunction that can handle requests for this resource

Referenced by handle_request().

void pion::http::server::handle_bad_request ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn 
)
static

used to send responses when a bad HTTP request is made

Parameters
http_request_ptrthe new HTTP request to handle
tcp_connthe TCP connection that has the new request

References pion::http::response_writer::create(), pion::tcp::connection::finish(), pion::http::types::RESPONSE_CODE_BAD_REQUEST, and pion::http::types::RESPONSE_MESSAGE_BAD_REQUEST.

void pion::http::server::handle_connection ( const tcp::connection_ptr tcp_conn)
protectedvirtual

handles a new TCP connection

Parameters
tcp_connthe new TCP connection to handle

Reimplemented from pion::tcp::server.

References pion::http::request_reader::create(), and handle_request().

void pion::http::server::handle_forbidden_request ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn,
const std::string &  error_msg 
)
static

used to send responses when a request is forbidden

Parameters
http_request_ptrthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
error_msgmessage that explains what went wrong

References pion::http::response_writer::create(), pion::tcp::connection::finish(), pion::http::types::RESPONSE_CODE_FORBIDDEN, pion::http::types::RESPONSE_MESSAGE_FORBIDDEN, and pion::algorithm::xml_encode().

void pion::http::server::handle_method_not_allowed ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn,
const std::string &  allowed_methods = "" 
)
static

used to send responses when a method is not allowed

Parameters
http_request_ptrthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
allowed_methodsoptional comma separated list of allowed methods

References pion::http::response_writer::create(), pion::tcp::connection::finish(), pion::http::types::RESPONSE_CODE_METHOD_NOT_ALLOWED, pion::http::types::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED, and pion::algorithm::xml_encode().

void pion::http::server::handle_not_found_request ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn 
)
static

used to send responses when no web services can handle the request

Parameters
http_request_ptrthe new HTTP request to handle
tcp_connthe TCP connection that has the new request

References pion::http::response_writer::create(), pion::tcp::connection::finish(), pion::http::types::RESPONSE_CODE_NOT_FOUND, pion::http::types::RESPONSE_MESSAGE_NOT_FOUND, and pion::algorithm::xml_encode().

void pion::http::server::handle_request ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn,
const boost::system::error_code &  ec 
)
protectedvirtual

handles a new HTTP request

Parameters
http_request_ptrthe HTTP request to handle
tcp_connTCP connection containing a new request
ecerror_code contains additional information for parsing errors

References pion::diagnostic_information(), find_request_handler(), pion::http::parser::get_error_category(), pion::tcp::server::get_port(), pion::tcp::connection::LIFECYCLE_CLOSE, pion::tcp::server::m_logger, PION_LOG_DEBUG, PION_LOG_ERROR, PION_LOG_INFO, and strip_trailing_slash().

Referenced by handle_connection().

void pion::http::server::handle_server_error ( const http::request_ptr http_request_ptr,
const tcp::connection_ptr tcp_conn,
const std::string &  error_msg 
)
static

used to send responses when a server error occurs

Parameters
http_request_ptrthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
error_msgmessage that explains what went wrong

References pion::http::response_writer::create(), pion::tcp::connection::finish(), pion::http::types::RESPONSE_CODE_SERVER_ERROR, pion::http::types::RESPONSE_MESSAGE_SERVER_ERROR, and pion::algorithm::xml_encode().

void pion::http::server::remove_resource ( const std::string &  resource)

removes a web service from the HTTP server

Parameters
resourcethe resource name or uri-stem to remove

References pion::tcp::server::m_logger, PION_LOG_INFO, and strip_trailing_slash().

void pion::http::server::set_authentication ( http::auth_ptr  auth)
inline

sets the handler object for authentication verification processing

Referenced by pion::http::plugin_server::load_service_config().

void pion::http::server::set_bad_request_handler ( request_handler_t  h)
inline

sets the function that handles bad HTTP requests

void pion::http::server::set_error_handler ( error_handler_t  h)
inline

sets the function that handles requests which match no other web services

void pion::http::server::set_max_content_length ( std::size_t  n)
inline

sets the maximum length for HTTP request payload content

void pion::http::server::set_not_found_handler ( request_handler_t  h)
inline

sets the function that handles requests which match no other web services

static std::string pion::http::server::strip_trailing_slash ( const std::string &  str)
inlinestatic

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