Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Add Comment
Please, Sign In to add comment