pion  5.0.6
include/pion/http/request_reader.hpp
00001 // ---------------------------------------------------------------------
00002 // pion:  a Boost C++ framework for building lightweight HTTP interfaces
00003 // ---------------------------------------------------------------------
00004 // Copyright (C) 2007-2014 Splunk Inc.  (https://github.com/splunk/pion)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
00008 //
00009 
00010 #ifndef __PION_HTTP_REQUEST_READER_HEADER__
00011 #define __PION_HTTP_REQUEST_READER_HEADER__
00012 
00013 #include <boost/asio.hpp>
00014 #include <boost/bind.hpp>
00015 #include <boost/function.hpp>
00016 #include <boost/function/function2.hpp>
00017 #include <boost/shared_ptr.hpp>
00018 #include <boost/enable_shared_from_this.hpp>
00019 #include <pion/config.hpp>
00020 #include <pion/http/request.hpp>
00021 #include <pion/http/reader.hpp>
00022 
00023 
00024 namespace pion {    // begin namespace pion
00025 namespace http {    // begin namespace http
00026 
00027 
00031 class request_reader :
00032     public http::reader,
00033     public boost::enable_shared_from_this<request_reader>
00034 {
00035 
00036 public:
00037 
00039     typedef boost::function3<void, http::request_ptr, tcp::connection_ptr,
00040         const boost::system::error_code&>   finished_handler_t;
00041 
00042     
00043     // default destructor
00044     virtual ~request_reader() {}
00045     
00052     static inline boost::shared_ptr<request_reader>
00053         create(tcp::connection_ptr& tcp_conn, finished_handler_t handler)
00054     {
00055         return boost::shared_ptr<request_reader>
00056             (new request_reader(tcp_conn, handler));
00057     }
00058     
00060     inline void set_headers_parsed_callback(finished_handler_t& h) { m_parsed_headers = h; }
00061     
00062     
00063 protected:
00064 
00071     request_reader(tcp::connection_ptr& tcp_conn, finished_handler_t handler)
00072         : http::reader(true, tcp_conn), m_http_msg(new http::request),
00073         m_finished(handler)
00074     {
00075         m_http_msg->set_remote_ip(tcp_conn->get_remote_ip());
00076         set_logger(PION_GET_LOGGER("pion.http.request_reader"));
00077     }
00078         
00080     virtual void read_bytes(void) {
00081         get_connection()->async_read_some(boost::bind(&request_reader::consume_bytes,
00082                                                         shared_from_this(),
00083                                                         boost::asio::placeholders::error,
00084                                                         boost::asio::placeholders::bytes_transferred));
00085     }
00086 
00088     virtual void finished_parsing_headers(const boost::system::error_code& ec) {
00089         // call the finished headers handler with the HTTP message
00090         if (m_parsed_headers) m_parsed_headers(m_http_msg, get_connection(), ec);
00091     }
00092     
00094     virtual void finished_reading(const boost::system::error_code& ec) {
00095         // call the finished handler with the finished HTTP message
00096         if (m_finished) m_finished(m_http_msg, get_connection(), ec);
00097     }
00098     
00100     virtual http::message& get_message(void) { return *m_http_msg; }
00101 
00103     http::request_ptr              m_http_msg;
00104 
00106     finished_handler_t             m_finished;
00107 
00109     finished_handler_t             m_parsed_headers;
00110 };
00111 
00112 
00114 typedef boost::shared_ptr<request_reader>    request_reader_ptr;
00115 
00116 
00117 }   // end namespace http
00118 }   // end namespace pion
00119 
00120 #endif