Guest User

Untitled

a guest
Oct 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. static bool downloadToStream(const char * inUrl, Stream &dst, Mordor::IOManager * iomanager)
  2. {
  3. boost::scoped_ptr<WorkerPool> workInThisThread;
  4. if (Scheduler::getThis() == NULL) {
  5. workInThisThread.reset(new WorkerPool(1, true));
  6. }
  7.  
  8. HTTP::RequestBrokerOptions options;
  9. options.ioManager = iomanager;
  10.  
  11. HTTP::RequestBroker::ptr requestBroker = HTTP::createRequestBroker(options).first;
  12.  
  13. try {
  14.  
  15. URI uri(inUrl);
  16.  
  17. HTTP::Request requestHeaders;
  18. requestHeaders.requestLine.uri = uri;
  19. requestHeaders.request.host = uri.authority.host();
  20.  
  21. HTTP::ClientRequest::ptr request = requestBroker->request(requestHeaders);
  22.  
  23. if (request->response().status.status != HTTP::OK) {
  24. // possibly NOT_FOUND or other HTTP error. For the moment not
  25. // expecting redirect or other condition we can recover from
  26. MORDOR_LOG_WARNING(g_log) << "Failed to GET " << inUrl << " - " << request->response().status.status;
  27. return false;
  28. }
  29.  
  30. if (!request->hasResponseBody()) {
  31. MORDOR_LOG_WARNING(g_log) << "Got no response from " << inUrl;
  32. return false;
  33. }
  34.  
  35. transferStream(request->responseStream(), dst);
  36. return true;
  37. }
  38. catch(HTTP::InvalidResponseException &ex) {
  39. MORDOR_LOG_WARNING(g_log) << "Failed to retrieve text from " << inUrl << " - " << ex.request()->response().status.status;
  40. }
  41. catch(std::invalid_argument &) {
  42. MORDOR_LOG_WARNING(g_log) << "Invalid URL argument " << inUrl;
  43. }
  44. catch(Mordor::TimedOutException &) {
  45. MORDOR_LOG_WARNING(g_log) << "Timeout attempting to retrieve " << inUrl;
  46. }
  47. catch(Mordor::Exception &) {
  48. MORDOR_LOG_ERROR(g_log) << "Failed to retrieve " << inUrl << " - " << boost::current_exception_diagnostic_information();
  49. }
  50.  
  51. return false;
  52. }
Add Comment
Please, Sign In to add comment