request.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_HEADER__
#define __PION_HTTP_REQUEST_HEADER__
#include <boost/shared_ptr.hpp>
#include <pion/config.hpp>
#include <pion/http/message.hpp>
#include <pion/user.hpp>
namespace pion { // begin namespace pion
namespace http { // begin namespace http
class request
: public http::message
{
public:
request(const std::string& resource)
: m_method(REQUEST_METHOD_GET), m_resource(resource) {}
request(void) : m_method(REQUEST_METHOD_GET) {}
virtual ~request() {}
virtual void clear(void) {
http::message::clear();
m_method.erase();
m_resource.erase();
m_original_resource.erase();
m_query_string.erase();
m_query_params.clear();
m_user_record.reset();
}
virtual bool is_content_length_implied(void) const { return false; }
inline const std::string& get_method(void) const { return m_method; }
inline const std::string& get_resource(void) const { return m_resource; }
inline const std::string& get_original_resource(void) const { return m_original_resource; }
inline const std::string& get_query_string(void) const { return m_query_string; }
inline const std::string& get_query(const std::string& key) const {
return get_value(m_query_params, key);
}
inline ihash_multimap& get_queries(void) {
return m_query_params;
}
inline bool has_query(const std::string& key) const {
return(m_query_params.find(key) != m_query_params.end());
}
inline void set_method(const std::string& str) {
m_method = str;
clear_first_line();
}
inline void set_resource(const std::string& str) {
m_resource = m_original_resource = str;
clear_first_line();
}
inline void change_resource(const std::string& str) { m_resource = str; }
inline void set_query_string(const std::string& str) {
m_query_string = str;
clear_first_line();
}
inline void add_query(const std::string& key, const std::string& value) {
m_query_params.insert(std::make_pair(key, value));
}
inline void change_query(const std::string& key, const std::string& value) {
change_value(m_query_params, key, value);
}
inline void delete_query(const std::string& key) {
delete_value(m_query_params, key);
}
inline void use_query_params_for_query_string(void) {
set_query_string(make_query_string(m_query_params));
}
inline void use_query_params_for_post_content(void) {
std::string post_content(make_query_string(m_query_params));
set_content_length(post_content.size());
char *ptr = create_content_buffer(); // null-terminates buffer
if (! post_content.empty())
memcpy(ptr, post_content.c_str(), post_content.size());
set_method(REQUEST_METHOD_POST);
set_content_type(CONTENT_TYPE_URLENCODED);
}
inline void set_content(const std::string &value) {
set_content_length(value.size());
char *ptr = create_content_buffer();
if (! value.empty())
memcpy(ptr, value.c_str(), value.size());
}
inline void set_content(const char* value, size_t size) {
if ( NULL == value || 0 == size )
return;
set_content_length(size);
char *ptr = create_content_buffer();
memcpy(ptr, value, size);
}
inline void set_user(user_ptr user) { m_user_record = user; }
inline user_ptr get_user() const { return m_user_record; }
protected:
virtual void update_first_line(void) const {
// start out with the request method
m_first_line = m_method;
m_first_line += ' ';
// append the resource requested
m_first_line += m_resource;
if (! m_query_string.empty()) {
// append query string if not empty
m_first_line += '?';
m_first_line += m_query_string;
}
m_first_line += ' ';
// append HTTP version
m_first_line += get_version_string();
}
virtual void append_cookie_headers(void) {
for (ihash_multimap::const_iterator i = get_cookies().begin(); i != get_cookies().end(); ++i) {
std::string cookie_header;
cookie_header = i->first;
cookie_header += COOKIE_NAME_VALUE_DELIMITER;
cookie_header += i->second;
add_header(HEADER_COOKIE, cookie_header);
}
}
private:
std::string m_method;
std::string m_resource;
std::string m_original_resource;
std::string m_query_string;
ihash_multimap m_query_params;
user_ptr m_user_record;
};
typedef boost::shared_ptr<request> request_ptr;
} // end namespace http
} // end namespace pion
#endif
pion::http::message::clear_first_line
void clear_first_line(void) const
Definition: message.hpp:668
pion::http::request::has_query
bool has_query(const std::string &key) const
returns true if at least one value for the query key is defined
Definition: request.hpp:82
pion::http::request::delete_query
void delete_query(const std::string &key)
removes all values for a query key
Definition: request.hpp:118
pion::http::request::~request
virtual ~request()
virtual destructor
Definition: request.hpp:43
pion::http::request::set_content
void set_content(const std::string &value)
add content (for POST) from string
Definition: request.hpp:139
pion::http::request::get_resource
const std::string & get_resource(void) const
returns the resource uri-stem to be delivered (possibly the result of a redirect) ...
Definition: request.hpp:63
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::request::clear
virtual void clear(void)
clears all request data
Definition: request.hpp:46
pion::http::request
Definition: request.hpp:26
pion::http::request::change_resource
void change_resource(const std::string &str)
changes the resource or uri-stem to be delivered (called as the result of a redirect) ...
Definition: request.hpp:99
pion::http::request::get_user
user_ptr get_user() const
get the user record for HTTP request after authentication
Definition: request.hpp:160
pion::http::message::get_value
static const std::string & get_value(const DictionaryType &dict, const std::string &key)
Definition: message.hpp:609
pion::http::request::get_original_resource
const std::string & get_original_resource(void) const
returns the resource uri-stem originally requested
Definition: request.hpp:66
pion::http::message::set_content_length
void set_content_length(size_t n)
sets the length of the payload content (in bytes)
Definition: message.hpp:302
pion::http::message::set_content_type
void set_content_type(const std::string &type)
sets the content type for the message payload
Definition: message.hpp:358
pion::http::message::change_value
static void change_value(DictionaryType &dict, const std::string &key, const std::string &value)
Definition: message.hpp:626
pion::user
Definition: user.hpp:39
pion::http::request::request
request(void)
constructs a new request object (default constructor)
Definition: request.hpp:40
pion::http::request::set_method
void set_method(const std::string &str)
sets the HTTP request method (i.e. GET, POST, PUT)
Definition: request.hpp:87
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::request::set_user
void set_user(user_ptr user)
sets the user record for HTTP request after authentication
Definition: request.hpp:157
pion::http::message
Definition: message.hpp:55
pion::http::request::set_content
void set_content(const char *value, size_t size)
Definition: request.hpp:148
pion::http::message::m_first_line
std::string m_first_line
Definition: message.hpp:678
pion::http::request::get_query
const std::string & get_query(const std::string &key) const
returns a value for the query key if any are defined; otherwise, an empty string
Definition: request.hpp:72
pion::http::request::change_query
void change_query(const std::string &key, const std::string &value)
changes the value of a query key
Definition: request.hpp:113
pion::http::request::add_query
void add_query(const std::string &key, const std::string &value)
adds a value for the query key
Definition: request.hpp:108
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::request::update_first_line
virtual void update_first_line(void) const
updates the string containing the first line for the HTTP message
Definition: request.hpp:166
pion::http::request::append_cookie_headers
virtual void append_cookie_headers(void)
appends HTTP headers for any cookies defined by the http::message
Definition: request.hpp:183
pion
Definition: admin_rights.hpp:19
pion::http::request::get_query_string
const std::string & get_query_string(void) const
returns the uri-query or query string requested
Definition: request.hpp:69
pion::http::message::create_content_buffer
char * create_content_buffer(void)
Definition: message.hpp:338
pion::http::message::delete_value
static void delete_value(DictionaryType &dict, const std::string &key)
Definition: message.hpp:657
pion::http::request::use_query_params_for_query_string
void use_query_params_for_query_string(void)
use the query parameters to build a query string for the request
Definition: request.hpp:123
pion::http::request::set_query_string
void set_query_string(const std::string &str)
sets the uri-query or query string requested
Definition: request.hpp:102
pion::http::request::is_content_length_implied
virtual bool is_content_length_implied(void) const
the content length of the message can never be implied for requests
Definition: request.hpp:57
pion::http::request::get_queries
ihash_multimap & get_queries(void)
returns the query parameters
Definition: request.hpp:77
pion::http::request::request
request(const std::string &resource)
Definition: request.hpp:36
pion::http::message::get_cookies
ihash_multimap & get_cookies(void)
returns the cookie parameters
Definition: message.hpp:234
pion::http::request::use_query_params_for_post_content
void use_query_params_for_post_content(void)
use the query parameters to build POST content for the request
Definition: request.hpp:128
pion::http::types::make_query_string
static std::string make_query_string(const ihash_multimap &query_params)
builds an HTTP query string from a collection of query parameters
Definition: http_types.cpp:114
pion::http::request::set_resource
void set_resource(const std::string &str)
sets the resource or uri-stem originally requested
Definition: request.hpp:93