| 38 | } |
| 39 | |
| 40 | OperationResult HttpFetcher::Fetch(std::string const& url, std::vector<char> & response) |
| 41 | { |
| 42 | cancelling_ = false; |
| 43 | socket_ = NULL; |
| 44 | |
| 45 | if (curl_ == NULL) { |
| 46 | curl_ = curl_easy_init(); |
| 47 | } |
| 48 | |
| 49 | curl_easy_setopt(curl_, CURLOPT_URL, url.c_str()); |
| 50 | curl_easy_setopt(curl_, CURLOPT_NOBODY, 0l); |
| 51 | curl_easy_setopt(curl_, CURLOPT_HEADER, 0l); |
| 52 | curl_easy_setopt(curl_, CURLOPT_FAILONERROR, 1l); |
| 53 | curl_easy_setopt(curl_, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| 54 | curl_easy_setopt(curl_, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA | CURLSSLOPT_REVOKE_BEST_EFFORT); |
| 55 | |
| 56 | if (Timeout) { |
| 57 | curl_easy_setopt(curl_, CURLOPT_TIMEOUT_MS, *Timeout); |
| 58 | curl_easy_setopt(curl_, CURLOPT_CONNECTTIMEOUT_MS, std::min(*Timeout, ConnectionTimeout)); |
| 59 | } else { |
| 60 | curl_easy_setopt(curl_, CURLOPT_TIMEOUT_MS, 0); |
| 61 | curl_easy_setopt(curl_, CURLOPT_CONNECTTIMEOUT_MS, ConnectionTimeout); |
| 62 | } |
| 63 | |
| 64 | if (DebugLogging) { |
| 65 | curl_easy_setopt(curl_, CURLOPT_VERBOSE, 1l); |
| 66 | curl_easy_setopt(curl_, CURLOPT_DEBUGFUNCTION, &DebugFunc); |
| 67 | } |
| 68 | |
| 69 | if (IPv4Only) { |
| 70 | curl_easy_setopt(curl_, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
| 71 | } |
| 72 | |
| 73 | curl_easy_setopt(curl_, CURLOPT_XFERINFOFUNCTION, &XferInfoFunc); |
| 74 | curl_easy_setopt(curl_, CURLOPT_XFERINFODATA, this); |
| 75 | curl_easy_setopt(curl_, CURLOPT_OPENSOCKETFUNCTION, &OpenSocketFunc); |
| 76 | curl_easy_setopt(curl_, CURLOPT_OPENSOCKETDATA, &this->socket_); |
| 77 | curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, &WriteFunc); |
| 78 | curl_easy_setopt(curl_, CURLOPT_WRITEDATA, this); |
| 79 | lastResponse_.clear(); |
| 80 | |
| 81 | DEBUG("Start cURL fetch for URL %s", url.c_str()); |
| 82 | DEBUG(" (Debug %s, IPv4 only %s)", (DebugLogging ? "on" : "off"), (IPv4Only ? "on" : "off")); |
| 83 | |
| 84 | lastResult_ = curl_easy_perform(curl_); |
| 85 | if (lastResult_ != CURLE_OK) { |
| 86 | LogError(curl_, lastResult_); |
| 87 | } else { |
| 88 | DEBUG(" Fetch succeeded (%lld bytes)", lastResponse_.size()); |
| 89 | } |
| 90 | |
| 91 | response = lastResponse_; |
| 92 | if (lastResult_ == CURLE_OK) { |
| 93 | return OperationSuccessful{}; |
| 94 | } else { |
| 95 | return ErrorReason{ TransferCategory, lastResult_, lastError_ }; |
| 96 | } |
| 97 | } |