net/include/pion/net/HTTPWriter.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_HTTPWRITER_HEADER__
00011 #define __PION_HTTPWRITER_HEADER__
00012 
00013 #include <vector>
00014 #include <string>
00015 #include <exception>
00016 #include <boost/shared_ptr.hpp>
00017 #include <boost/function.hpp>
00018 #include <boost/function/function0.hpp>
00019 #include <boost/function/function2.hpp>
00020 #include <boost/asio.hpp>
00021 #include <boost/noncopyable.hpp>
00022 #include <pion/PionConfig.hpp>
00023 #include <pion/PionLogger.hpp>
00024 #include <pion/net/HTTPMessage.hpp>
00025 #include <pion/net/TCPConnection.hpp>
00026 
00027 
00028 namespace pion {    // begin namespace pion
00029 namespace net {     // begin namespace net (Pion Network Library)
00030 
00034 class PION_NET_API HTTPWriter :
00035     private boost::noncopyable
00036 {
00037 protected:
00038     
00040     typedef boost::function0<void>  FinishedHandler;
00041 
00043     typedef boost::function2<void,const boost::system::error_code&,std::size_t> WriteHandler;
00044     
00045     
00052     HTTPWriter(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
00053         : m_logger(PION_GET_LOGGER("pion.net.HTTPWriter")),
00054         m_tcp_conn(tcp_conn), m_content_length(0), m_stream_is_empty(true), 
00055         m_client_supports_chunks(true), m_sending_chunks(false),
00056         m_sent_headers(false), m_finished(handler)
00057     {}
00058     
00065     virtual void handleWrite(const boost::system::error_code& write_error,
00066                      std::size_t bytes_written) = 0;
00067 
00068     
00074     virtual void prepareBuffersForSend(HTTPMessage::WriteBuffers& write_buffers) = 0;
00075                                       
00077     virtual WriteHandler bindToWriteHandler(void) = 0;
00078     
00080     inline void finishedWriting(void) { if (m_finished) m_finished(); }
00081     
00082     
00083 public:
00084 
00086     class LostConnectionException : public std::exception {
00087     public:
00088         virtual const char* what() const throw() {
00089             return "Lost TCP connection while or before sending an HTTP message";
00090         }
00091     };
00092         
00094     virtual ~HTTPWriter() {}
00095 
00097     inline void clear(void) {
00098         m_content_buffers.clear();
00099         m_binary_cache.clear();
00100         m_text_cache.clear();
00101         m_content_stream.str("");
00102         m_stream_is_empty = true;
00103         m_content_length = 0;
00104     }
00105 
00111     template <typename T>
00112     inline void write(const T& data) {
00113         m_content_stream << data;
00114         if (m_stream_is_empty) m_stream_is_empty = false;
00115     }
00116 
00123     inline void write(const void *data, size_t length) {
00124         if (length != 0) {
00125             flushContentStream();
00126             m_content_buffers.push_back(m_binary_cache.add(data, length));
00127             m_content_length += length;
00128         }
00129     }
00130     
00138     inline void writeNoCopy(const std::string& data) {
00139         if (! data.empty()) {
00140             flushContentStream();
00141             m_content_buffers.push_back(boost::asio::buffer(data));
00142             m_content_length += data.size();
00143         }
00144     }
00145     
00153     inline void writeNoCopy(void *data, size_t length) {
00154         if (length > 0) {
00155             flushContentStream();
00156             m_content_buffers.push_back(boost::asio::buffer(data, length));
00157             m_content_length += length;
00158         }
00159     }
00160 
00161     
00167     inline void send(void) {
00168         sendMoreData(false, bindToWriteHandler());
00169     }
00170     
00180     template <typename SendHandler>
00181     inline void send(SendHandler send_handler) {
00182         sendMoreData(false, send_handler);
00183     }
00184     
00195     template <typename SendHandler>
00196     inline void sendChunk(SendHandler send_handler) {
00197         m_sending_chunks = true;
00198         if (!supportsChunkedMessages()) {
00199             // sending data in chunks, but the client does not support chunking;
00200             // make sure that the connection will be closed when we are all done
00201             m_tcp_conn->setLifecycle(TCPConnection::LIFECYCLE_CLOSE);
00202         }
00203         // send more data
00204         sendMoreData(false, send_handler);
00205     }
00206 
00218     template <typename SendHandler>
00219     inline void sendFinalChunk(SendHandler send_handler) {
00220         m_sending_chunks = true;
00221         sendMoreData(true, send_handler);
00222     }
00223     
00231     inline void sendFinalChunk(void) {
00232         m_sending_chunks = true;
00233         sendMoreData(true, bindToWriteHandler());
00234     }
00235     
00236     
00238     inline TCPConnectionPtr& getTCPConnection(void) { return m_tcp_conn; }
00239 
00241     inline size_t getContentLength(void) const { return m_content_length; }
00242 
00244     inline void supportsChunkedMessages(bool b) { m_client_supports_chunks = b; }
00245     
00247     inline bool supportsChunkedMessages() const { return m_client_supports_chunks; }
00248 
00250     inline bool sendingChunkedMessage() const { return m_sending_chunks; }
00251     
00253     inline void setLogger(PionLogger log_ptr) { m_logger = log_ptr; }
00254     
00256     inline PionLogger getLogger(void) { return m_logger; }
00257 
00258     
00259 private:
00260 
00267     template <typename SendHandler>
00268     inline void sendMoreData(const bool send_final_chunk, SendHandler send_handler)
00269     {
00270         // make sure that we did not lose the TCP connection
00271         if (! m_tcp_conn->is_open()) throw LostConnectionException();
00272         // make sure that the content-length is up-to-date
00273         flushContentStream();
00274         // prepare the write buffers to be sent
00275         HTTPMessage::WriteBuffers write_buffers;
00276         prepareWriteBuffers(write_buffers, send_final_chunk);
00277         // send data in the write buffers
00278         m_tcp_conn->async_write(write_buffers, send_handler);
00279     }
00280     
00287     void prepareWriteBuffers(HTTPMessage::WriteBuffers &write_buffers,
00288                              const bool send_final_chunk);
00289     
00291     inline void flushContentStream(void) {
00292         if (! m_stream_is_empty) {
00293             std::string string_to_add(m_content_stream.str());
00294             if (! string_to_add.empty()) {
00295                 m_content_stream.str("");
00296                 m_content_length += string_to_add.size();
00297                 m_text_cache.push_back(string_to_add);
00298                 m_content_buffers.push_back(boost::asio::buffer(m_text_cache.back()));
00299             }
00300             m_stream_is_empty = true;
00301         }
00302     }
00303     
00304     
00306     class BinaryCache : public std::vector<std::pair<const char *, size_t> > {
00307     public:
00308         ~BinaryCache() {
00309             for (iterator i=begin(); i!=end(); ++i) {
00310                 delete[] i->first;
00311             }
00312         }
00313         inline boost::asio::const_buffer add(const void *ptr, const size_t size) {
00314             char *data_ptr = new char[size];
00315             memcpy(data_ptr, ptr, size);
00316             push_back( std::make_pair(data_ptr, size) );
00317             return boost::asio::buffer(data_ptr, size);
00318         }
00319     };
00320     
00322     typedef std::list<std::string>              TextCache;
00323 
00324     
00326     PionLogger                              m_logger;
00327 
00329     TCPConnectionPtr                        m_tcp_conn;
00330     
00332     HTTPMessage::WriteBuffers               m_content_buffers;
00333     
00335     BinaryCache                             m_binary_cache;
00336 
00338     TextCache                               m_text_cache;
00339     
00341     std::ostringstream                      m_content_stream;
00342     
00344     size_t                                  m_content_length;
00345 
00347     bool                                    m_stream_is_empty;
00348     
00350     bool                                    m_client_supports_chunks;
00351     
00353     bool                                    m_sending_chunks;
00354     
00356     bool                                    m_sent_headers;
00357 
00359     FinishedHandler                         m_finished;
00360 };
00361 
00362 
00364 typedef boost::shared_ptr<HTTPWriter>   HTTPWriterPtr;
00365 
00366 
00368 template <typename T>
00369 HTTPWriterPtr& operator<<(HTTPWriterPtr& writer, const T& data) {
00370     writer->write(data);
00371     return writer;
00372 }
00373 
00374 
00375 }   // end namespace net
00376 }   // end namespace pion
00377 
00378 #endif

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