response.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_RESPONSE_HEADER__
#define __PION_HTTP_RESPONSE_HEADER__
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <pion/config.hpp>
#include <pion/http/message.hpp>
#include <pion/http/request.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class response
: public http::message
{
public:
response(const http::request& http_request_ptr)
: m_status_code(RESPONSE_CODE_OK),
m_status_message(RESPONSE_MESSAGE_OK)
{
update_request_info(http_request_ptr);
}
response(const std::string& request_method)
: m_status_code(RESPONSE_CODE_OK), m_status_message(RESPONSE_MESSAGE_OK),
m_request_method(request_method)
{}
response(const response& http_response)
: message(http_response),
m_status_code(http_response.m_status_code),
m_status_message(http_response.m_status_message),
m_request_method(http_response.m_request_method)
{}
response(void)
: m_status_code(RESPONSE_CODE_OK),
m_status_message(RESPONSE_MESSAGE_OK)
{}
virtual ~response() {}
virtual void clear(void) {
http::message::clear();
m_status_code = RESPONSE_CODE_OK;
m_status_message = RESPONSE_MESSAGE_OK;
m_request_method.clear();
}
virtual bool is_content_length_implied(void) const {
return (m_request_method == REQUEST_METHOD_HEAD // HEAD responses have no content
|| (m_status_code >= 100 && m_status_code <= 199) // 1xx responses have no content
|| m_status_code == 204 || m_status_code == 205 // no content & reset content responses
|| m_status_code == 304 // not modified responses have no content
);
}
inline void update_request_info(const http::request& http_request) {
m_request_method = http_request.get_method();
if (http_request.get_version_major() == 1 && http_request.get_version_minor() >= 1) {
set_chunks_supported(true);
} else if (http_request.get_version_major() == 0) {
// the request is likely HTTP 0.9 "simple-request", so expect the response to contain no header and no version info
set_status_code(0U);
set_status_message("");
set_version_major(0);
set_version_minor(0);
}
}
inline void set_status_code(unsigned int n) {
m_status_code = n;
clear_first_line();
}
inline void set_status_message(const std::string& msg) {
m_status_message = msg;
clear_first_line();
}
inline unsigned int get_status_code(void) const { return m_status_code; }
inline const std::string& get_status_message(void) const { return m_status_message; }
inline void set_cookie(const std::string& name, const std::string& value) {
std::string set_cookie_header(make_set_cookie_header(name, value, "/"));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void set_cookie(const std::string& name, const std::string& value,
const std::string& path)
{
std::string set_cookie_header(make_set_cookie_header(name, value, path));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void set_cookie(const std::string& name, const std::string& value,
const std::string& path, const unsigned long max_age)
{
std::string set_cookie_header(make_set_cookie_header(name, value, path, true, max_age));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void set_cookie(const std::string& name, const std::string& value,
const unsigned long max_age)
{
std::string set_cookie_header(make_set_cookie_header(name, value, "/", true, max_age));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void delete_cookie(const std::string& name) {
std::string set_cookie_header(make_set_cookie_header(name, "", "/", true, 0));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void delete_cookie(const std::string& name, const std::string& path) {
std::string set_cookie_header(make_set_cookie_header(name, "", path, true, 0));
add_header(HEADER_SET_COOKIE, set_cookie_header);
}
inline void set_last_modified(const unsigned long t) {
change_header(HEADER_LAST_MODIFIED, get_date_string(t));
}
protected:
virtual void update_first_line(void) const {
// start out with the HTTP version
m_first_line = get_version_string();
m_first_line += ' ';
// append the response status code
m_first_line += boost::lexical_cast<std::string>(m_status_code);
m_first_line += ' ';
// append the response status message
m_first_line += m_status_message;
}
virtual void append_cookie_headers(void) {
for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
set_cookie(i->first, i->second);
}
}
private:
unsigned int m_status_code;
std::string m_status_message;
std::string m_request_method;
};
typedef boost::shared_ptr<response> response_ptr;
} // end namespace http
} // end namespace pion
#endif
pion::http::response::set_cookie
void set_cookie(const std::string &name, const std::string &value, const std::string &path)
Definition: response.hpp:147
pion::http::message::clear_first_line
void clear_first_line(void) const
Definition: message.hpp:668
pion::http::response::response
response(void)
Definition: response.hpp:64
pion::http::response::clear
virtual void clear(void)
clears all response data
Definition: response.hpp:73
pion::http::response::delete_cookie
void delete_cookie(const std::string &name, const std::string &path)
deletes cookie called name by adding a Set-Cookie header (cookie has a path)
Definition: response.hpp:190
pion::http::response::set_cookie
void set_cookie(const std::string &name, const std::string &value, const std::string &path, const unsigned long max_age)
Definition: response.hpp:162
pion::http::message::add_header
void add_header(const std::string &key, const std::string &value)
adds a value for the HTTP header named key
Definition: message.hpp:363
pion::http::message::clear
virtual void clear(void)
clears all message data
Definition: message.hpp:146
pion::http::response::set_status_code
void set_status_code(unsigned int n)
sets the HTTP response status code
Definition: response.hpp:109
pion::http::response
Definition: response.hpp:27
pion::http::request
Definition: request.hpp:26
pion::http::message::set_version_minor
void set_version_minor(const boost::uint16_t n)
sets the minor HTTP version number
Definition: message.hpp:296
pion::http::message::set_version_major
void set_version_major(const boost::uint16_t n)
sets the major HTTP version number
Definition: message.hpp:290
pion::http::message::change_header
void change_header(const std::string &key, const std::string &value)
changes the value for the HTTP header named key
Definition: message.hpp:368
pion::http::message::set_chunks_supported
void set_chunks_supported(bool b)
set to true if chunked transfer encodings are supported
Definition: message.hpp:284
pion::http::message::get_version_minor
boost::uint16_t get_version_minor(void) const
returns the minor HTTP version number
Definition: message.hpp:180
pion::http::types::get_date_string
static std::string get_date_string(const time_t t)
converts time_t format into an HTTP-date string
Definition: http_types.cpp:98
pion::http::response::get_status_code
unsigned int get_status_code(void) const
returns the HTTP response status code
Definition: response.hpp:121
pion::http::message::get_version_string
std::string get_version_string(void) const
returns a string representation of the HTTP version (i.e. "HTTP/1.1")
Definition: message.hpp:183
pion::http::message
Definition: message.hpp:55
pion::http::message::m_first_line
std::string m_first_line
Definition: message.hpp:678
pion::http::response::~response
virtual ~response()
virtual destructor
Definition: response.hpp:70
pion::http::response::is_content_length_implied
virtual bool is_content_length_implied(void) const
the content length may be implied for certain types of responses
Definition: response.hpp:81
pion::http::response::delete_cookie
void delete_cookie(const std::string &name)
deletes cookie called name by adding a Set-Cookie header (cookie has no path)
Definition: response.hpp:184
pion::http::response::response
response(const std::string &request_method)
Definition: response.hpp:49
pion::http::response::set_status_message
void set_status_message(const std::string &msg)
sets the HTTP response status message
Definition: response.hpp:115
pion::http::response::response
response(const http::request &http_request_ptr)
Definition: response.hpp:37
pion::http::response::get_status_message
const std::string & get_status_message(void) const
returns the HTTP response status message
Definition: response.hpp:124
pion::http::request::get_method
const std::string & get_method(void) const
returns the request method (i.e. GET, POST, PUT)
Definition: request.hpp:60
pion::http::response::set_cookie
void set_cookie(const std::string &name, const std::string &value, const unsigned long max_age)
Definition: response.hpp:176
pion
Definition: admin_rights.hpp:19
pion::http::response::append_cookie_headers
virtual void append_cookie_headers(void)
appends HTTP headers for any cookies defined by the http::message
Definition: response.hpp:216
pion::http::response::response
response(const response &http_response)
copy constructor
Definition: response.hpp:55
pion::http::message::get_version_major
boost::uint16_t get_version_major(void) const
returns the major HTTP version number
Definition: message.hpp:177
pion::http::response::update_request_info
void update_request_info(const http::request &http_request)
Definition: response.hpp:95
pion::http::response::update_first_line
virtual void update_first_line(void) const
updates the string containing the first line for the HTTP message
Definition: response.hpp:204
pion::http::message::get_cookies
ihash_multimap & get_cookies(void)
returns the cookie parameters
Definition: message.hpp:234
pion::http::types::make_set_cookie_header
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)
Definition: http_types.cpp:127
pion::http::response::set_last_modified
void set_last_modified(const unsigned long t)
sets the time that the response was last modified (Last-Modified)
Definition: response.hpp:196
pion::http::response::set_cookie
void set_cookie(const std::string &name, const std::string &value)
Definition: response.hpp:134