pion-net  4.0.9
HTTPResponseReader.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_HTTPRESPONSEREADER_HEADER__
11 #define __PION_HTTPRESPONSEREADER_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/PionConfig.hpp>
20 #include <pion/net/HTTPResponse.hpp>
21 #include <pion/net/HTTPReader.hpp>
22 
23 
24 namespace pion { // begin namespace pion
25 namespace net { // begin namespace net (Pion Network Library)
26 
27 
32  public HTTPReader,
33  public boost::enable_shared_from_this<HTTPResponseReader>
34 {
35 
36 public:
37 
39  typedef boost::function3<void, HTTPResponsePtr, TCPConnectionPtr,
40  const boost::system::error_code&> FinishedHandler;
41 
42 
43  // default destructor
44  virtual ~HTTPResponseReader() {}
45 
53  static inline boost::shared_ptr<HTTPResponseReader>
54  create(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
55  FinishedHandler handler)
56  {
57  return boost::shared_ptr<HTTPResponseReader>
58  (new HTTPResponseReader(tcp_conn, http_request, handler));
59  }
60 
61 
62 protected:
63 
71  HTTPResponseReader(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
72  FinishedHandler handler)
73  : HTTPReader(false, tcp_conn), m_http_msg(new HTTPResponse(http_request)),
74  m_finished(handler)
75  {
76  m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
77  setLogger(PION_GET_LOGGER("pion.net.HTTPResponseReader"));
78  }
79 
81  virtual void readBytes(void) {
82  getTCPConnection()->async_read_some(boost::bind(&HTTPResponseReader::consumeBytes,
83  shared_from_this(),
84  boost::asio::placeholders::error,
85  boost::asio::placeholders::bytes_transferred));
86  }
87 
89  virtual void finishedReading(const boost::system::error_code& ec) {
90  // call the finished handler with the finished HTTP message
92  }
93 
95  virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
96 
97 
99  HTTPResponsePtr m_http_msg;
100 
103 };
104 
105 
107 typedef boost::shared_ptr<HTTPResponseReader> HTTPResponseReaderPtr;
108 
109 
110 } // end namespace net
111 } // end namespace pion
112 
113 #endif
virtual void finishedReading(const boost::system::error_code &ec)
Called after we have finished reading/parsing the HTTP message.
boost::function3< void, HTTPResponsePtr, TCPConnectionPtr, const boost::system::error_code & > FinishedHandler
function called after the HTTP message has been parsed
void consumeBytes(void)
Consumes bytes that have been read using an HTTP parser.
Definition: HTTPReader.cpp:66
FinishedHandler m_finished
function called after the HTTP message has been parsed
virtual void readBytes(void)
Reads more bytes from the TCP connection.
HTTPResponseReader(TCPConnectionPtr &tcp_conn, const HTTPRequest &http_request, FinishedHandler handler)
TCPConnectionPtr & getTCPConnection(void)
returns a shared pointer to the TCP connection
Definition: HTTPReader.hpp:40
void setLogger(PionLogger log_ptr)
sets the logger to be used
Definition: HTTPParser.hpp:279
static boost::shared_ptr< HTTPResponseReader > create(TCPConnectionPtr &tcp_conn, const HTTPRequest &http_request, FinishedHandler handler)
HTTPResponsePtr m_http_msg
The new HTTP message container being created.
virtual HTTPMessage & getMessage(void)
Returns a reference to the HTTP message being parsed.