writer.hpp
// ---------------------------------------------------------------------
// pion: a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2014 Splunk Inc. (https://github.com/splunk/pion)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//
#ifndef __PION_HTTP_WRITER_HEADER__
#define __PION_HTTP_WRITER_HEADER__
#include <vector>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/function/function0.hpp>
#include <boost/function/function2.hpp>
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <pion/config.hpp>
#include <pion/logger.hpp>
#include <pion/tcp/connection.hpp>
#include <pion/http/message.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class PION_API writer :
private boost::noncopyable
{
protected:
typedef boost::function1<void,const boost::system::error_code&> finished_handler_t;
typedef boost::function2<void,const boost::system::error_code&,std::size_t> write_handler_t;
writer(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
: m_logger(PION_GET_LOGGER("pion.http.writer")),
m_tcp_conn(tcp_conn), m_content_length(0), m_stream_is_empty(true),
m_client_supports_chunks(true), m_sending_chunks(false),
m_sent_headers(false), m_finished(handler)
{}
virtual void handle_write(const boost::system::error_code& write_error,
std::size_t bytes_written) = 0;
virtual void prepare_buffers_for_send(http::message::write_buffers_t& write_buffers) = 0;
virtual write_handler_t bind_to_write_handler(void) = 0;
inline void finished_writing(const boost::system::error_code& ec) {
if (m_finished) m_finished(ec);
}
public:
virtual ~writer() {}
inline void clear(void) {
m_content_buffers.clear();
m_binary_cache.clear();
m_text_cache.clear();
m_content_stream.str("");
m_stream_is_empty = true;
m_content_length = 0;
}
template <typename T>
inline void write(const T& data) {
m_content_stream << data;
if (m_stream_is_empty) m_stream_is_empty = false;
}
inline void write(std::ostream& (*iomanip)(std::ostream&)) {
m_content_stream << iomanip;
if (m_stream_is_empty) m_stream_is_empty = false;
}
inline void write(const void *data, size_t length) {
if (length != 0) {
flush_content_stream();
m_content_buffers.push_back(m_binary_cache.add(data, length));
m_content_length += length;
}
}
inline void write_no_copy(const std::string& data) {
if (! data.empty()) {
flush_content_stream();
m_content_buffers.push_back(boost::asio::buffer(data));
m_content_length += data.size();
}
}
inline void write_no_copy(void *data, size_t length) {
if (length > 0) {
flush_content_stream();
m_content_buffers.push_back(boost::asio::buffer(data, length));
m_content_length += length;
}
}
inline void send(void) {
send_more_data(false, bind_to_write_handler());
}
template <typename SendHandler>
inline void send(SendHandler send_handler) {
send_more_data(false, send_handler);
}
template <typename SendHandler>
inline void send_chunk(SendHandler send_handler) {
m_sending_chunks = true;
if (!supports_chunked_messages()) {
// sending data in chunks, but the client does not support chunking;
// make sure that the connection will be closed when we are all done
m_tcp_conn->set_lifecycle(tcp::connection::LIFECYCLE_CLOSE);
}
// send more data
send_more_data(false, send_handler);
}
template <typename SendHandler>
inline void send_final_chunk(SendHandler send_handler) {
m_sending_chunks = true;
send_more_data(true, send_handler);
}
inline void send_final_chunk(void) {
m_sending_chunks = true;
send_more_data(true, bind_to_write_handler());
}
inline tcp::connection_ptr& get_connection(void) { return m_tcp_conn; }
inline size_t get_content_length(void) const { return m_content_length; }
inline void supports_chunked_messages(bool b) { m_client_supports_chunks = b; }
inline bool supports_chunked_messages() const { return m_client_supports_chunks; }
inline bool sending_chunked_message() const { return m_sending_chunks; }
inline void set_logger(logger log_ptr) { m_logger = log_ptr; }
inline logger get_logger(void) { return m_logger; }
private:
template <typename SendHandler>
inline void send_more_data(const bool send_final_chunk, SendHandler send_handler)
{
// make sure that we did not lose the TCP connection
if (m_tcp_conn->is_open()) {
// make sure that the content-length is up-to-date
flush_content_stream();
// prepare the write buffers to be sent
http::message::write_buffers_t write_buffers;
prepare_write_buffers(write_buffers, send_final_chunk);
// send data in the write buffers
m_tcp_conn->async_write(write_buffers, send_handler);
} else {
finished_writing(boost::asio::error::connection_reset);
}
}
void prepare_write_buffers(http::message::write_buffers_t &write_buffers,
const bool send_final_chunk);
inline void flush_content_stream(void) {
if (! m_stream_is_empty) {
std::string string_to_add(m_content_stream.str());
if (! string_to_add.empty()) {
m_content_stream.str("");
m_content_length += string_to_add.size();
m_text_cache.push_back(string_to_add);
m_content_buffers.push_back(boost::asio::buffer(m_text_cache.back()));
}
m_stream_is_empty = true;
}
}
class binary_cache_t : public std::vector<std::pair<const char *, size_t> > {
public:
~binary_cache_t() {
for (iterator i=begin(); i!=end(); ++i) {
delete[] i->first;
}
}
inline boost::asio::const_buffer add(const void *ptr, const size_t size) {
char *data_ptr = new char[size];
memcpy(data_ptr, ptr, size);
push_back( std::make_pair(data_ptr, size) );
return boost::asio::buffer(data_ptr, size);
}
};
typedef std::list<std::string> text_cache_t;
logger m_logger;
tcp::connection_ptr m_tcp_conn;
http::message::write_buffers_t m_content_buffers;
binary_cache_t m_binary_cache;
text_cache_t m_text_cache;
std::ostringstream m_content_stream;
size_t m_content_length;
bool m_stream_is_empty;
bool m_client_supports_chunks;
bool m_sending_chunks;
bool m_sent_headers;
finished_handler_t m_finished;
};
typedef boost::shared_ptr<writer> writer_ptr;
template <typename T>
inline const writer_ptr& operator<<(const writer_ptr& writer, const T& data) {
writer->write(data);
return writer;
}
inline const writer_ptr& operator<<(const writer_ptr& writer, std::ostream& (*iomanip)(std::ostream&)) {
writer->write(iomanip);
return writer;
}
} // end namespace http
} // end namespace pion
#endif
pion::http::writer::clear
void clear(void)
clears out all of the memory buffers used to cache payload content data
Definition: writer.hpp:91
pion::http::writer::send
void send(void)
Definition: writer.hpp:166
pion::http::writer::write_handler_t
boost::function2< void, const boost::system::error_code &, std::size_t > write_handler_t
data type for a function that handles write operations
Definition: writer.hpp:43
pion::http::writer::get_content_length
size_t get_content_length(void) const
returns the length of the payload content (in bytes)
Definition: writer.hpp:240
pion::http::writer::write
void write(const void *data, size_t length)
Definition: writer.hpp:122
pion::http::writer::writer
writer(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: writer.hpp:52
pion::http::writer::get_logger
logger get_logger(void)
returns the logger currently in use
Definition: writer.hpp:255
pion::http::writer::write
void write(const T &data)
Definition: writer.hpp:106
pion::http::writer::finished_writing
void finished_writing(const boost::system::error_code &ec)
called after we have finished sending the HTTP message
Definition: writer.hpp:80
pion::http::writer::get_connection
tcp::connection_ptr & get_connection(void)
returns a shared pointer to the TCP connection
Definition: writer.hpp:237
pion::http::writer::finished_handler_t
boost::function1< void, const boost::system::error_code & > finished_handler_t
function called after the HTTP message has been sent
Definition: writer.hpp:40
pion::http::writer::send
void send(SendHandler send_handler)
Definition: writer.hpp:180
pion::http::writer::supports_chunked_messages
void supports_chunked_messages(bool b)
sets whether or not the client supports chunked messages
Definition: writer.hpp:243
pion::logger
Definition: logger.hpp:238
pion::http::writer::write_no_copy
void write_no_copy(void *data, size_t length)
Definition: writer.hpp:152
pion
Definition: admin_rights.hpp:19
pion::http::writer::write_no_copy
void write_no_copy(const std::string &data)
Definition: writer.hpp:137
pion::http::writer::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: writer.hpp:252
pion::http::writer::send_final_chunk
void send_final_chunk(SendHandler send_handler)
Definition: writer.hpp:218
pion::http::writer::~writer
virtual ~writer()
default destructor
Definition: writer.hpp:88
pion::http::writer::send_final_chunk
void send_final_chunk(void)
Definition: writer.hpp:230
pion::http::writer::sending_chunked_message
bool sending_chunked_message() const
returns true if we are sending a chunked message to the client
Definition: writer.hpp:249
pion::http::writer::send_chunk
void send_chunk(SendHandler send_handler)
Definition: writer.hpp:195
pion::http::message::write_buffers_t
std::vector< boost::asio::const_buffer > write_buffers_t
data type for I/O write buffers (these wrap existing data to be sent)
Definition: message.hpp:61
pion::http::writer::supports_chunked_messages
bool supports_chunked_messages() const
returns true if the client supports chunked messages
Definition: writer.hpp:246
pion::http::writer
Definition: writer.hpp:34