/usr/include/pion/http/server.hpp is in libpion-dev 5.0.7+dfsg-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | // ---------------------------------------------------------------------
// 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
///
/// server: a server that handles HTTP connections
///
class PION_API server :
public tcp::server
{
public:
/// type of function that is used to handle requests
typedef boost::function2<void, const http::request_ptr&, const tcp::connection_ptr&> request_handler_t;
/// handler for requests that result in "500 Server Error"
typedef boost::function3<void, const http::request_ptr&, const tcp::connection_ptr&,
const std::string&> error_handler_t;
/// default destructor
virtual ~server() { if (is_listening()) stop(); }
/**
* creates a new server object
*
* @param tcp_port port number used to listen for new connections (IPv4)
*/
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"));
}
/**
* creates a new server object
*
* @param endpoint TCP endpoint used to listen for new connections (see ASIO docs)
*/
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"));
}
/**
* creates a new server object
*
* @param sched the scheduler that will be used to manage worker threads
* @param tcp_port port number used to listen for new connections (IPv4)
*/
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"));
}
/**
* creates a new server object
*
* @param sched the scheduler that will be used to manage worker threads
* @param endpoint TCP endpoint used to listen for new connections (see ASIO docs)
*/
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"));
}
/**
* adds a new web service to the HTTP server
*
* @param resource the resource name or uri-stem to bind to the handler
* @param request_handler function used to handle requests to the resource
*/
void add_resource(const std::string& resource, request_handler_t request_handler);
/**
* removes a web service from the HTTP server
*
* @param resource the resource name or uri-stem to remove
*/
void remove_resource(const std::string& resource);
/**
* adds a new resource redirection to the HTTP server
*
* @param requested_resource the resource name or uri-stem that will be redirected
* @param new_resource the resource that requested_resource will be redirected to
*/
void add_redirect(const std::string& requested_resource, const std::string& new_resource);
/// sets the function that handles bad HTTP requests
inline void set_bad_request_handler(request_handler_t h) { m_bad_request_handler = h; }
/// sets the function that handles requests which match no other web services
inline void set_not_found_handler(request_handler_t h) { m_not_found_handler = h; }
/// sets the function that handles requests which match no other web services
inline void set_error_handler(error_handler_t h) { m_server_error_handler = h; }
/// clears the collection of resources recognized by the HTTP server
virtual void clear(void) {
if (is_listening()) stop();
boost::mutex::scoped_lock resource_lock(m_resource_mutex);
m_resources.clear();
}
/**
* strips trailing slash from a string, if one exists
*
* @param str the original string
* @return the resulting string, after any trailing slash is removed
*/
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;
}
/**
* used to send responses when a bad HTTP request is made
*
* @param http_request_ptr the new HTTP request to handle
* @param tcp_conn the TCP connection that has the new request
*/
static void handle_bad_request(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn);
/**
* used to send responses when no web services can handle the request
*
* @param http_request_ptr the new HTTP request to handle
* @param tcp_conn the TCP connection that has the new request
*/
static void handle_not_found_request(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn);
/**
* used to send responses when a server error occurs
*
* @param http_request_ptr the new HTTP request to handle
* @param tcp_conn the TCP connection that has the new request
* @param error_msg message that explains what went wrong
*/
static void handle_server_error(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn,
const std::string& error_msg);
/**
* used to send responses when a request is forbidden
*
* @param http_request_ptr the new HTTP request to handle
* @param tcp_conn the TCP connection that has the new request
* @param error_msg message that explains what went wrong
*/
static void handle_forbidden_request(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn,
const std::string& error_msg);
/**
* used to send responses when a method is not allowed
*
* @param http_request_ptr the new HTTP request to handle
* @param tcp_conn the TCP connection that has the new request
* @param allowed_methods optional comma separated list of allowed methods
*/
static void handle_method_not_allowed(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn,
const std::string& allowed_methods = "");
/**
* sets the handler object for authentication verification processing
*/
inline void set_authentication(http::auth_ptr auth) { m_auth_ptr = auth; }
/// sets the maximum length for HTTP request payload content
inline void set_max_content_length(std::size_t n) { m_max_content_length = n; }
protected:
/**
* handles a new TCP connection
*
* @param tcp_conn the new TCP connection to handle
*/
virtual void handle_connection(const tcp::connection_ptr& tcp_conn);
/**
* handles a new HTTP request
*
* @param http_request_ptr the HTTP request to handle
* @param tcp_conn TCP connection containing a new request
* @param ec error_code contains additional information for parsing errors
*/
virtual void handle_request(const http::request_ptr& http_request_ptr,
const tcp::connection_ptr& tcp_conn, const boost::system::error_code& ec);
/**
* searches for the appropriate request handler to use for a given resource
*
* @param resource the name of the resource to search for
* @param request_handler function that can handle requests for this resource
*/
virtual bool find_request_handler(const std::string& resource,
request_handler_t& request_handler) const;
private:
/// maximum number of redirections
static const unsigned int MAX_REDIRECTS;
/// data type for a map of resources to request handlers
typedef std::map<std::string, request_handler_t> resource_map_t;
/// data type for a map of requested resources to other resources
typedef std::map<std::string, std::string> redirect_map_t;
/// collection of resources that are recognized by this HTTP server
resource_map_t m_resources;
/// collection of redirections from a requested resource to another resource
redirect_map_t m_redirects;
/// points to a function that handles bad HTTP requests
request_handler_t m_bad_request_handler;
/// points to a function that handles requests which match no web services
request_handler_t m_not_found_handler;
/// points to the function that handles server errors
error_handler_t m_server_error_handler;
/// mutex used to protect access to the resources
mutable boost::mutex m_resource_mutex;
/// pointer to authentication handler object
http::auth_ptr m_auth_ptr;
/// maximum length for HTTP request payload content
std::size_t m_max_content_length;
};
/// data type for a HTTP server pointer
typedef boost::shared_ptr<server> server_ptr;
} // end namespace http
} // end namespace pion
#endif
|