Skip to content

Instantly share code, notes, and snippets.

@padenot
Created November 27, 2010 10:56
Show Gist options
  • Save padenot/717795 to your computer and use it in GitHub Desktop.
Save padenot/717795 to your computer and use it in GitHub Desktop.
HttpRequest::HttpRequest (const std::string& url)
:attributeCount_(0),
url_(url),
hostname_(url.substr(0, url.find_first_of('/'))),
path_(url.substr(url.find_first_of('/'))),
resolver_(io_service_),
socket_(io_service_)
{
//check if a port is in the hostname.
size_t index = hostname_.find_first_of(':');
if(index != string::npos)
{
// The ':' is discarded
port_ = hostname_.substr(index + 1);
hostname_ = hostname_.substr(0, index);
}
else
{
port_ = RESOLVER_QUERY_TYPE_HTTP;
}
}
HttpRequest::~HttpRequest ( )
{ }
void HttpRequest::SetAttribute(std::pair<std::string, std::string > attr)
{
setGetAttribute(attr);
}
void HttpRequest::Perform(boost::function<void(std::string, void*)> callbackFunc)
{
callbackFunc_ = callbackFunc;
ostream request(&request_);
request << getRequest();
IOServiceSingleton::CreateThread();
// Start a new async task on a new thread.
IOServiceSingleton::GetInstance()->post(boost::bind(&HttpRequest::async_perform, this));
}
void HttpRequest::async_perform ( )
{
/**
* Build a query.
*/
tcp::resolver::query query(hostname_, port_);
resolver_.async_resolve(query, boost::bind(&HttpRequest::handleResolve,
this, boost::asio::placeholders::error,
boost::asio::placeholders::iterator));
io_service_.reset();
io_service_.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment