Sirikata
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends
Sirikata::Transfer::HttpManager Class Reference

#include <HttpManager.hpp>

Inheritance diagram for Sirikata::Transfer::HttpManager:
Collaboration diagram for Sirikata::Transfer::HttpManager:

List of all members.

Classes

struct  CaseInsensitiveStringLess
class  HttpRequest
class  HttpResponse
struct  MultipartData
 Represents one field in a multipart/form-data. More...

Public Types

enum  ERR_TYPE { SUCCESS, REQUEST_PARSING_FAILED, RESPONSE_PARSING_FAILED, BOOST_ERROR }
enum  HTTP_METHOD { HEAD, GET, POST }
typedef std::map< std::string,
std::string > 
StringDictionary
typedef std::map< std::string,
std::string,
CaseInsensitiveStringLess
CaseInsensitiveStringDictionary
typedef
CaseInsensitiveStringDictionary 
Headers
typedef StringDictionary QueryParameters
typedef std::vector
< MultipartData
MultipartDataList
typedef std::tr1::shared_ptr
< HttpResponse
HttpResponsePtr
typedef std::tr1::function
< void(std::tr1::shared_ptr
< HttpResponse > response,
ERR_TYPE error, const
boost::system::error_code
&boost_error)> 
HttpCallback

Public Member Functions

void makeRequest (Sirikata::Network::Address addr, HTTP_METHOD method, std::string req, bool allow_redirects, HttpCallback cb)
 Makes an HTTP request and calls cb when finished.
void makeRequest (Sirikata::Network::Address addr, HTTP_METHOD method, const String &path, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), const String &body="", bool allow_redirects=true)
 Formats and makes an HTTP request and calls cb when finished.
void head (Sirikata::Network::Address addr, const String &path, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), bool allow_redirects=true)
void get (Sirikata::Network::Address addr, const String &path, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), bool allow_redirects=true)
void post (Sirikata::Network::Address addr, const String &path, const String &content_type, const String &body, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), bool allow_redirects=true)
 Perform an HTTP POST using the specified content type and message body.
void postURLEncoded (Sirikata::Network::Address addr, const String &path, const StringDictionary &body, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), bool allow_redirects=true)
 Perform a HTTP POST whose body is x-www-form-urlencoded parameters.
void postMultipartForm (Sirikata::Network::Address addr, const String &path, const MultipartDataList &data, HttpCallback cb, const Headers &headers=Headers(), const QueryParameters &query_params=QueryParameters(), bool allow_redirects=true)
 Perform a HTTP POST whose body is multipart/form-data encoded body.
void postCallback (IOCallback cb, const char *tag)
void postCallback (const Duration &waitFor, IOCallback cb, const char *tag)

Static Public Member Functions

static HttpManagergetSingleton ()
static void destroy ()
static String methodAsString (HTTP_METHOD m)
static String formatURLEncodedDictionary (const StringDictionary &query_params)
static String formatPath (const String &path, const QueryParameters &query_params)
static String formatURL (const String &host, const String &service, const String &path, const QueryParameters &query_params)

Protected Types

enum  LAST_HEADER_CB { NONE, FIELD, VALUE }

Protected Member Functions

 HttpManager ()
 ~HttpManager ()
friend std::auto_ptr ()

Static Protected Member Functions

static void formatURLEncodedDictionary (std::ostream &os, const StringDictionary &query_params)
static void formatPath (std::ostream &os, const String &path, const QueryParameters &query_params)

Private Types

enum  HTTP_PARSER_FLAGS {
  F_CHUNKED = 1 << 0, F_CONNECTION_KEEP_ALIVE = 1 << 1, F_CONNECTION_CLOSE = 1 << 2, F_TRAILING = 1 << 3,
  F_UPGRADE = 1 << 4, F_SKIPBODY = 1 << 5
}
typedef
Sirikata::Network::IOServicePool 
IOServicePool
typedef
Sirikata::Network::TCPResolver 
TCPResolver
typedef
Sirikata::Network::TCPSocket 
TCPSocket
typedef Sirikata::Network::IOWork IOWork
typedef
Sirikata::Network::IOCallback 
IOCallback
typedef
boost::asio::ip::tcp::endpoint 
TCPEndPoint
typedef std::list
< std::tr1::shared_ptr
< HttpRequest > > 
RequestQueueType
typedef std::map
< Sirikata::Network::Address,
uint32 > 
NumConnsType
typedef std::map
< Sirikata::Network::Address,
std::queue
< std::tr1::shared_ptr
< TCPSocket > > > 
RecycleBinType

Private Member Functions

void processQueue ()
void add_req (std::tr1::shared_ptr< HttpRequest > req)
void decrement_connection (const Sirikata::Network::Address &addr)
void write_request (std::tr1::shared_ptr< TCPSocket > socket, std::tr1::shared_ptr< HttpRequest > req)
void handle_resolve (std::tr1::shared_ptr< HttpRequest > req, const boost::system::error_code &err, TCPResolver::iterator endpoint_iterator)
void handle_connect (std::tr1::shared_ptr< TCPSocket > socket, std::tr1::shared_ptr< HttpRequest > req, const boost::system::error_code &err, TCPResolver::iterator endpoint_iterator)
void handle_write_request (std::tr1::shared_ptr< TCPSocket > socket, std::tr1::shared_ptr< HttpRequest > req, const boost::system::error_code &err, std::tr1::shared_ptr< boost::asio::streambuf > request_stream)
void handle_read (std::tr1::shared_ptr< TCPSocket > socket, std::tr1::shared_ptr< HttpRequest > req, std::tr1::shared_ptr< std::vector< unsigned char > > vecbuf, std::tr1::shared_ptr< HttpResponse > respPtr, const boost::system::error_code &err, std::size_t bytes_transferred)

Static Private Member Functions

static int on_header_field (http_parser *_, const char *at, size_t len)
static int on_header_value (http_parser *_, const char *at, size_t len)
static int on_headers_complete (http_parser *_)
static int on_body (http_parser *_, const char *at, size_t len)
static int on_message_complete (http_parser *_)
static int on_request_header_field (http_parser *_, const char *at, size_t len)
static int on_request_header_value (http_parser *_, const char *at, size_t len)
static int on_request_headers_complete (http_parser *_)
static void print_flags (std::tr1::shared_ptr< HttpResponse > resp)

Private Attributes

RequestQueueType mRequestQueue
boost::mutex mRequestQueueLock
uint32 mNumTotalConnections
NumConnsType mNumConnsPerAddr
boost::mutex mNumConnsLock
RecycleBinType mRecycleBin
boost::mutex mRecycleBinLock
IOServicePoolmServicePool
TCPResolvermResolver
http_parser_settings EMPTY_PARSER_SETTINGS

Static Private Attributes

static const uint32 MAX_CONNECTIONS_PER_ENDPOINT = 8
static const uint32 MAX_TOTAL_CONNECTIONS = 40
static const uint32 SOCKET_BUFFER_SIZE = 10240

Friends

class AutoSingleton< HttpManager >
void std::auto_ptr (HttpManager *)

Member Typedef Documentation

typedef std::tr1::function<void( std::tr1::shared_ptr<HttpResponse> response, ERR_TYPE error, const boost::system::error_code& boost_error )> Sirikata::Transfer::HttpManager::HttpCallback
typedef std::map<Sirikata::Network::Address, std::queue<std::tr1::shared_ptr<TCPSocket> > > Sirikata::Transfer::HttpManager::RecycleBinType [private]
typedef std::list<std::tr1::shared_ptr<HttpRequest> > Sirikata::Transfer::HttpManager::RequestQueueType [private]
typedef std::map<std::string, std::string> Sirikata::Transfer::HttpManager::StringDictionary
typedef boost::asio::ip::tcp::endpoint Sirikata::Transfer::HttpManager::TCPEndPoint [private]

Member Enumeration Documentation

Enumerator:
SUCCESS 
REQUEST_PARSING_FAILED 
RESPONSE_PARSING_FAILED 
BOOST_ERROR 
Enumerator:
HEAD 
GET 
POST 
Enumerator:
F_CHUNKED 
F_CONNECTION_KEEP_ALIVE 
F_CONNECTION_CLOSE 
F_TRAILING 
F_UPGRADE 
F_SKIPBODY 
Enumerator:
NONE 
FIELD 
VALUE 

Constructor & Destructor Documentation

Sirikata::Transfer::HttpManager::HttpManager ( ) [protected]
Sirikata::Transfer::HttpManager::~HttpManager ( ) [protected]

Member Function Documentation

void Sirikata::Transfer::HttpManager::add_req ( std::tr1::shared_ptr< HttpRequest req) [private]
void Sirikata::Transfer::HttpManager::decrement_connection ( const Sirikata::Network::Address addr) [private]
void Sirikata::Transfer::HttpManager::destroy ( ) [static]
void Sirikata::Transfer::HttpManager::formatPath ( std::ostream &  os,
const String &  path,
const QueryParameters query_params 
) [static, protected]
String Sirikata::Transfer::HttpManager::formatPath ( const String &  path,
const QueryParameters query_params 
) [static]

Referenced by formatURL(), and makeRequest().

String Sirikata::Transfer::HttpManager::formatURL ( const String &  host,
const String &  service,
const String &  path,
const QueryParameters query_params 
) [static]
String Sirikata::Transfer::HttpManager::formatURLEncodedDictionary ( const StringDictionary query_params) [static]
void Sirikata::Transfer::HttpManager::formatURLEncodedDictionary ( std::ostream &  os,
const StringDictionary query_params 
) [static, protected]
void Sirikata::Transfer::HttpManager::get ( Sirikata::Network::Address  addr,
const String &  path,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
bool  allow_redirects = true 
)
HttpManager & Sirikata::Transfer::HttpManager::getSingleton ( ) [static]
void Sirikata::Transfer::HttpManager::handle_connect ( std::tr1::shared_ptr< TCPSocket socket,
std::tr1::shared_ptr< HttpRequest req,
const boost::system::error_code &  err,
TCPResolver::iterator  endpoint_iterator 
) [private]
void Sirikata::Transfer::HttpManager::handle_read ( std::tr1::shared_ptr< TCPSocket socket,
std::tr1::shared_ptr< HttpRequest req,
std::tr1::shared_ptr< std::vector< unsigned char > >  vecbuf,
std::tr1::shared_ptr< HttpResponse respPtr,
const boost::system::error_code &  err,
std::size_t  bytes_transferred 
) [private]
void Sirikata::Transfer::HttpManager::handle_resolve ( std::tr1::shared_ptr< HttpRequest req,
const boost::system::error_code &  err,
TCPResolver::iterator  endpoint_iterator 
) [private]
void Sirikata::Transfer::HttpManager::handle_write_request ( std::tr1::shared_ptr< TCPSocket socket,
std::tr1::shared_ptr< HttpRequest req,
const boost::system::error_code &  err,
std::tr1::shared_ptr< boost::asio::streambuf >  request_stream 
) [private]
void Sirikata::Transfer::HttpManager::head ( Sirikata::Network::Address  addr,
const String &  path,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
bool  allow_redirects = true 
)

References HEAD, and makeRequest().

void Sirikata::Transfer::HttpManager::makeRequest ( Sirikata::Network::Address  addr,
HTTP_METHOD  method,
std::string  req,
bool  allow_redirects,
HttpCallback  cb 
)

Makes an HTTP request and calls cb when finished.

This is the lowest level version exposed publicly, taking a raw HTTP request, which you should ensure is properly formatted. Usually you should use the convenience wrappers that format the request for you.

References add_req(), EMPTY_PARSER_SETTINGS, on_request_header_field(), on_request_header_value(), on_request_headers_complete(), postCallback(), processQueue(), REQUEST_PARSING_FAILED, SILOG, and Sirikata::Logging::warning.

Referenced by get(), handle_read(), head(), Sirikata::JS::EmersonHttpManager::makeRequest(), makeRequest(), and post().

void Sirikata::Transfer::HttpManager::makeRequest ( Sirikata::Network::Address  addr,
HTTP_METHOD  method,
const String &  path,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
const String &  body = "",
bool  allow_redirects = true 
)

Formats and makes an HTTP request and calls cb when finished.

This version is a utility for the more specific request types (i.e. head() and get()).

Parameters:
addrthe address of the server
methodthe HTTP request method, i.e. HEAD, GET, or POST
paththe path of the resource to access
cbcallback to invoke upon completion
headersdictionary of headers to add to the request
query_paramsdictionary of unencoded query parameters to add to the url
bodyif non-empty, the encoded HTTP request body, i.e. the data provided after all the headers. If you provide this, you should probably include headers to specify it's format
allow_redirectsif true, redirects will be followed, triggering a new requests

References formatPath(), makeRequest(), and methodAsString().

String Sirikata::Transfer::HttpManager::methodAsString ( HTTP_METHOD  m) [static]

References GET, HEAD, and POST.

Referenced by handle_read(), and makeRequest().

int Sirikata::Transfer::HttpManager::on_body ( http_parser *  _,
const char *  at,
size_t  len 
) [static, private]
int Sirikata::Transfer::HttpManager::on_header_field ( http_parser *  _,
const char *  at,
size_t  len 
) [static, private]
int Sirikata::Transfer::HttpManager::on_header_value ( http_parser *  _,
const char *  at,
size_t  len 
) [static, private]
int Sirikata::Transfer::HttpManager::on_headers_complete ( http_parser *  _) [static, private]
int Sirikata::Transfer::HttpManager::on_message_complete ( http_parser *  _) [static, private]
int Sirikata::Transfer::HttpManager::on_request_header_field ( http_parser *  _,
const char *  at,
size_t  len 
) [static, private]
int Sirikata::Transfer::HttpManager::on_request_header_value ( http_parser *  _,
const char *  at,
size_t  len 
) [static, private]
int Sirikata::Transfer::HttpManager::on_request_headers_complete ( http_parser *  _) [static, private]
void Sirikata::Transfer::HttpManager::post ( Sirikata::Network::Address  addr,
const String &  path,
const String &  content_type,
const String &  body,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
bool  allow_redirects = true 
)

Perform an HTTP POST using the specified content type and message body.

This can be used if you want to use an unusual encoding or as a utility for other, more specific post methods.

References makeRequest(), and POST.

Referenced by Sirikata::Transfer::OAuthHttpManager::post(), postMultipartForm(), and postURLEncoded().

void Sirikata::Transfer::HttpManager::postCallback ( const Duration waitFor,
IOCallback  cb,
const char *  tag 
)
void Sirikata::Transfer::HttpManager::postCallback ( IOCallback  cb,
const char *  tag 
)
void Sirikata::Transfer::HttpManager::postMultipartForm ( Sirikata::Network::Address  addr,
const String &  path,
const MultipartDataList data,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
bool  allow_redirects = true 
)
void Sirikata::Transfer::HttpManager::postURLEncoded ( Sirikata::Network::Address  addr,
const String &  path,
const StringDictionary body,
HttpCallback  cb,
const Headers headers = Headers(),
const QueryParameters query_params = QueryParameters(),
bool  allow_redirects = true 
)

Perform a HTTP POST whose body is x-www-form-urlencoded parameters.

References formatURLEncodedDictionary(), and post().

void Sirikata::Transfer::HttpManager::print_flags ( std::tr1::shared_ptr< HttpResponse resp) [static, private]
void Sirikata::Transfer::HttpManager::processQueue ( ) [private]
Sirikata::Transfer::HttpManager::std::auto_ptr ( ) [protected]
void Sirikata::Transfer::HttpManager::write_request ( std::tr1::shared_ptr< TCPSocket socket,
std::tr1::shared_ptr< HttpRequest req 
) [private]

Friends And Related Function Documentation

friend class AutoSingleton< HttpManager > [friend]
void std::auto_ptr ( HttpManager ) [friend]

Member Data Documentation

Referenced by processQueue().

const uint32 Sirikata::Transfer::HttpManager::MAX_TOTAL_CONNECTIONS = 40 [static, private]

Referenced by processQueue().

Referenced by handle_read(), and processQueue().

Referenced by add_req(), processQueue(), and ~HttpManager().

Referenced by add_req(), and processQueue().

const uint32 Sirikata::Transfer::HttpManager::SOCKET_BUFFER_SIZE = 10240 [static, private]

Referenced by handle_write_request().


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