pion-net  4.0.9
HTTPServer.hpp
1 // ------------------------------------------------------------------
2 // pion-net: a C++ framework for building lightweight HTTP interfaces
3 // ------------------------------------------------------------------
4 // Copyright (C) 2007-2008 Atomic Labs, Inc. (http://www.atomiclabs.com)
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
8 //
9 
10 #ifndef __PION_HTTPSERVER_HEADER__
11 #define __PION_HTTPSERVER_HEADER__
12 
13 #include <map>
14 #include <string>
15 #include <boost/asio.hpp>
16 #include <boost/function.hpp>
17 #include <boost/function/function2.hpp>
18 #include <boost/function/function3.hpp>
19 #include <boost/shared_ptr.hpp>
20 #include <boost/thread/mutex.hpp>
21 #include <pion/PionConfig.hpp>
22 #include <pion/net/TCPServer.hpp>
23 #include <pion/net/TCPConnection.hpp>
24 #include <pion/net/HTTPRequest.hpp>
25 #include <pion/net/HTTPAuth.hpp>
26 #include <pion/net/HTTPParser.hpp>
27 
28 
29 namespace pion { // begin namespace pion
30 namespace net { // begin namespace net (Pion Network Library)
31 
35 class PION_NET_API HTTPServer :
36  public TCPServer
37 {
38 
39 public:
40 
42  typedef boost::function2<void, HTTPRequestPtr&, TCPConnectionPtr&> RequestHandler;
43 
45  typedef boost::function3<void, HTTPRequestPtr&, TCPConnectionPtr&,
46  const std::string&> ServerErrorHandler;
47 
48 
50  virtual ~HTTPServer() { if (isListening()) stop(); }
51 
57  explicit HTTPServer(const unsigned int tcp_port = 0)
58  : TCPServer(tcp_port),
59  m_bad_request_handler(HTTPServer::handleBadRequest),
60  m_not_found_handler(HTTPServer::handleNotFoundRequest),
61  m_server_error_handler(HTTPServer::handleServerError),
62  m_max_content_length(HTTPParser::DEFAULT_CONTENT_MAX)
63  {
64  setLogger(PION_GET_LOGGER("pion.net.HTTPServer"));
65  }
66 
72  explicit HTTPServer(const boost::asio::ip::tcp::endpoint& endpoint)
73  : TCPServer(endpoint),
74  m_bad_request_handler(HTTPServer::handleBadRequest),
75  m_not_found_handler(HTTPServer::handleNotFoundRequest),
76  m_server_error_handler(HTTPServer::handleServerError),
77  m_max_content_length(HTTPParser::DEFAULT_CONTENT_MAX)
78  {
79  setLogger(PION_GET_LOGGER("pion.net.HTTPServer"));
80  }
81 
88  explicit HTTPServer(PionScheduler& scheduler, const unsigned int tcp_port = 0)
89  : TCPServer(scheduler, tcp_port),
90  m_bad_request_handler(HTTPServer::handleBadRequest),
91  m_not_found_handler(HTTPServer::handleNotFoundRequest),
92  m_server_error_handler(HTTPServer::handleServerError),
93  m_max_content_length(HTTPParser::DEFAULT_CONTENT_MAX)
94  {
95  setLogger(PION_GET_LOGGER("pion.net.HTTPServer"));
96  }
97 
104  HTTPServer(PionScheduler& scheduler, const boost::asio::ip::tcp::endpoint& endpoint)
105  : TCPServer(scheduler, endpoint),
106  m_bad_request_handler(HTTPServer::handleBadRequest),
107  m_not_found_handler(HTTPServer::handleNotFoundRequest),
108  m_server_error_handler(HTTPServer::handleServerError),
109  m_max_content_length(HTTPParser::DEFAULT_CONTENT_MAX)
110  {
111  setLogger(PION_GET_LOGGER("pion.net.HTTPServer"));
112  }
113 
120  void addResource(const std::string& resource, RequestHandler request_handler);
121 
127  void removeResource(const std::string& resource);
128 
135  void addRedirect(const std::string& requested_resource, const std::string& new_resource);
136 
138  inline void setBadRequestHandler(RequestHandler h) { m_bad_request_handler = h; }
139 
141  inline void setNotFoundHandler(RequestHandler h) { m_not_found_handler = h; }
142 
144  inline void setServerErrorHandler(ServerErrorHandler h) { m_server_error_handler = h; }
145 
147  virtual void clear(void) {
148  if (isListening()) stop();
149  boost::mutex::scoped_lock resource_lock(m_resource_mutex);
150  m_resources.clear();
151  }
152 
159  static inline std::string stripTrailingSlash(const std::string& str) {
160  std::string result(str);
161  if (!result.empty() && result[result.size()-1]=='/')
162  result.resize(result.size() - 1);
163  return result;
164  }
165 
172  static void handleBadRequest(HTTPRequestPtr& http_request,
173  TCPConnectionPtr& tcp_conn);
174 
181  static void handleNotFoundRequest(HTTPRequestPtr& http_request,
182  TCPConnectionPtr& tcp_conn);
183 
191  static void handleServerError(HTTPRequestPtr& http_request,
192  TCPConnectionPtr& tcp_conn,
193  const std::string& error_msg);
194 
202  static void handleForbiddenRequest(HTTPRequestPtr& http_request,
203  TCPConnectionPtr& tcp_conn,
204  const std::string& error_msg);
205 
213  static void handleMethodNotAllowed(HTTPRequestPtr& http_request,
214  TCPConnectionPtr& tcp_conn,
215  const std::string& allowed_methods = "");
216 
220  inline void setAuthentication(HTTPAuthPtr auth) { m_auth = auth; }
221 
223  inline void setMaxContentLength(std::size_t n) { m_max_content_length = n; }
224 
225 protected:
226 
232  virtual void handleConnection(TCPConnectionPtr& tcp_conn);
233 
241  virtual void handleRequest(HTTPRequestPtr& http_request,
242  TCPConnectionPtr& tcp_conn, const boost::system::error_code& ec);
243 
250  virtual bool findRequestHandler(const std::string& resource,
251  RequestHandler& request_handler) const;
252 
253 
254 private:
255 
257  static const unsigned int MAX_REDIRECTS;
258 
260  typedef std::map<std::string, RequestHandler> ResourceMap;
261 
263  typedef std::map<std::string, std::string> RedirectMap;
264 
265 
267  ResourceMap m_resources;
268 
270  RedirectMap m_redirects;
271 
273  RequestHandler m_bad_request_handler;
274 
276  RequestHandler m_not_found_handler;
277 
279  ServerErrorHandler m_server_error_handler;
280 
282  mutable boost::mutex m_resource_mutex;
283 
285  HTTPAuthPtr m_auth;
286 
288  std::size_t m_max_content_length;
289 };
290 
291 
293 typedef boost::shared_ptr<HTTPServer> HTTPServerPtr;
294 
295 
296 } // end namespace net
297 } // end namespace pion
298 
299 #endif
HTTPServer(PionScheduler &scheduler, const unsigned int tcp_port=0)
Definition: HTTPServer.hpp:88
HTTPServer(const unsigned int tcp_port=0)
Definition: HTTPServer.hpp:57
void setBadRequestHandler(RequestHandler h)
sets the function that handles bad HTTP requests
Definition: HTTPServer.hpp:138
static std::string stripTrailingSlash(const std::string &str)
Definition: HTTPServer.hpp:159
boost::function3< void, HTTPRequestPtr &, TCPConnectionPtr &, const std::string & > ServerErrorHandler
handler for requests that result in "500 Server Error"
Definition: HTTPServer.hpp:46
HTTPServer(PionScheduler &scheduler, const boost::asio::ip::tcp::endpoint &endpoint)
Definition: HTTPServer.hpp:104
boost::function2< void, HTTPRequestPtr &, TCPConnectionPtr & > RequestHandler
type of function that is used to handle requests
Definition: HTTPServer.hpp:42
virtual void clear(void)
clears the collection of resources recognized by the HTTP server
Definition: HTTPServer.hpp:147
void setAuthentication(HTTPAuthPtr auth)
Definition: HTTPServer.hpp:220
void setMaxContentLength(std::size_t n)
sets the maximum length for HTTP request payload content
Definition: HTTPServer.hpp:223
void setNotFoundHandler(RequestHandler h)
sets the function that handles requests which match no other web services
Definition: HTTPServer.hpp:141
HTTPServer(const boost::asio::ip::tcp::endpoint &endpoint)
Definition: HTTPServer.hpp:72
void setServerErrorHandler(ServerErrorHandler h)
sets the function that handles requests which match no other web services
Definition: HTTPServer.hpp:144
virtual ~HTTPServer()
default destructor
Definition: HTTPServer.hpp:50