request_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_REQUEST_WRITER_HEADER__
#define __PION_HTTP_REQUEST_WRITER_HEADER__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <pion/config.hpp>
#include <pion/http/writer.hpp>
#include <pion/http/request.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class request_writer :
public http::writer,
public boost::enable_shared_from_this<request_writer>
{
public:
virtual ~request_writer() {}
static inline boost::shared_ptr<request_writer> create(const tcp::connection_ptr& tcp_conn,
finished_handler_t handler = finished_handler_t())
{
return boost::shared_ptr<request_writer>(new request_writer(tcp_conn, handler));
}
static inline boost::shared_ptr<request_writer> create(const tcp::connection_ptr& tcp_conn,
const http::request_ptr& http_request_ptr,
finished_handler_t handler = finished_handler_t())
{
return boost::shared_ptr<request_writer>(new request_writer(tcp_conn, http_request_ptr, handler));
}
inline http::request& get_request(void) { return *m_http_request; }
protected:
request_writer(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
: http::writer(tcp_conn, handler), m_http_request(new http::request)
{
set_logger(PION_GET_LOGGER("pion.http.request_writer"));
}
request_writer(const tcp::connection_ptr& tcp_conn, const http::request_ptr& http_request_ptr,
finished_handler_t handler)
: http::writer(tcp_conn, handler), m_http_request(http_request_ptr)
{
set_logger(PION_GET_LOGGER("pion.http.request_writer"));
// check if we should initialize the payload content using
// the request's content buffer
if (m_http_request->get_content_length() > 0
&& m_http_request->get_content() != NULL
&& m_http_request->get_content()[0] != '\0')
{
write_no_copy(m_http_request->get_content(),
m_http_request->get_content_length());
}
}
virtual void prepare_buffers_for_send(http::message::write_buffers_t& write_buffers) {
if (get_content_length() > 0)
m_http_request->set_content_length(get_content_length());
m_http_request->prepare_buffers_for_send(write_buffers,
get_connection()->get_keep_alive(),
sending_chunked_message());
}
virtual write_handler_t bind_to_write_handler(void) {
return boost::bind(&request_writer::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred);
}
virtual void handle_write(const boost::system::error_code& write_error,
std::size_t bytes_written)
{
logger log_ptr(get_logger());
if (! write_error) {
// request sent OK
if (sending_chunked_message()) {
PION_LOG_DEBUG(log_ptr, "Sent HTTP request chunk of " << bytes_written << " bytes");
clear();
} else {
PION_LOG_DEBUG(log_ptr, "Sent HTTP request of " << bytes_written << " bytes");
}
}
finished_writing(write_error);
}
private:
http::request_ptr m_http_request;
std::string m_request_line;
};
typedef boost::shared_ptr<request_writer> request_writer_ptr;
template <typename T>
const request_writer_ptr& operator<<(const request_writer_ptr& writer, const T& data) {
writer->write(data);
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::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::request
Definition: request.hpp:26
pion::http::writer::writer
writer(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: writer.hpp:52
pion::http::request_writer::create
static boost::shared_ptr< request_writer > create(const tcp::connection_ptr &tcp_conn, finished_handler_t handler=finished_handler_t())
Definition: request_writer.hpp:48
pion::http::writer::get_logger
logger get_logger(void)
returns the logger currently in use
Definition: writer.hpp:255
pion::http::request_writer::handle_write
virtual void handle_write(const boost::system::error_code &write_error, std::size_t bytes_written)
Definition: request_writer.hpp:139
pion::http::request_writer::~request_writer
virtual ~request_writer()
default destructor
Definition: request_writer.hpp:37
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::request_writer::get_request
http::request & get_request(void)
returns a non-const reference to the request that will be sent
Definition: request_writer.hpp:72
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::logger
Definition: logger.hpp:238
pion::http::request_writer
Definition: request_writer.hpp:30
pion::http::request_writer::request_writer
request_writer(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: request_writer.hpp:83
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::request_writer::prepare_buffers_for_send
virtual void prepare_buffers_for_send(http::message::write_buffers_t &write_buffers)
Definition: request_writer.hpp:118
pion::http::request_writer::create
static boost::shared_ptr< request_writer > create(const tcp::connection_ptr &tcp_conn, const http::request_ptr &http_request_ptr, finished_handler_t handler=finished_handler_t())
Definition: request_writer.hpp:64
pion::http::request_writer::bind_to_write_handler
virtual write_handler_t bind_to_write_handler(void)
returns a function bound to http::writer::handle_write()
Definition: request_writer.hpp:127
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::request_writer::request_writer
request_writer(const tcp::connection_ptr &tcp_conn, const http::request_ptr &http_request_ptr, finished_handler_t handler)
Definition: request_writer.hpp:96
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
Definition: writer.hpp:34