pion-net  4.0.9
net/include/pion/net/HTTPRequestReader.hpp
00001 // ------------------------------------------------------------------
00002 // pion-net: a C++ framework for building lightweight HTTP interfaces
00003 // ------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
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_HTTPREQUESTREADER_HEADER__
00011 #define __PION_HTTPREQUESTREADER_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/PionConfig.hpp>
00020 #include <pion/net/HTTPRequest.hpp>
00021 #include <pion/net/HTTPReader.hpp>
00022 
00023 
00024 namespace pion {    // begin namespace pion
00025 namespace net {     // begin namespace net (Pion Network Library)
00026 
00027 
00031 class HTTPRequestReader :
00032     public HTTPReader,
00033     public boost::enable_shared_from_this<HTTPRequestReader>
00034 {
00035 
00036 public:
00037 
00039     typedef boost::function3<void, HTTPRequestPtr, TCPConnectionPtr,
00040         const boost::system::error_code&>   FinishedHandler;
00041 
00042     
00043     // default destructor
00044     virtual ~HTTPRequestReader() {}
00045     
00052     static inline boost::shared_ptr<HTTPRequestReader>
00053         create(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
00054     {
00055         return boost::shared_ptr<HTTPRequestReader>
00056             (new HTTPRequestReader(tcp_conn, handler));
00057     }
00058 
00059     
00060 protected:
00061 
00068     HTTPRequestReader(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
00069         : HTTPReader(true, tcp_conn), m_http_msg(new HTTPRequest),
00070         m_finished(handler)
00071     {
00072         m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
00073         setLogger(PION_GET_LOGGER("pion.net.HTTPRequestReader"));
00074     }
00075         
00077     virtual void readBytes(void) {
00078         getTCPConnection()->async_read_some(boost::bind(&HTTPRequestReader::consumeBytes,
00079                                                         shared_from_this(),
00080                                                         boost::asio::placeholders::error,
00081                                                         boost::asio::placeholders::bytes_transferred));
00082     }
00083 
00085     virtual void finishedReading(const boost::system::error_code& ec) {
00086         // call the finished handler with the finished HTTP message
00087         if (m_finished) m_finished(m_http_msg, getTCPConnection(), ec);
00088     }
00089     
00091     virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
00092 
00094     HTTPRequestPtr              m_http_msg;
00095 
00097     FinishedHandler             m_finished;
00098 };
00099 
00100 
00102 typedef boost::shared_ptr<HTTPRequestReader>    HTTPRequestReaderPtr;
00103 
00104 
00105 }   // end namespace net
00106 }   // end namespace pion
00107 
00108 #endif