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::function2<void, HTTPRequestPtr, TCPConnectionPtr>    FinishedHandler;
00040 
00041     
00042     // default destructor
00043     virtual ~HTTPRequestReader() {}
00044     
00051     static inline boost::shared_ptr<HTTPRequestReader>
00052         create(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
00053     {
00054         return boost::shared_ptr<HTTPRequestReader>
00055             (new HTTPRequestReader(tcp_conn, handler));
00056     }
00057 
00058     
00059 protected:
00060 
00067     HTTPRequestReader(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
00068         : HTTPReader(true, tcp_conn), m_http_msg(new HTTPRequest),
00069         m_finished(handler)
00070     {
00071         m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
00072         setLogger(PION_GET_LOGGER("pion.net.HTTPRequestReader"));
00073     }
00074         
00076     virtual void readBytes(void) {
00077         getTCPConnection()->async_read_some(boost::bind(&HTTPRequestReader::consumeBytes,
00078                                                         shared_from_this(),
00079                                                         boost::asio::placeholders::error,
00080                                                         boost::asio::placeholders::bytes_transferred));
00081     }
00082 
00084     virtual void finishedReading(void) {
00085         // call the finished handler with the finished HTTP message
00086         m_finished(m_http_msg, getTCPConnection());
00087     }
00088     
00090     virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
00091 
00093     HTTPRequestPtr              m_http_msg;
00094 
00096     FinishedHandler             m_finished;
00097 };
00098 
00099 
00101 typedef boost::shared_ptr<HTTPRequestReader>    HTTPRequestReaderPtr;
00102 
00103 
00104 }   // end namespace net
00105 }   // end namespace pion
00106 
00107 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7