Skip to content

Instantly share code, notes, and snippets.

@tommetge
Created June 13, 2012 17:04
Show Gist options
  • Save tommetge/2925283 to your computer and use it in GitHub Desktop.
Save tommetge/2925283 to your computer and use it in GitHub Desktop.
mordor download to stream
static bool downloadToStream(const char * inUrl, Stream &dst, Mordor::IOManager * iomanager)
{
boost::scoped_ptr<WorkerPool> workInThisThread;
if (Scheduler::getThis() == NULL) {
workInThisThread.reset(new WorkerPool(1, true));
}
HTTP::RequestBrokerOptions options;
options.ioManager = iomanager;
HTTP::RequestBroker::ptr requestBroker = HTTP::createRequestBroker(options).first;
try {
URI uri(inUrl);
HTTP::Request requestHeaders;
requestHeaders.requestLine.uri = uri;
requestHeaders.request.host = uri.authority.host();
HTTP::ClientRequest::ptr request = requestBroker->request(requestHeaders);
if (request->response().status.status != HTTP::OK) {
// possibly NOT_FOUND or other HTTP error. For the moment not
// expecting redirect or other condition we can recover from
MORDOR_LOG_WARNING(g_log) << "Failed to GET " << inUrl << " - " << request->response().status.status;
return false;
}
if (!request->hasResponseBody()) {
MORDOR_LOG_WARNING(g_log) << "Got no response from " << inUrl;
return false;
}
transferStream(request->responseStream(), dst);
return true;
}
catch(HTTP::InvalidResponseException &ex) {
MORDOR_LOG_WARNING(g_log) << "Failed to retrieve text from " << inUrl << " - " << ex.request()->response().status.status;
}
catch(std::invalid_argument &) {
MORDOR_LOG_WARNING(g_log) << "Invalid URL argument " << inUrl;
}
catch(Mordor::TimedOutException &) {
MORDOR_LOG_WARNING(g_log) << "Timeout attempting to retrieve " << inUrl;
}
catch(Mordor::Exception &) {
MORDOR_LOG_ERROR(g_log) << "Failed to retrieve " << inUrl << " - " << boost::current_exception_diagnostic_information();
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment