pion  5.0.6
request_reader.hpp
1 // ---------------------------------------------------------------------
2 // pion: a Boost C++ framework for building lightweight HTTP interfaces
3 // ---------------------------------------------------------------------
4 // Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
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_HTTP_REQUEST_READER_HEADER__
11 #define __PION_HTTP_REQUEST_READER_HEADER__
12 
13 #include <boost/asio.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/function.hpp>
16 #include <boost/function/function2.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <boost/enable_shared_from_this.hpp>
19 #include <pion/config.hpp>
20 #include <pion/http/request.hpp>
21 #include <pion/http/reader.hpp>
22 
23 
24 namespace pion { // begin namespace pion
25 namespace http { // begin namespace http
26 
27 
32  public http::reader,
33  public boost::enable_shared_from_this<request_reader>
34 {
35 
36 public:
37 
39  typedef boost::function3<void, http::request_ptr, tcp::connection_ptr,
40  const boost::system::error_code&> finished_handler_t;
41 
42 
43  // default destructor
44  virtual ~request_reader() {}
45 
52  static inline boost::shared_ptr<request_reader>
53  create(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
54  {
55  return boost::shared_ptr<request_reader>
56  (new request_reader(tcp_conn, handler));
57  }
58 
60  inline void set_headers_parsed_callback(finished_handler_t& h) { m_parsed_headers = h; }
61 
62 
63 protected:
64 
71  request_reader(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
72  : http::reader(true, tcp_conn), m_http_msg(new http::request),
73  m_finished(handler)
74  {
75  m_http_msg->set_remote_ip(tcp_conn->get_remote_ip());
76  set_logger(PION_GET_LOGGER("pion.http.request_reader"));
77  }
78 
80  virtual void read_bytes(void) {
81  get_connection()->async_read_some(boost::bind(&request_reader::consume_bytes,
82  shared_from_this(),
83  boost::asio::placeholders::error,
84  boost::asio::placeholders::bytes_transferred));
85  }
86 
88  virtual void finished_parsing_headers(const boost::system::error_code& ec) {
89  // call the finished headers handler with the HTTP message
91  }
92 
94  virtual void finished_reading(const boost::system::error_code& ec) {
95  // call the finished handler with the finished HTTP message
97  }
98 
100  virtual http::message& get_message(void) { return *m_http_msg; }
101 
103  http::request_ptr m_http_msg;
104 
106  finished_handler_t m_finished;
107 
109  finished_handler_t m_parsed_headers;
110 };
111 
112 
114 typedef boost::shared_ptr<request_reader> request_reader_ptr;
115 
116 
117 } // end namespace http
118 } // end namespace pion
119 
120 #endif
void consume_bytes(void)
Consumes bytes that have been read using an HTTP parser.
Definition: http_reader.cpp:66
virtual http::message & get_message(void)
Returns a reference to the HTTP message being parsed.
virtual void finished_parsing_headers(const boost::system::error_code &ec)
Called after we have finished parsing the HTTP message headers.
finished_handler_t m_finished
function called after the HTTP message has been parsed
static boost::shared_ptr< request_reader > create(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
http::request_ptr m_http_msg
The new HTTP message container being created.
tcp::connection_ptr & get_connection(void)
returns a shared pointer to the TCP connection
Definition: reader.hpp:40
virtual void read_bytes(void)
Reads more bytes from the TCP connection.
boost::function3< void, http::request_ptr, tcp::connection_ptr, const boost::system::error_code & > finished_handler_t
function called after the HTTP message has been parsed
virtual void finished_reading(const boost::system::error_code &ec)
Called after we have finished reading/parsing the HTTP message.
void set_headers_parsed_callback(finished_handler_t &h)
sets a function to be called after HTTP headers have been parsed
finished_handler_t m_parsed_headers
function called after the HTTP message headers have been parsed
void set_logger(logger log_ptr)
sets the logger to be used
Definition: parser.hpp:294
request_reader(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)