# pion::net::HTTPServer Class Reference

`#include <HTTPServer.hpp>`

Inherits pion::net::TCPServer.

Inherited by pion::net::WebServer.

List of all members.

## Detailed Description

HTTPServer: a server that handles HTTP connections

Definition at line 35 of file HTTPServer.hpp.

## Public Types

typedef boost::function2< void, HTTPRequestPtr &, TCPConnectionPtr & > | RequestHandler
| type of function that is used to handle requests
typedef boost::function3< void, HTTPRequestPtr &, TCPConnectionPtr &, const std::string & > | ServerErrorHandler
| handler for requests that result in "500 Server Error"

## Public Member Functions

virtual | ~HTTPServer ()
| default destructor
| HTTPServer (const unsigned int tcp_port=0)
| HTTPServer (const boost::asio::ip::tcp::endpoint &endpoint)
| HTTPServer (PionScheduler &scheduler, const unsigned int tcp_port=0)
| HTTPServer (PionScheduler &scheduler, const boost::asio::ip::tcp::endpoint &endpoint)
void | addResource (const std::string &resource, RequestHandler request_handler)
void | removeResource (const std::string &resource)
void | addRedirect (const std::string &requested_resource, const std::string &new_resource)
void | setBadRequestHandler (RequestHandler h)
| sets the function that handles bad HTTP requests
void | setNotFoundHandler (RequestHandler h)
| sets the function that handles requests which match no other web services
void | setServerErrorHandler (ServerErrorHandler 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 | setAuthentication (HTTPAuthPtr auth)
void | setMaxContentLength (std::size_t n)
| sets the maximum length for HTTP request payload content

## Static Public Member Functions

static std::string | stripTrailingSlash (const std::string &str)
static void | handleBadRequest (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn)
static void | handleNotFoundRequest (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn)
static void | handleServerError (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn, const std::string &error_msg)
static void | handleForbiddenRequest (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn, const std::string &error_msg)
static void | handleMethodNotAllowed (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn, const std::string &allowed_methods="")

## Protected Member Functions

virtual void | handleConnection (TCPConnectionPtr &tcp_conn)
virtual void | handleRequest (HTTPRequestPtr &http_request, TCPConnectionPtr &tcp_conn, const boost::system::error_code &ec)
virtual bool | findRequestHandler (const std::string &resource, RequestHandler &request_handler) const

## Constructor & Destructor Documentation

pion::net::HTTPServer::HTTPServer | ( | const unsigned int | tcp_port = `0` | ) | `[inline, explicit]`

creates a new HTTPServer object

Parameters:

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

Definition at line 57 of file HTTPServer.hpp.

pion::net::HTTPServer::HTTPServer | ( | const boost::asio::ip::tcp::endpoint & | endpoint | ) | `[inline, explicit]`

creates a new HTTPServer object

Parameters:

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

Definition at line 72 of file HTTPServer.hpp.

pion::net::HTTPServer::HTTPServer | ( | PionScheduler & | scheduler,
|  | const unsigned int | tcp_port = `0` |
| ) |  |  | `[inline, explicit]`

creates a new HTTPServer object

Parameters:

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

Definition at line 88 of file HTTPServer.hpp.

pion::net::HTTPServer::HTTPServer | ( | PionScheduler & | scheduler,
|  | const boost::asio::ip::tcp::endpoint & | endpoint |
| ) |  |  | `[inline]`

creates a new HTTPServer object

Parameters:

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

Definition at line 104 of file HTTPServer.hpp.

## Member Function Documentation

void pion::net::HTTPServer::addRedirect | ( | 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 162 of file HTTPServer.cpp.

References pion::net::TCPServer::m_logger, and stripTrailingSlash().

void pion::net::HTTPServer::addResource | ( | const std::string & | resource,
|  | RequestHandler | 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 145 of file HTTPServer.cpp.

References pion::net::TCPServer::m_logger, and stripTrailingSlash().

Referenced by pion::net::WebServer::addService(), and pion::net::WebServer::loadService().

bool pion::net::HTTPServer::findRequestHandler | ( | const std::string & | resource,
|  | RequestHandler & | 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 119 of file HTTPServer.cpp.

Referenced by handleRequest().

void pion::net::HTTPServer::handleBadRequest | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn |
| ) |  |  | `[static]`

used to send responses when a bad HTTP request is made

Parameters:

  | http_request | the new HTTP request to handle
  | tcp_conn | the TCP connection that has the new request

Definition at line 172 of file HTTPServer.cpp.

References pion::net::HTTPResponseWriter::create(), pion::net::TCPConnection::finish(), pion::net::HTTPTypes::RESPONSE_CODE_BAD_REQUEST, and pion::net::HTTPTypes::RESPONSE_MESSAGE_BAD_REQUEST.

void pion::net::HTTPServer::handleConnection | ( | TCPConnectionPtr & | tcp_conn | ) | `[protected, virtual]`

handles a new TCP connection

Parameters:

  | tcp_conn | the new TCP connection to handle

Reimplemented from pion::net::TCPServer.

Definition at line 27 of file HTTPServer.cpp.

References pion::net::HTTPRequestReader::create(), and handleRequest().

void pion::net::HTTPServer::handleForbiddenRequest | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn,
|  | const std::string & | error_msg |
| ) |  |  | `[static]`

used to send responses when a request is forbidden

Parameters:

  | http_request | 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 235 of file HTTPServer.cpp.

References pion::net::HTTPResponseWriter::create(), pion::net::TCPConnection::finish(), pion::net::HTTPTypes::RESPONSE_CODE_FORBIDDEN, and pion::net::HTTPTypes::RESPONSE_MESSAGE_FORBIDDEN.

void pion::net::HTTPServer::handleMethodNotAllowed | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn,
|  | const std::string & | allowed_methods = `""` |
| ) |  |  | `[static]`

used to send responses when a method is not allowed

Parameters:

  | http_request | 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 262 of file HTTPServer.cpp.

References pion::net::HTTPResponseWriter::create(), pion::net::TCPConnection::finish(), pion::net::HTTPTypes::RESPONSE_CODE_METHOD_NOT_ALLOWED, and pion::net::HTTPTypes::RESPONSE_MESSAGE_METHOD_NOT_ALLOWED.

void pion::net::HTTPServer::handleNotFoundRequest | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn |
| ) |  |  | `[static]`

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

Parameters:

  | http_request | the new HTTP request to handle
  | tcp_conn | the TCP connection that has the new request

Definition at line 190 of file HTTPServer.cpp.

References pion::net::HTTPResponseWriter::create(), pion::net::TCPConnection::finish(), pion::net::HTTPTypes::RESPONSE_CODE_NOT_FOUND, and pion::net::HTTPTypes::RESPONSE_MESSAGE_NOT_FOUND.

void pion::net::HTTPServer::handleRequest | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn,
|  | const boost::system::error_code & | ec |
| ) |  |  | `[protected, virtual]`

handles a new HTTP request

Parameters:

  | http_request | 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 36 of file HTTPServer.cpp.

References findRequestHandler(), pion::net::HTTPParser::getErrorCategory(), pion::net::TCPServer::getPort(), pion::net::TCPServer::m_logger, and stripTrailingSlash().

Referenced by handleConnection().

void pion::net::HTTPServer::handleServerError | ( | HTTPRequestPtr & | http_request,
|  | TCPConnectionPtr & | tcp_conn,
|  | const std::string & | error_msg |
| ) |  |  | `[static]`

used to send responses when a server error occurs

Parameters:

  | http_request | 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 212 of file HTTPServer.cpp.

References pion::net::HTTPResponseWriter::create(), pion::net::TCPConnection::finish(), pion::net::HTTPTypes::RESPONSE_CODE_SERVER_ERROR, and pion::net::HTTPTypes::RESPONSE_MESSAGE_SERVER_ERROR.

void pion::net::HTTPServer::removeResource | ( | 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 154 of file HTTPServer.cpp.

References pion::net::TCPServer::m_logger, and stripTrailingSlash().

void pion::net::HTTPServer::setAuthentication | ( | HTTPAuthPtr | auth | ) | `[inline]`

sets the handler object for authentication verification processing

Definition at line 220 of file HTTPServer.hpp.

Referenced by pion::net::WebServer::loadServiceConfig().

static std::string pion::net::HTTPServer::stripTrailingSlash | ( | 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 159 of file HTTPServer.hpp.

Referenced by pion::net::HTTPAuth::addPermit(), addRedirect(), addResource(), pion::net::HTTPAuth::addRestrict(), pion::net::WebServer::addService(), handleRequest(), pion::net::WebServer::loadService(), pion::net::HTTPAuth::needAuthentication(), pion::net::HTTPCookieAuth::processLogin(), removeResource(), and pion::net::WebServer::setServiceOption().

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

- net/include/pion/net/HTTPServer.hpp
- net/src/HTTPServer.cpp