server.hpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_HTTP_SERVER_HEADER__
#define __PION_HTTP_SERVER_HEADER__
#include <map>
#include <string>
#include <boost/asio.hpp>
#include <boost/function.hpp>
#include <boost/function/function2.hpp>
#include <boost/function/function3.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <pion/config.hpp>
#include <pion/tcp/server.hpp>
#include <pion/tcp/connection.hpp>
#include <pion/http/request.hpp>
#include <pion/http/auth.hpp>
#include <pion/http/parser.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class PION_API server :
public tcp::server
{
public:
typedef boost::function2<void, const http::request_ptr&, const tcp::connection_ptr&> request_handler_t;
typedef boost::function3<void, const http::request_ptr&, const tcp::connection_ptr&,
const std::string&> error_handler_t;
virtual ~server() { if (is_listening()) stop(); }
explicit server(const unsigned int tcp_port = 0)
: tcp::server(tcp_port),
m_bad_request_handler(server::handle_bad_request),
m_not_found_handler(server::handle_not_found_request),
m_server_error_handler(server::handle_server_error),
m_max_content_length(http::parser::DEFAULT_CONTENT_MAX)
{
set_logger(PION_GET_LOGGER("pion.http.server"));
}
explicit server(const boost::asio::ip::tcp::endpoint& endpoint)
: tcp::server(endpoint),
m_bad_request_handler(server::handle_bad_request),
m_not_found_handler(server::handle_not_found_request),
m_server_error_handler(server::handle_server_error),
m_max_content_length(http::parser::DEFAULT_CONTENT_MAX)
{
set_logger(PION_GET_LOGGER("pion.http.server"));
}
explicit server(scheduler& sched, const unsigned int tcp_port = 0)
: tcp::server(sched, tcp_port),
m_bad_request_handler(server::handle_bad_request),
m_not_found_handler(server::handle_not_found_request),
m_server_error_handler(server::handle_server_error),
m_max_content_length(http::parser::DEFAULT_CONTENT_MAX)
{
set_logger(PION_GET_LOGGER("pion.http.server"));
}
server(scheduler& sched, const boost::asio::ip::tcp::endpoint& endpoint)
: tcp::server(sched, endpoint),
m_bad_request_handler(server::handle_bad_request),
m_not_found_handler(server::handle_not_found_request),
m_server_error_handler(server::handle_server_error),
m_max_content_length(http::parser::DEFAULT_CONTENT_MAX)
{
set_logger(PION_GET_LOGGER("pion.http.server"));
}
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);
inline void set_bad_request_handler(request_handler_t h) { m_bad_request_handler = h; }
inline void set_not_found_handler(request_handler_t h) { m_not_found_handler = h; }
inline void set_error_handler(error_handler_t h) { m_server_error_handler = h; }
virtual void clear(void) {
if (is_listening()) stop();
boost::mutex::scoped_lock resource_lock(m_resource_mutex);
m_resources.clear();
}
static inline std::string strip_trailing_slash(const std::string& str) {
std::string result(str);
if (!result.empty() && result[result.size()-1]=='/')
result.resize(result.size() - 1);
return result;
}
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 = "");
inline void set_authentication(http::auth_ptr auth) { m_auth_ptr = auth; }
inline void set_max_content_length(std::size_t n) { m_max_content_length = n; }
protected:
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;
private:
static const unsigned int MAX_REDIRECTS;
typedef std::map<std::string, request_handler_t> resource_map_t;
typedef std::map<std::string, std::string> redirect_map_t;
resource_map_t m_resources;
redirect_map_t m_redirects;
request_handler_t m_bad_request_handler;
request_handler_t m_not_found_handler;
error_handler_t m_server_error_handler;
mutable boost::mutex m_resource_mutex;
http::auth_ptr m_auth_ptr;
std::size_t m_max_content_length;
};
typedef boost::shared_ptr<server> server_ptr;
} // end namespace http
} // end namespace pion
#endif
pion::http::server::server
server(scheduler &sched, const unsigned int tcp_port=0)
Definition: server.hpp:89
pion::tcp::server
Definition: server.hpp:32
pion::http::server::error_handler_t
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"
Definition: server.hpp:47
pion::http::server::set_error_handler
void set_error_handler(error_handler_t h)
sets the function that handles requests which match no other web services
Definition: server.hpp:145
pion::http::server::set_max_content_length
void set_max_content_length(std::size_t n)
sets the maximum length for HTTP request payload content
Definition: server.hpp:224
pion::http::server::server
server(scheduler &sched, const boost::asio::ip::tcp::endpoint &endpoint)
Definition: server.hpp:105
pion::http::auth
Definition: auth.hpp:34
pion::http::server::~server
virtual ~server()
default destructor
Definition: server.hpp:51
pion::http::server::set_not_found_handler
void set_not_found_handler(request_handler_t h)
sets the function that handles requests which match no other web services
Definition: server.hpp:142
pion::http::server::clear
virtual void clear(void)
clears the collection of resources recognized by the HTTP server
Definition: server.hpp:148
pion::http::server
Definition: server.hpp:36
pion::http::server::strip_trailing_slash
static std::string strip_trailing_slash(const std::string &str)
Definition: server.hpp:160
pion::http::server::request_handler_t
boost::function2< void, const http::request_ptr &, const tcp::connection_ptr & > request_handler_t
type of function that is used to handle requests
Definition: server.hpp:43
pion
Definition: admin_rights.hpp:19
pion::http::server::server
server(const unsigned int tcp_port=0)
Definition: server.hpp:58
pion::http::server::set_bad_request_handler
void set_bad_request_handler(request_handler_t h)
sets the function that handles bad HTTP requests
Definition: server.hpp:139
pion::http::parser
Definition: parser.hpp:39
pion::http::server::server
server(const boost::asio::ip::tcp::endpoint &endpoint)
Definition: server.hpp:73
pion::http::server::set_authentication
void set_authentication(http::auth_ptr auth)
Definition: server.hpp:221
pion::scheduler
Definition: scheduler.hpp:34