request_reader.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_READER_HEADER__
#define __PION_HTTP_REQUEST_READER_HEADER__
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/function/function2.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <pion/config.hpp>
#include <pion/http/request.hpp>
#include <pion/http/reader.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class request_reader :
public http::reader,
public boost::enable_shared_from_this<request_reader>
{
public:
typedef boost::function3<void, http::request_ptr, tcp::connection_ptr,
const boost::system::error_code&> finished_handler_t;
// default destructor
virtual ~request_reader() {}
static inline boost::shared_ptr<request_reader>
create(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
{
return boost::shared_ptr<request_reader>
(new request_reader(tcp_conn, handler));
}
inline void set_headers_parsed_callback(finished_handler_t& h) { m_parsed_headers = h; }
protected:
request_reader(const tcp::connection_ptr& tcp_conn, finished_handler_t handler)
: http::reader(true, tcp_conn), m_http_msg(new http::request),
m_finished(handler)
{
m_http_msg->set_remote_ip(tcp_conn->get_remote_ip());
set_logger(PION_GET_LOGGER("pion.http.request_reader"));
}
virtual void read_bytes(void) {
get_connection()->async_read_some(boost::bind(&request_reader::consume_bytes,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
virtual void finished_parsing_headers(const boost::system::error_code& ec) {
// call the finished headers handler with the HTTP message
if (m_parsed_headers) m_parsed_headers(m_http_msg, get_connection(), ec);
}
virtual void finished_reading(const boost::system::error_code& ec) {
// call the finished handler with the finished HTTP message
if (m_finished) m_finished(m_http_msg, get_connection(), ec);
}
virtual http::message& get_message(void) { return *m_http_msg; }
http::request_ptr m_http_msg;
finished_handler_t m_finished;
finished_handler_t m_parsed_headers;
};
typedef boost::shared_ptr<request_reader> request_reader_ptr;
} // end namespace http
} // end namespace pion
#endif
pion::http::reader::consume_bytes
void consume_bytes(void)
Consumes bytes that have been read using an HTTP parser.
Definition: http_reader.cpp:66
pion::http::request_reader::get_message
virtual http::message & get_message(void)
Returns a reference to the HTTP message being parsed.
Definition: request_reader.hpp:100
pion::http::request_reader::finished_parsing_headers
virtual void finished_parsing_headers(const boost::system::error_code &ec)
Called after we have finished parsing the HTTP message headers.
Definition: request_reader.hpp:88
pion::http::request_reader::m_finished
finished_handler_t m_finished
function called after the HTTP message has been parsed
Definition: request_reader.hpp:106
pion::http::request
Definition: request.hpp:26
pion::http::request_reader::create
static boost::shared_ptr< request_reader > create(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: request_reader.hpp:53
pion::http::request_reader::m_http_msg
http::request_ptr m_http_msg
The new HTTP message container being created.
Definition: request_reader.hpp:103
pion::http::reader
Definition: reader.hpp:28
pion::http::reader::get_connection
tcp::connection_ptr & get_connection(void)
returns a shared pointer to the TCP connection
Definition: reader.hpp:40
pion::http::request_reader::read_bytes
virtual void read_bytes(void)
Reads more bytes from the TCP connection.
Definition: request_reader.hpp:80
pion::http::request_reader::finished_handler_t
boost::function3< void, http::request_ptr, tcp::connection_ptr, const boost::system::error_code & > finished_handler_t
function called after the HTTP message has been parsed
Definition: request_reader.hpp:40
pion::http::message
Definition: message.hpp:55
pion::http::request_reader::finished_reading
virtual void finished_reading(const boost::system::error_code &ec)
Called after we have finished reading/parsing the HTTP message.
Definition: request_reader.hpp:94
pion::http::request_reader::set_headers_parsed_callback
void set_headers_parsed_callback(finished_handler_t &h)
sets a function to be called after HTTP headers have been parsed
Definition: request_reader.hpp:60
pion::http::request_reader::m_parsed_headers
finished_handler_t m_parsed_headers
function called after the HTTP message headers have been parsed
Definition: request_reader.hpp:109
pion
Definition: admin_rights.hpp:19
pion::http::request_reader
Definition: request_reader.hpp:31
pion::http::parser::set_logger
void set_logger(logger log_ptr)
sets the logger to be used
Definition: parser.hpp:294
pion::http::request_reader::request_reader
request_reader(const tcp::connection_ptr &tcp_conn, finished_handler_t handler)
Definition: request_reader.hpp:71