10 #ifndef __PION_HTTPRESPONSEWRITER_HEADER__
11 #define __PION_HTTPRESPONSEWRITER_HEADER__
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 #include <pion/net/HTTPResponse.hpp>
32 public boost::enable_shared_from_this<HTTPResponseWriter>
49 static inline boost::shared_ptr<HTTPResponseWriter>
create(TCPConnectionPtr& tcp_conn,
50 HTTPResponsePtr& http_response,
53 return boost::shared_ptr<HTTPResponseWriter>(
new HTTPResponseWriter(tcp_conn, http_response, handler));
66 static inline boost::shared_ptr<HTTPResponseWriter>
create(TCPConnectionPtr& tcp_conn,
70 return boost::shared_ptr<HTTPResponseWriter>(
new HTTPResponseWriter(tcp_conn, http_request, handler));
88 :
HTTPWriter(tcp_conn, handler), m_http_response(http_response)
90 setLogger(PION_GET_LOGGER(
"pion.net.HTTPResponseWriter"));
92 supportsChunkedMessages(m_http_response->getChunksSupported());
95 if (http_response->getContentLength() > 0
96 && http_response->getContent() != NULL
97 && http_response->getContent()[0] !=
'\0')
99 writeNoCopy(http_response->getContent(), http_response->getContentLength());
114 setLogger(PION_GET_LOGGER(
"pion.net.HTTPResponseWriter"));
116 supportsChunkedMessages(m_http_response->getChunksSupported());
126 if (getContentLength() > 0)
127 m_http_response->setContentLength(getContentLength());
128 m_http_response->prepareBuffersForSend(write_buffers,
129 getTCPConnection()->getKeepAlive(),
130 sendingChunkedMessage());
136 boost::asio::placeholders::error,
137 boost::asio::placeholders::bytes_transferred);
146 virtual void handleWrite(
const boost::system::error_code& write_error,
147 std::size_t bytes_written)
152 if (sendingChunkedMessage()) {
153 PION_LOG_DEBUG(log_ptr,
"Sent HTTP response chunk of " << bytes_written <<
" bytes");
155 PION_LOG_DEBUG(log_ptr,
"Sent HTTP response of " << bytes_written <<
" bytes ("
156 << (getTCPConnection()->getKeepAlive() ?
"keeping alive)" :
"closing)"));
159 finishedWriting(write_error);
166 HTTPResponsePtr m_http_response;
169 std::string m_response_line;
174 typedef boost::shared_ptr<HTTPResponseWriter> HTTPResponseWriterPtr;
178 template <
typename T>
179 const HTTPResponseWriterPtr& operator<<(
const HTTPResponseWriterPtr& writer,
const T& data) {
virtual void prepareBuffersForSend(HTTPMessage::WriteBuffers &write_buffers)
virtual ~HTTPResponseWriter()
default destructor
boost::function1< void, const boost::system::error_code & > FinishedHandler
function called after the HTTP message has been sent
std::vector< boost::asio::const_buffer > WriteBuffers
data type for I/O write buffers (these wrap existing data to be sent)
HTTPResponseWriter(TCPConnectionPtr &tcp_conn, HTTPResponsePtr &http_response, FinishedHandler handler)
virtual WriteHandler bindToWriteHandler(void)
returns a function bound to HTTPWriter::handleWrite()
boost::function2< void, const boost::system::error_code &, std::size_t > WriteHandler
data type for a function that handles write operations
HTTPResponse & getResponse(void)
returns a non-const reference to the response that will be sent
static boost::shared_ptr< HTTPResponseWriter > create(TCPConnectionPtr &tcp_conn, const HTTPRequest &http_request, FinishedHandler handler=FinishedHandler())
static boost::shared_ptr< HTTPResponseWriter > create(TCPConnectionPtr &tcp_conn, HTTPResponsePtr &http_response, FinishedHandler handler=FinishedHandler())
HTTPResponseWriter(TCPConnectionPtr &tcp_conn, const HTTPRequest &http_request, FinishedHandler handler)
virtual void handleWrite(const boost::system::error_code &write_error, std::size_t bytes_written)