pion
5.0.6
|
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_RESPONSE_READER_HEADER__ 00011 #define __PION_HTTP_RESPONSE_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/response.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 response_reader : 00032 public http::reader, 00033 public boost::enable_shared_from_this<response_reader> 00034 { 00035 00036 public: 00037 00039 typedef boost::function3<void, http::response_ptr, tcp::connection_ptr, 00040 const boost::system::error_code&> finished_handler_t; 00041 00042 00043 // default destructor 00044 virtual ~response_reader() {} 00045 00053 static inline boost::shared_ptr<response_reader> 00054 create(tcp::connection_ptr& tcp_conn, const http::request& http_request, 00055 finished_handler_t handler) 00056 { 00057 return boost::shared_ptr<response_reader> 00058 (new response_reader(tcp_conn, http_request, handler)); 00059 } 00060 00062 inline void set_headers_parsed_callback(finished_handler_t& h) { m_parsed_headers = h; } 00063 00064 00065 protected: 00066 00074 response_reader(tcp::connection_ptr& tcp_conn, const http::request& http_request, 00075 finished_handler_t handler) 00076 : http::reader(false, tcp_conn), m_http_msg(new http::response(http_request)), 00077 m_finished(handler) 00078 { 00079 m_http_msg->set_remote_ip(tcp_conn->get_remote_ip()); 00080 set_logger(PION_GET_LOGGER("pion.http.response_reader")); 00081 } 00082 00084 virtual void read_bytes(void) { 00085 get_connection()->async_read_some(boost::bind(&response_reader::consume_bytes, 00086 shared_from_this(), 00087 boost::asio::placeholders::error, 00088 boost::asio::placeholders::bytes_transferred)); 00089 } 00090 00092 virtual void finished_parsing_headers(const boost::system::error_code& ec) { 00093 // call the finished headers handler with the HTTP message 00094 if (m_parsed_headers) m_parsed_headers(m_http_msg, get_connection(), ec); 00095 } 00096 00098 virtual void finished_reading(const boost::system::error_code& ec) { 00099 // call the finished handler with the finished HTTP message 00100 if (m_finished) m_finished(m_http_msg, get_connection(), ec); 00101 } 00102 00104 virtual http::message& get_message(void) { return *m_http_msg; } 00105 00106 00108 http::response_ptr m_http_msg; 00109 00111 finished_handler_t m_finished; 00112 00114 finished_handler_t m_parsed_headers; 00115 }; 00116 00117 00119 typedef boost::shared_ptr<response_reader> response_reader_ptr; 00120 00121 00122 } // end namespace http 00123 } // end namespace pion 00124 00125 #endif