pion-net  4.0.9
HTTPRequestWriter.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_HTTPREQUESTWRITER_HEADER__
11 #define __PION_HTTPREQUESTWRITER_HEADER__
12 
13 #include <boost/asio.hpp>
14 #include <boost/bind.hpp>
15 #include <boost/noncopyable.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18 #include <pion/PionConfig.hpp>
19 #include <pion/net/HTTPWriter.hpp>
20 #include <pion/net/HTTPRequest.hpp>
21 
22 
23 namespace pion { // begin namespace pion
24 namespace net { // begin namespace net (Pion Network Library)
25 
30  public HTTPWriter,
31  public boost::enable_shared_from_this<HTTPRequestWriter>
32 {
33 public:
34 
36  virtual ~HTTPRequestWriter() {}
37 
47  static inline boost::shared_ptr<HTTPRequestWriter> create(TCPConnectionPtr& tcp_conn,
48  FinishedHandler handler = FinishedHandler())
49  {
50  return boost::shared_ptr<HTTPRequestWriter>(new HTTPRequestWriter(tcp_conn, handler));
51  }
52 
63  static inline boost::shared_ptr<HTTPRequestWriter> create(TCPConnectionPtr& tcp_conn,
64  HTTPRequestPtr& http_request,
65  FinishedHandler handler = FinishedHandler())
66  {
67  return boost::shared_ptr<HTTPRequestWriter>(new HTTPRequestWriter(tcp_conn, http_request, handler));
68  }
69 
71  inline HTTPRequest& getRequest(void) { return *m_http_request; }
72 
73 
74 protected:
75 
83  HTTPRequestWriter(TCPConnectionPtr& tcp_conn, FinishedHandler handler)
84  : HTTPWriter(tcp_conn, handler), m_http_request(new HTTPRequest)
85  {
86  setLogger(PION_GET_LOGGER("pion.net.HTTPRequestWriter"));
87  }
88 
96  HTTPRequestWriter(TCPConnectionPtr& tcp_conn, HTTPRequestPtr& http_request,
97  FinishedHandler handler)
98  : HTTPWriter(tcp_conn, handler), m_http_request(http_request)
99  {
100  setLogger(PION_GET_LOGGER("pion.net.HTTPRequestWriter"));
101  // check if we should initialize the payload content using
102  // the request's content buffer
103  if (m_http_request->getContentLength() > 0
104  && m_http_request->getContent() != NULL
105  && m_http_request->getContent()[0] != '\0')
106  {
107  writeNoCopy(m_http_request->getContent(),
108  m_http_request->getContentLength());
109  }
110  }
111 
112 
118  virtual void prepareBuffersForSend(HTTPMessage::WriteBuffers& write_buffers) {
119  if (getContentLength() > 0)
120  m_http_request->setContentLength(getContentLength());
121  m_http_request->prepareBuffersForSend(write_buffers,
122  getTCPConnection()->getKeepAlive(),
124  }
125 
128  return boost::bind(&HTTPRequestWriter::handleWrite, shared_from_this(),
129  boost::asio::placeholders::error,
130  boost::asio::placeholders::bytes_transferred);
131  }
132 
139  virtual void handleWrite(const boost::system::error_code& write_error,
140  std::size_t bytes_written)
141  {
142  PionLogger log_ptr(getLogger());
143  if (! write_error) {
144  // request sent OK
145  if (sendingChunkedMessage()) {
146  PION_LOG_DEBUG(log_ptr, "Sent HTTP request chunk of " << bytes_written << " bytes");
147  clear();
148  } else {
149  PION_LOG_DEBUG(log_ptr, "Sent HTTP request of " << bytes_written << " bytes");
150  }
151  }
152  finishedWriting(write_error);
153  }
154 
155 
156 private:
157 
159  HTTPRequestPtr m_http_request;
160 
162  std::string m_request_line;
163 };
164 
165 
167 typedef boost::shared_ptr<HTTPRequestWriter> HTTPRequestWriterPtr;
168 
169 
171 template <typename T>
172 const HTTPRequestWriterPtr& operator<<(const HTTPRequestWriterPtr& writer, const T& data) {
173  writer->write(data);
174  return writer;
175 }
176 
177 
178 } // end namespace net
179 } // end namespace pion
180 
181 #endif
HTTPRequestWriter(TCPConnectionPtr &tcp_conn, HTTPRequestPtr &http_request, FinishedHandler handler)
void finishedWriting(const boost::system::error_code &ec)
called after we have finished sending the HTTP message
Definition: HTTPWriter.hpp:79
virtual void prepareBuffersForSend(HTTPMessage::WriteBuffers &write_buffers)
bool sendingChunkedMessage() const
returns true if we are sending a chunked message to the client
Definition: HTTPWriter.hpp:243
HTTPRequest & getRequest(void)
returns a non-const reference to the request that will be sent
void writeNoCopy(const std::string &data)
Definition: HTTPWriter.hpp:131
virtual ~HTTPRequestWriter()
default destructor
void setLogger(PionLogger log_ptr)
sets the logger to be used
Definition: HTTPWriter.hpp:246
boost::function1< void, const boost::system::error_code & > FinishedHandler
function called after the HTTP message has been sent
Definition: HTTPWriter.hpp:39
std::vector< boost::asio::const_buffer > WriteBuffers
data type for I/O write buffers (these wrap existing data to be sent)
Definition: HTTPMessage.hpp:43
virtual WriteHandler bindToWriteHandler(void)
returns a function bound to HTTPWriter::handleWrite()
size_t getContentLength(void) const
returns the length of the payload content (in bytes)
Definition: HTTPWriter.hpp:234
boost::function2< void, const boost::system::error_code &, std::size_t > WriteHandler
data type for a function that handles write operations
Definition: HTTPWriter.hpp:42
PionLogger getLogger(void)
returns the logger currently in use
Definition: HTTPWriter.hpp:249
static boost::shared_ptr< HTTPRequestWriter > create(TCPConnectionPtr &tcp_conn, FinishedHandler handler=FinishedHandler())
virtual void handleWrite(const boost::system::error_code &write_error, std::size_t bytes_written)
HTTPRequestWriter(TCPConnectionPtr &tcp_conn, FinishedHandler handler)
static boost::shared_ptr< HTTPRequestWriter > create(TCPConnectionPtr &tcp_conn, HTTPRequestPtr &http_request, FinishedHandler handler=FinishedHandler())
void clear(void)
clears out all of the memory buffers used to cache payload content data
Definition: HTTPWriter.hpp:90
TCPConnectionPtr & getTCPConnection(void)
returns a shared pointer to the TCP connection
Definition: HTTPWriter.hpp:231