# net/include/pion/net/HTTPResponseReader.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_HTTPRESPONSEREADER_HEADER__
    00011 #define __PION_HTTPRESPONSEREADER_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/HTTPResponse.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 HTTPResponseReader :
    00032     public HTTPReader,
    00033     public boost::enable_shared_from_this<HTTPResponseReader>
    00034 {
    00035
    00036 public:
    00037
    00039     typedef boost::function3<void, HTTPResponsePtr, TCPConnectionPtr,
    00040         const boost::system::error_code&>   FinishedHandler;
    00041
    00042
    00043     // default destructor
    00044     virtual ~HTTPResponseReader() {}
    00045
    00053     static inline boost::shared_ptr<HTTPResponseReader>
    00054         create(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
    00055                FinishedHandler handler)
    00056     {
    00057         return boost::shared_ptr<HTTPResponseReader>
    00058             (new HTTPResponseReader(tcp_conn, http_request, handler));
    00059     }
    00060
    00061
    00062 protected:
    00063
    00071     HTTPResponseReader(TCPConnectionPtr& tcp_conn, const HTTPRequest& http_request,
    00072                        FinishedHandler handler)
    00073         : HTTPReader(false, tcp_conn), m_http_msg(new HTTPResponse(http_request)),
    00074         m_finished(handler)
    00075     {
    00076         m_http_msg->setRemoteIp(tcp_conn->getRemoteIp());
    00077         setLogger(PION_GET_LOGGER("pion.net.HTTPResponseReader"));
    00078     }
    00079
    00081     virtual void readBytes(void) {
    00082         getTCPConnection()->async_read_some(boost::bind(&HTTPResponseReader::consumeBytes,
    00083                                                         shared_from_this(),
    00084                                                         boost::asio::placeholders::error,
    00085                                                         boost::asio::placeholders::bytes_transferred));
    00086     }
    00087
    00089     virtual void finishedReading(const boost::system::error_code& ec) {
    00090         // call the finished handler with the finished HTTP message
    00091         if (m_finished) m_finished(m_http_msg, getTCPConnection(), ec);
    00092     }
    00093
    00095     virtual HTTPMessage& getMessage(void) { return *m_http_msg; }
    00096
    00097
    00099     HTTPResponsePtr             m_http_msg;
    00100
    00102     FinishedHandler             m_finished;
    00103 };
    00104
    00105
    00107 typedef boost::shared_ptr<HTTPResponseReader>   HTTPResponseReaderPtr;
    00108
    00109
    00110 }   // end namespace net
    00111 }   // end namespace pion
    00112
    00113 #endif