PionNetworkLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pion::http::message Class Referenceabstract

#include <message.hpp>

Inheritance diagram for pion::http::message:
Collaboration diagram for pion::http::message:

Classes

class  content_buffer_t
 a simple helper class used to manage a fixed-size payload content buffer More...
 
struct  receive_error_t
 data type for library errors returned during receive() operations More...
 

Public Types

enum  data_status_t { STATUS_NONE, STATUS_TRUNCATED, STATUS_PARTIAL, STATUS_OK }
 defines message data integrity status codes More...
 
typedef std::vector
< boost::asio::const_buffer > 
write_buffers_t
 data type for I/O write buffers (these wrap existing data to be sent) More...
 
typedef std::vector< char > chunk_cache_t
 used to cache chunked data More...
 

Public Member Functions

 message (void)
 constructs a new HTTP message object More...
 
 message (const message &http_msg)
 copy constructor More...
 
messageoperator= (const message &http_msg)
 assignment operator More...
 
virtual ~message ()
 virtual destructor More...
 
virtual void clear (void)
 clears all message data More...
 
virtual bool is_content_length_implied (void) const =0
 should return true if the content length can be implied without headers More...
 
bool is_valid (void) const
 returns true if the message is valid More...
 
bool get_chunks_supported (void) const
 returns true if chunked transfer encodings are supported More...
 
boost::asio::ip::address & get_remote_ip (void)
 returns IP address of the remote endpoint More...
 
boost::uint16_t get_version_major (void) const
 returns the major HTTP version number More...
 
boost::uint16_t get_version_minor (void) const
 returns the minor HTTP version number More...
 
std::string get_version_string (void) const
 returns a string representation of the HTTP version (i.e. "HTTP/1.1") More...
 
size_t get_content_length (void) const
 returns the length of the payload content (in bytes) More...
 
bool is_chunked (void) const
 returns true if the message content is chunked More...
 
bool is_content_buffer_allocated () const
 returns true if buffer for content is allocated More...
 
std::size_t get_content_buffer_size () const
 returns size of allocated buffer More...
 
char * get_content (void)
 returns a pointer to the payload content, or empty string if there is none More...
 
const char * get_content (void) const
 returns a const pointer to the payload content, or empty string if there is none More...
 
chunk_cache_tget_chunk_cache (void)
 returns a reference to the chunk cache More...
 
const std::string & get_header (const std::string &key) const
 returns a value for the header if any are defined; otherwise, an empty string More...
 
ihash_multimapget_headers (void)
 returns a reference to the HTTP headers More...
 
bool has_header (const std::string &key) const
 returns true if at least one value for the header is defined More...
 
const std::string & get_cookie (const std::string &key) const
 
ihash_multimapget_cookies (void)
 returns the cookie parameters More...
 
bool has_cookie (const std::string &key) const
 
void add_cookie (const std::string &key, const std::string &value)
 
void change_cookie (const std::string &key, const std::string &value)
 
void delete_cookie (const std::string &key)
 
const std::string & get_first_line (void) const
 returns a string containing the first line for the HTTP message More...
 
bool has_missing_packets () const
 true if there were missing packets More...
 
void set_missing_packets (bool newVal)
 set to true when missing packets detected More...
 
bool has_data_after_missing_packets () const
 true if more data seen after the missing packets More...
 
void set_data_after_missing_packet (bool newVal)
 
void set_is_valid (bool b=true)
 sets whether or not the message is valid More...
 
void set_chunks_supported (bool b)
 set to true if chunked transfer encodings are supported More...
 
void set_remote_ip (const boost::asio::ip::address &ip)
 sets IP address of the remote endpoint More...
 
void set_version_major (const boost::uint16_t n)
 sets the major HTTP version number More...
 
void set_version_minor (const boost::uint16_t n)
 sets the minor HTTP version number More...
 
void set_content_length (size_t n)
 sets the length of the payload content (in bytes) More...
 
void set_do_not_send_content_length (void)
 if called, the content-length will not be sent in the HTTP headers More...
 
data_status_t get_status () const
 return the data receival status More...
 
void set_status (data_status_t newVal)
 
void update_content_length_using_header (void)
 sets the length of the payload content using the Content-Length header More...
 
void update_transfer_encoding_using_header (void)
 sets the transfer coding using the Transfer-Encoding header More...
 
char * create_content_buffer (void)
 
void set_content (const std::string &content)
 resets payload content to match the value of a string More...
 
void clear_content (void)
 clears payload content buffer More...
 
void set_content_type (const std::string &type)
 sets the content type for the message payload More...
 
void add_header (const std::string &key, const std::string &value)
 adds a value for the HTTP header named key More...
 
void change_header (const std::string &key, const std::string &value)
 changes the value for the HTTP header named key More...
 
void delete_header (const std::string &key)
 removes all values for the HTTP header named key More...
 
bool check_keep_alive (void) const
 returns true if the HTTP connection may be kept alive More...
 
void prepare_buffers_for_send (write_buffers_t &write_buffers, const bool keep_alive, const bool using_chunks)
 
std::size_t send (tcp::connection &tcp_conn, boost::system::error_code &ec, bool headers_only=false)
 
std::size_t receive (tcp::connection &tcp_conn, boost::system::error_code &ec, parser &http_parser)
 
std::size_t receive (tcp::connection &tcp_conn, boost::system::error_code &ec, bool headers_only=false, std::size_t max_content_length=static_cast< size_t >(-1))
 
std::size_t write (std::ostream &out, boost::system::error_code &ec, bool headers_only=false)
 
std::size_t read (std::istream &in, boost::system::error_code &ec, parser &http_parser)
 
std::size_t read (std::istream &in, boost::system::error_code &ec, bool headers_only=false, std::size_t max_content_length=static_cast< size_t >(-1))
 
void concatenate_chunks (void)
 
- Public Member Functions inherited from pion::http::types
virtual ~types ()
 virtual destructor More...
 

Protected Member Functions

void prepare_headers_for_send (const bool keep_alive, const bool using_chunks)
 
void append_headers (write_buffers_t &write_buffers)
 
virtual void append_cookie_headers (void)
 appends HTTP headers for any cookies defined by the http::message More...
 
void clear_first_line (void) const
 
virtual void update_first_line (void) const =0
 updates the string containing the first line for the HTTP message More...
 

Static Protected Member Functions

template<typename DictionaryType >
static const std::string & get_value (const DictionaryType &dict, const std::string &key)
 
template<typename DictionaryType >
static void change_value (DictionaryType &dict, const std::string &key, const std::string &value)
 
template<typename DictionaryType >
static void delete_value (DictionaryType &dict, const std::string &key)
 

Protected Attributes

std::string m_first_line
 

Additional Inherited Members

- Static Public Member Functions inherited from pion::http::types
static std::string get_date_string (const time_t t)
 converts time_t format into an HTTP-date string More...
 
static std::string make_query_string (const ihash_multimap &query_params)
 builds an HTTP query string from a collection of query parameters More...
 
static std::string make_set_cookie_header (const std::string &name, const std::string &value, const std::string &path, const bool has_max_age=false, const unsigned long max_age=0)
 
- Static Public Attributes inherited from pion::http::types
static const std::string STRING_EMPTY
 
static const std::string STRING_CRLF
 
static const std::string STRING_HTTP_VERSION
 
static const std::string HEADER_NAME_VALUE_DELIMITER
 
static const std::string COOKIE_NAME_VALUE_DELIMITER
 
static const std::string HEADER_HOST
 
static const std::string HEADER_COOKIE
 
static const std::string HEADER_SET_COOKIE
 
static const std::string HEADER_CONNECTION
 
static const std::string HEADER_CONTENT_TYPE
 
static const std::string HEADER_CONTENT_LENGTH
 
static const std::string HEADER_CONTENT_LOCATION
 
static const std::string HEADER_CONTENT_ENCODING
 
static const std::string HEADER_CONTENT_DISPOSITION
 
static const std::string HEADER_LAST_MODIFIED
 
static const std::string HEADER_IF_MODIFIED_SINCE
 
static const std::string HEADER_TRANSFER_ENCODING
 
static const std::string HEADER_LOCATION
 
static const std::string HEADER_AUTHORIZATION
 
static const std::string HEADER_REFERER
 
static const std::string HEADER_USER_AGENT
 
static const std::string HEADER_X_FORWARDED_FOR
 
static const std::string HEADER_CLIENT_IP
 
static const std::string CONTENT_TYPE_HTML
 
static const std::string CONTENT_TYPE_TEXT
 
static const std::string CONTENT_TYPE_XML
 
static const std::string CONTENT_TYPE_URLENCODED
 
static const std::string CONTENT_TYPE_MULTIPART_FORM_DATA
 
static const std::string REQUEST_METHOD_HEAD
 
static const std::string REQUEST_METHOD_GET
 
static const std::string REQUEST_METHOD_PUT
 
static const std::string REQUEST_METHOD_POST
 
static const std::string REQUEST_METHOD_DELETE
 
static const std::string RESPONSE_MESSAGE_OK
 
static const std::string RESPONSE_MESSAGE_CREATED
 
static const std::string RESPONSE_MESSAGE_ACCEPTED
 
static const std::string RESPONSE_MESSAGE_NO_CONTENT
 
static const std::string RESPONSE_MESSAGE_FOUND
 
static const std::string RESPONSE_MESSAGE_UNAUTHORIZED
 
static const std::string RESPONSE_MESSAGE_FORBIDDEN
 
static const std::string RESPONSE_MESSAGE_NOT_FOUND
 
static const std::string RESPONSE_MESSAGE_METHOD_NOT_ALLOWED
 
static const std::string RESPONSE_MESSAGE_NOT_MODIFIED
 
static const std::string RESPONSE_MESSAGE_BAD_REQUEST
 
static const std::string RESPONSE_MESSAGE_SERVER_ERROR
 
static const std::string RESPONSE_MESSAGE_NOT_IMPLEMENTED
 
static const std::string RESPONSE_MESSAGE_CONTINUE
 
static const unsigned int RESPONSE_CODE_OK = 200
 
static const unsigned int RESPONSE_CODE_CREATED = 201
 
static const unsigned int RESPONSE_CODE_ACCEPTED = 202
 
static const unsigned int RESPONSE_CODE_NO_CONTENT = 204
 
static const unsigned int RESPONSE_CODE_FOUND = 302
 
static const unsigned int RESPONSE_CODE_UNAUTHORIZED = 401
 
static const unsigned int RESPONSE_CODE_FORBIDDEN = 403
 
static const unsigned int RESPONSE_CODE_NOT_FOUND = 404
 
static const unsigned int RESPONSE_CODE_METHOD_NOT_ALLOWED = 405
 
static const unsigned int RESPONSE_CODE_NOT_MODIFIED = 304
 
static const unsigned int RESPONSE_CODE_BAD_REQUEST = 400
 
static const unsigned int RESPONSE_CODE_SERVER_ERROR = 500
 
static const unsigned int RESPONSE_CODE_NOT_IMPLEMENTED = 501
 
static const unsigned int RESPONSE_CODE_CONTINUE = 100
 

Detailed Description

message: base container for HTTP messages

Member Typedef Documentation

typedef std::vector<char> pion::http::message::chunk_cache_t

used to cache chunked data

typedef std::vector<boost::asio::const_buffer> pion::http::message::write_buffers_t

data type for I/O write buffers (these wrap existing data to be sent)

Member Enumeration Documentation

defines message data integrity status codes

Enumerator
STATUS_NONE 
STATUS_TRUNCATED 
STATUS_PARTIAL 
STATUS_OK 

Constructor & Destructor Documentation

pion::http::message::message ( void  )
inline

constructs a new HTTP message object

pion::http::message::message ( const message http_msg)
inline

copy constructor

virtual pion::http::message::~message ( )
inlinevirtual

virtual destructor

Member Function Documentation

void pion::http::message::add_cookie ( const std::string &  key,
const std::string &  value 
)
inline

adds a value for the cookie since cookie names are insensitive, key should use lowercase alpha chars

void pion::http::message::add_header ( const std::string &  key,
const std::string &  value 
)
inline
virtual void pion::http::message::append_cookie_headers ( void  )
inlineprotectedvirtual

appends HTTP headers for any cookies defined by the http::message

Reimplemented in pion::http::response, and pion::http::request.

void pion::http::message::append_headers ( write_buffers_t write_buffers)
inlineprotected

appends the message's HTTP headers to a vector of write buffers

Parameters
write_buffersthe buffers to append HTTP headers into
void pion::http::message::change_cookie ( const std::string &  key,
const std::string &  value 
)
inline

changes the value of a cookie since cookie names are insensitive, key should use lowercase alpha chars

void pion::http::message::change_header ( const std::string &  key,
const std::string &  value 
)
inline

changes the value for the HTTP header named key

Referenced by pion::http::response::set_last_modified().

template<typename DictionaryType >
static void pion::http::message::change_value ( DictionaryType &  dict,
const std::string &  key,
const std::string &  value 
)
inlinestaticprotected

Changes the value for a dictionary key. Adds the key if it does not already exist. If multiple values exist for the key, they will be removed and only the new value will remain.

Parameters
dictthe dictionary object to update
keythe key to change the value for
valuethe value to assign to the key

Referenced by pion::http::request::change_query().

bool pion::http::message::check_keep_alive ( void  ) const
inline

returns true if the HTTP connection may be kept alive

Referenced by receive().

virtual void pion::http::message::clear ( void  )
inlinevirtual

clears all message data

Reimplemented in pion::http::response, and pion::http::request.

Referenced by pion::http::request::clear(), pion::http::response::clear(), read(), and receive().

void pion::http::message::clear_content ( void  )
inline

clears payload content buffer

void pion::http::message::clear_first_line ( void  ) const
inlineprotected

erases the string containing the first line for the HTTP message (it will be updated the next time get_first_line() is called)

Referenced by pion::http::request::set_method(), pion::http::request::set_query_string(), pion::http::request::set_resource(), pion::http::response::set_status_code(), and pion::http::response::set_status_message().

void pion::http::message::concatenate_chunks ( void  )
char* pion::http::message::create_content_buffer ( void  )
inline

creates a payload content buffer of size m_content_length and returns a pointer to the new buffer (memory is managed by message class)

Referenced by concatenate_chunks(), pion::http::parser::finish(), pion::http::parser::finish_header_parsing(), pion::http::request::set_content(), and pion::http::request::use_query_params_for_post_content().

void pion::http::message::delete_cookie ( const std::string &  key)
inline

removes all values for a cookie since cookie names are insensitive, key should use lowercase alpha chars

void pion::http::message::delete_header ( const std::string &  key)
inline

removes all values for the HTTP header named key

template<typename DictionaryType >
static void pion::http::message::delete_value ( DictionaryType &  dict,
const std::string &  key 
)
inlinestaticprotected

Deletes all values for a key

Parameters
dictthe dictionary object to update
keythe key to delete

Referenced by pion::http::request::delete_query().

chunk_cache_t& pion::http::message::get_chunk_cache ( void  )
inline
bool pion::http::message::get_chunks_supported ( void  ) const
inline

returns true if chunked transfer encodings are supported

char* pion::http::message::get_content ( void  )
inline

returns a pointer to the payload content, or empty string if there is none

Referenced by pion::http::parser::consume_content(), pion::http::parser::finish(), pion::http::parser::parse_missing_data(), send(), and write().

const char* pion::http::message::get_content ( void  ) const
inline

returns a const pointer to the payload content, or empty string if there is none

std::size_t pion::http::message::get_content_buffer_size ( ) const
inline

returns size of allocated buffer

size_t pion::http::message::get_content_length ( void  ) const
inline

returns the length of the payload content (in bytes)

Referenced by pion::http::parser::finish(), pion::http::parser::finish_header_parsing(), send(), and write().

const std::string& pion::http::message::get_cookie ( const std::string &  key) const
inline

returns a value for the cookie if any are defined; otherwise, an empty string since cookie names are insensitive, key should use lowercase alpha chars

ihash_multimap& pion::http::message::get_cookies ( void  )
inline
const std::string& pion::http::message::get_first_line ( void  ) const
inline

returns a string containing the first line for the HTTP message

const std::string& pion::http::message::get_header ( const std::string &  key) const
inline

returns a value for the header if any are defined; otherwise, an empty string

Referenced by pion::http::parser::finish().

ihash_multimap& pion::http::message::get_headers ( void  )
inline

returns a reference to the HTTP headers

Referenced by pion::http::parser::update_message_with_header_data().

boost::asio::ip::address& pion::http::message::get_remote_ip ( void  )
inline

returns IP address of the remote endpoint

data_status_t pion::http::message::get_status ( ) const
inline

return the data receival status

template<typename DictionaryType >
static const std::string& pion::http::message::get_value ( const DictionaryType &  dict,
const std::string &  key 
)
inlinestaticprotected

Returns the first value in a dictionary if key is found; or an empty string if no values are found

Parameters
dictthe dictionary to search for key
keythe key to search for
Returns
value if found; empty string if not

Referenced by pion::http::request::get_query().

boost::uint16_t pion::http::message::get_version_major ( void  ) const
inline

returns the major HTTP version number

Referenced by pion::http::parser::parse_headers(), and pion::http::response::update_request_info().

boost::uint16_t pion::http::message::get_version_minor ( void  ) const
inline

returns the minor HTTP version number

Referenced by pion::http::parser::parse_headers(), and pion::http::response::update_request_info().

std::string pion::http::message::get_version_string ( void  ) const
inline

returns a string representation of the HTTP version (i.e. "HTTP/1.1")

Referenced by pion::http::request::update_first_line(), and pion::http::response::update_first_line().

bool pion::http::message::has_cookie ( const std::string &  key) const
inline

returns true if at least one value for the cookie is defined since cookie names are insensitive, key should use lowercase alpha chars

bool pion::http::message::has_data_after_missing_packets ( ) const
inline

true if more data seen after the missing packets

Referenced by pion::http::parser::compute_msg_status().

bool pion::http::message::has_header ( const std::string &  key) const
inline

returns true if at least one value for the header is defined

Referenced by pion::http::parser::finish_header_parsing().

bool pion::http::message::has_missing_packets ( ) const
inline

true if there were missing packets

Referenced by pion::http::parser::compute_msg_status(), and pion::http::parser::parse().

bool pion::http::message::is_chunked ( void  ) const
inline

returns true if the message content is chunked

Referenced by pion::http::parser::finish_header_parsing().

bool pion::http::message::is_content_buffer_allocated ( ) const
inline

returns true if buffer for content is allocated

virtual bool pion::http::message::is_content_length_implied ( void  ) const
pure virtual

should return true if the content length can be implied without headers

Implemented in pion::http::response, and pion::http::request.

Referenced by pion::http::parser::finish_header_parsing().

bool pion::http::message::is_valid ( void  ) const
inline

returns true if the message is valid

Referenced by pion::http::parser::finish().

message& pion::http::message::operator= ( const message http_msg)
inline

assignment operator

References m_first_line.

void pion::http::message::prepare_buffers_for_send ( write_buffers_t write_buffers,
const bool  keep_alive,
const bool  using_chunks 
)
inline

initializes a vector of write buffers with the HTTP message information

Parameters
write_buffersvector of write buffers to initialize
keep_alivetrue if the connection should be kept alive
using_chunkstrue if the payload content will be sent in chunks

Referenced by send(), and write().

void pion::http::message::prepare_headers_for_send ( const bool  keep_alive,
const bool  using_chunks 
)
inlineprotected

prepares HTTP headers for a send operation

Parameters
keep_alivetrue if the connection should be kept alive
using_chunkstrue if the payload content will be sent in chunks
std::size_t pion::http::message::read ( std::istream &  in,
boost::system::error_code &  ec,
parser http_parser 
)

reads a new message from a std::istream (blocks until finished)

Parameters
instd::istream to use
eccontains error code if the read fails
http_parserhttp parser object to use
Returns
std::size_t number of bytes read from the connection

References pion::http::parser::check_premature_eof(), clear(), pion::http::parser::get_total_bytes_read(), pion::http::parser::parse(), and pion::http::parser::set_read_buffer().

Referenced by read().

std::size_t pion::http::message::read ( std::istream &  in,
boost::system::error_code &  ec,
bool  headers_only = false,
std::size_t  max_content_length = static_cast<size_t>(-1) 
)

reads a new message from a std::istream (blocks until finished)

Parameters
instd::istream to use
eccontains error code if the read fails
headers_onlyif true then only HTTP headers are read
max_content_lengthmaximum number of content bytes received
Returns
std::size_t number of bytes read from the connection

References pion::http::parser::parse_headers_only(), read(), and pion::http::parser::set_max_content_length().

std::size_t pion::http::message::receive ( tcp::connection tcp_conn,
boost::system::error_code &  ec,
bool  headers_only = false,
std::size_t  max_content_length = static_cast<size_t>(-1) 
)

receives a new message from a TCP connection (blocks until finished)

Parameters
tcp_connTCP connection to use
eccontains error code if the receive fails
headers_onlyif true then only HTTP headers are received
max_content_lengthmaximum number of content bytes received
Returns
std::size_t number of bytes read from the connection

References pion::http::parser::parse_headers_only(), receive(), and pion::http::parser::set_max_content_length().

std::size_t pion::http::message::send ( tcp::connection tcp_conn,
boost::system::error_code &  ec,
bool  headers_only = false 
)

sends the message over a TCP connection (blocks until finished)

Parameters
tcp_connTCP connection to use
eccontains error code if the send fails
headers_onlyif true then only HTTP headers are sent
Returns
std::size_t number of bytes written to the connection

References get_content(), get_content_length(), pion::tcp::connection::get_keep_alive(), prepare_buffers_for_send(), and pion::tcp::connection::write().

void pion::http::message::set_chunks_supported ( bool  b)
inline

set to true if chunked transfer encodings are supported

Referenced by pion::http::response::update_request_info().

void pion::http::message::set_content ( const std::string &  content)
inline

resets payload content to match the value of a string

void pion::http::message::set_content_length ( size_t  n)
inline
void pion::http::message::set_content_type ( const std::string &  type)
inline

sets the content type for the message payload

Referenced by pion::http::request::use_query_params_for_post_content().

void pion::http::message::set_data_after_missing_packet ( bool  newVal)
inline
void pion::http::message::set_do_not_send_content_length ( void  )
inline

if called, the content-length will not be sent in the HTTP headers

void pion::http::message::set_is_valid ( bool  b = true)
inline

sets whether or not the message is valid

Referenced by pion::http::reader::consume_bytes(), and pion::http::parser::finish().

void pion::http::message::set_missing_packets ( bool  newVal)
inline

set to true when missing packets detected

Referenced by pion::http::parser::parse_missing_data().

void pion::http::message::set_remote_ip ( const boost::asio::ip::address &  ip)
inline

sets IP address of the remote endpoint

void pion::http::message::set_status ( data_status_t  newVal)
inline
void pion::http::message::set_version_major ( const boost::uint16_t  n)
inline

sets the major HTTP version number

Referenced by pion::http::parser::parse_headers(), and pion::http::response::update_request_info().

void pion::http::message::set_version_minor ( const boost::uint16_t  n)
inline

sets the minor HTTP version number

Referenced by pion::http::parser::parse_headers(), and pion::http::response::update_request_info().

void pion::http::message::update_content_length_using_header ( void  )
inline

sets the length of the payload content using the Content-Length header

References pion::test::trim().

Referenced by pion::http::parser::finish_header_parsing().

virtual void pion::http::message::update_first_line ( void  ) const
protectedpure virtual

updates the string containing the first line for the HTTP message

Implemented in pion::http::response, and pion::http::request.

void pion::http::message::update_transfer_encoding_using_header ( void  )
inline

sets the transfer coding using the Transfer-Encoding header

Referenced by pion::http::parser::finish_header_parsing().

std::size_t pion::http::message::write ( std::ostream &  out,
boost::system::error_code &  ec,
bool  headers_only = false 
)

writes the message to a std::ostream (blocks until finished)

Parameters
outstd::ostream to use
eccontains error code if the write fails
headers_onlyif true then only HTTP headers are written
Returns
std::size_t number of bytes written to the connection

References get_content(), get_content_length(), and prepare_buffers_for_send().

Member Data Documentation

std::string pion::http::message::m_first_line
mutableprotected

first line sent in an HTTP message (i.e. "GET / HTTP/1.1" for request, or "HTTP/1.1 200 OK" for response)

Referenced by operator=(), pion::http::request::update_first_line(), and pion::http::response::update_first_line().


The documentation for this class was generated from the following files: