pion::http::server Class Reference

`#include <server.hpp>`

Inherits pion::tcp::server.

Inherited by pion::http::plugin_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
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"

## Public Member Functions

virtual | ~server ()
| default destructor
| 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
void | set_not_found_handler (request_handler_t h)
| sets the function that handles requests which match no other web services
void | set_error_handler (error_handler_t h)
| sets the function that handles requests which match no other web services
virtual void | clear (void)
| clears the collection of resources recognized by the HTTP server
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
Public Member Functions inherited from pion::tcp::server
void | start (void)
| starts listening for new connections
void | stop (bool wait_until_finished=false)
void | join (void)
| the calling thread will sleep until the server has stopped listening for connections
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
unsigned int | get_port (void) const
| returns tcp port number that the server listens for connections on
void | set_port (unsigned int p)
| sets tcp port number that the server listens for connections on
boost::asio::ip::address | get_address (void) const
| returns IP address that the server listens for connections on
void | set_address (const boost::asio::ip::address &addr)
| sets IP address that the server listens for connections on
const boost::asio::ip::tcp::endpoint & | get_endpoint (void) const
| returns tcp endpoint that the server listens for connections on
void | set_endpoint (const boost::asio::ip::tcp::endpoint &ep)
| sets tcp endpoint that the server listens for connections on
bool | get_ssl_flag (void) const
| returns true if the server uses SSL to encrypt connections
void | set_ssl_flag (bool b=true)
| sets value of SSL flag (true if the server uses SSL to encrypt connections)
connection::ssl_context_type & | get_ssl_context_type (void)
| returns the SSL context for configuration
bool | is_listening (void) const
| returns true if the server is listening for connections
void | set_logger (logger log_ptr)
| sets the logger to be used
logger | get_logger (void)
| returns the logger currently in use
boost::asio::ip::tcp::acceptor & | get_acceptor (void)
| returns mutable reference to the TCP connection acceptor
const boost::asio::ip::tcp::acceptor & | get_acceptor (void) const
| returns const reference to the TCP connection acceptor

## 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
virtual void | after_stopping (void)
| called after the TCP server has stopped listing for new connections
boost::asio::io_service & | get_io_service (void)
| returns an async I/O service used to schedule work

## Additional Inherited Members

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

## Detailed Description

server: a server that handles HTTP connections

Definition at line 36 of file server.hpp.

## Constructor & Destructor Documentation

## server() [1/4]

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

inline explicit

creates a new server object

Parameters

  tcp_port | port number used to listen for new connections (IPv4)

Definition at line 58 of file server.hpp.

## server() [2/4]

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

inline explicit

creates a new server object

Parameters

  endpoint | TCP endpoint used to listen for new connections (see ASIO docs)

Definition at line 73 of file server.hpp.

## server() [3/4]

pion::http::server::server | ( | scheduler & | sched,
|  | const unsigned int | tcp_port = `0`
| ) |  |

inline explicit

creates a new server object

Parameters

  sched | the scheduler that will be used to manage worker threads
  tcp_port | port number used to listen for new connections (IPv4)

Definition at line 89 of file server.hpp.

## server() [4/4]

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

inline

creates a new server object

Parameters

  sched | the scheduler that will be used to manage worker threads
  endpoint | TCP endpoint used to listen for new connections (see ASIO docs)

Definition at line 105 of file server.hpp.

## Member Function Documentation

## add_redirect()

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_resource | the resource name or uri-stem that will be redirected
  new_resource | the resource that requested_resource will be redirected to

Definition at line 177 of file http_server.cpp.

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

## add_resource()

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

  resource | the resource name or uri-stem to bind to the handler
  request_handler | function used to handle requests to the resource

Definition at line 160 of file http_server.cpp.

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

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

## find_request_handler()

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

protected virtual

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

Parameters

  resource | the name of the resource to search for
  request_handler | function that can handle requests for this resource

Definition at line 134 of file http_server.cpp.

Referenced by handle_request().

## handle_bad_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_ptr | the new HTTP request to handle
  tcp_conn | the TCP connection that has the new request

Definition at line 187 of file http_server.cpp.

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

## handle_connection()

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

protected virtual

handles a new TCP connection

Parameters

  tcp_conn | the new TCP connection to handle

Reimplemented from pion::tcp::server.

Definition at line 29 of file http_server.cpp.

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

## handle_forbidden_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_ptr | the new HTTP request to handle
  tcp_conn | the TCP connection that has the new request
  error_msg | message that explains what went wrong

Definition at line 250 of file http_server.cpp.

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

## handle_method_not_allowed()

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_ptr | the new HTTP request to handle
  tcp_conn | the TCP connection that has the new request
  allowed_methods | optional comma separated list of allowed methods

Definition at line 277 of file http_server.cpp.

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

## handle_not_found_request()

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_ptr | the new HTTP request to handle
  tcp_conn | the TCP connection that has the new request

Definition at line 205 of file http_server.cpp.

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

## handle_request()

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
| ) |  |

protected virtual

handles a new HTTP request

Parameters

  http_request_ptr | the HTTP request to handle
  tcp_conn | TCP connection containing a new request
  ec | error_code contains additional information for parsing errors

Definition at line 38 of file http_server.cpp.

References find_request_handler(), pion::http::parser::get_error_category(), pion::tcp::server::get_port(), pion::tcp::server::m_logger, and strip_trailing_slash().

Referenced by handle_connection().

## handle_server_error()

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_ptr | the new HTTP request to handle
  tcp_conn | the TCP connection that has the new request
  error_msg | message that explains what went wrong

Definition at line 227 of file http_server.cpp.

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

## remove_resource()

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

removes a web service from the HTTP server

Parameters

  resource | the resource name or uri-stem to remove

Definition at line 169 of file http_server.cpp.

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

## set_authentication()

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

inline

sets the handler object for authentication verification processing

Definition at line 221 of file server.hpp.

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

## strip_trailing_slash()

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

inline static

strips trailing slash from a string, if one exists

Parameters

  str | the original string

Returns
  the resulting string, after any trailing slash is removed

Definition at line 160 of file server.hpp.

Referenced by pion::http::auth::add_permit(), add_redirect(), add_resource(), pion::http::auth::add_restrict(), pion::http::plugin_server::add_service(), handle_request(), pion::http::plugin_server::load_service(), pion::http::auth::need_authentication(), pion::http::cookie_auth::process_login(), remove_resource(), and pion::http::plugin_server::set_service_option().

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

- include/pion/http/server.hpp
- src/http_server.cpp