pion-net  4.0.9
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
pion::net::HTTPServer Class Reference

#include <HTTPServer.hpp>

Inherits pion::net::TCPServer.

Inherited by pion::net::WebServer.

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
 
- Public Member Functions inherited from pion::net::TCPServer
virtual ~TCPServer ()
 default destructor
 
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 setSSLKeyFile (const std::string &pem_key_file)
 
std::size_t getConnections (void) const
 returns the number of active tcp connections
 
unsigned int getPort (void) const
 returns tcp port number that the server listens for connections on
 
void setPort (unsigned int p)
 sets tcp port number that the server listens for connections on
 
boost::asio::ip::address getAddress (void) const
 returns IP address that the server listens for connections on
 
void setAddress (const boost::asio::ip::address &addr)
 sets IP address that the server listens for connections on
 
const
boost::asio::ip::tcp::endpoint & 
getEndpoint (void) const
 returns tcp endpoint that the server listens for connections on
 
void setEndpoint (const boost::asio::ip::tcp::endpoint &ep)
 sets tcp endpoint that the server listens for connections on
 
bool getSSLFlag (void) const
 returns true if the server uses SSL to encrypt connections
 
void setSSLFlag (bool b=true)
 sets value of SSL flag (true if the server uses SSL to encrypt connections)
 
TCPConnection::SSLContext & getSSLContext (void)
 returns the SSL context for configuration
 
bool isListening (void) const
 returns true if the server is listening for connections
 
void setLogger (PionLogger log_ptr)
 sets the logger to be used
 
PionLogger getLogger (void)
 returns the logger currently in use
 

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
 
- Protected Member Functions inherited from pion::net::TCPServer
 TCPServer (const unsigned int tcp_port)
 
 TCPServer (const boost::asio::ip::tcp::endpoint &endpoint)
 
 TCPServer (PionScheduler &scheduler, const unsigned int tcp_port=0)
 
 TCPServer (PionScheduler &scheduler, const boost::asio::ip::tcp::endpoint &endpoint)
 
virtual void beforeStarting (void)
 called before the TCP server starts listening for new connections
 
virtual void afterStopping (void)
 called after the TCP server has stopped listing for new connections
 
boost::asio::io_service & getIOService (void)
 returns an async I/O service used to schedule work
 

Additional Inherited Members

- Protected Attributes inherited from pion::net::TCPServer
PionLogger m_logger
 primary logging interface used by this class
 

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

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

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

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

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

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

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

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: