pion-net  4.0.9
HTTPRequestReader.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_HTTPREQUESTREADER_HEADER__
11 #define __PION_HTTPREQUESTREADER_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/HTTPRequest.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<HTTPRequestReader>
34 {
35 
36 public:
37 
39  typedef boost::function3<void, HTTPRequestPtr, TCPConnectionPtr,
40  const boost::system::error_code&> FinishedHandler;
41 
42 
43  // default destructor
44  virtual ~HTTPRequestReader() {}
45 
52  static inline boost::shared_ptr<HTTPRequestReader>
53  create(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
54  {
55  return boost::shared_ptr<HTTPRequestReader>
56  (new HTTPRequestReader(tcp_conn, handler));
57  }
58 
59 
60 protected:
61 
68  HTTPRequestReader(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
69  : HTTPReader(true, tcp_conn), m_http_msg(new HTTPRequest),
70  m_finished(handler)
71  {
72  m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
73  setLogger(PION_GET_LOGGER("pion.net.HTTPRequestReader"));
74  }
75 
77  virtual void readBytes(void) {
78  getTCPConnection()->async_read_some(boost::bind(&HTTPRequestReader::consumeBytes,
79  shared_from_this(),
80  boost::asio::placeholders::error,
81  boost::asio::placeholders::bytes_transferred));
82  }
83 
85  virtual void finishedReading(const boost::system::error_code& ec) {
86  // call the finished handler with the finished HTTP message
88  }
89 
91  virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
92 
94  HTTPRequestPtr m_http_msg;
95 
98 };
99 
100 
102 typedef boost::shared_ptr<HTTPRequestReader> HTTPRequestReaderPtr;
103 
104 
105 } // end namespace net
106 } // end namespace pion
107 
108 #endif