pion-net  4.0.9
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions
pion::net::HTTPServer Class Reference

#include <HTTPServer.hpp>

Inherits pion::net::TCPServer.

Inherited by pion::net::WebServer.

List of all members.

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

Detailed Description

HTTPServer: a server that handles HTTP connections

Definition at line 35 of file HTTPServer.hpp.


Constructor & Destructor Documentation

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

creates a new HTTPServer object

Parameters:
tcp_portport 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:
endpointTCP 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:
schedulerthe PionScheduler that will be used to manage worker threads
tcp_portport 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:
schedulerthe PionScheduler that will be used to manage worker threads
endpointTCP 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_resourcethe resource name or uri-stem that will be redirected
new_resourcethe 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:
resourcethe resource name or uri-stem to bind to the handler
request_handlerfunction 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:
resourcethe name of the resource to search for
request_handlerfunction 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_requestthe new HTTP request to handle
tcp_connthe TCP connection that has the new request

Definition at line 172 of file HTTPServer.cpp.

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

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

handles a new TCP connection

Parameters:
tcp_connthe 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_requestthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
error_msgmessage that explains what went wrong

Definition at line 235 of file HTTPServer.cpp.

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

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_requestthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
allowed_methodsoptional comma separated list of allowed methods

Definition at line 262 of file HTTPServer.cpp.

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

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

Definition at line 190 of file HTTPServer.cpp.

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

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_requestthe HTTP request to handle
tcp_connTCP connection containing a new request
ecerror_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_requestthe new HTTP request to handle
tcp_connthe TCP connection that has the new request
error_msgmessage that explains what went wrong

Definition at line 212 of file HTTPServer.cpp.

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

void pion::net::HTTPServer::removeResource ( const std::string &  resource)

removes a web service from the HTTP server

Parameters:
resourcethe 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:
strthe 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: