Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.98 KB | None | 0 0
  1. void HTTPClient::ExecuteRequest()
  2. {
  3.     struct curl_slist* curlHeaders = 0;
  4.     char curlErrorBuffer[CURL_ERROR_SIZE];
  5.  
  6.     try
  7.     {
  8.         this->curlHandle = curl_easy_init();
  9.         SetStandardCurlHandleOptions(curlHandle);
  10.  
  11.         // This error buffer cannot be shared, because it's not protected by a mutex.
  12.         SET_CURL_OPTION(curlHandle, CURLOPT_URL, url.c_str());
  13.         SET_CURL_OPTION(curlHandle, CURLOPT_ERRORBUFFER, &curlErrorBuffer);
  14.  
  15.         SET_CURL_OPTION(curlHandle, CURLOPT_HEADERFUNCTION, &CurlHeaderCallback);
  16.         SET_CURL_OPTION(curlHandle, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
  17.         SET_CURL_OPTION(curlHandle, CURLOPT_PROGRESSFUNCTION, &CurlProgressCallback);
  18.         SET_CURL_OPTION(curlHandle, CURLOPT_WRITEHEADER, this);
  19.         SET_CURL_OPTION(curlHandle, CURLOPT_WRITEDATA, this);
  20.         SET_CURL_OPTION(curlHandle, CURLOPT_PROGRESSDATA, this);
  21.         // Zero means don't verify peer cert - we might want to
  22.         // make this configurable in the future
  23.         SET_CURL_OPTION(curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
  24.         SET_CURL_OPTION(curlHandle, CURLOPT_SSL_VERIFYHOST, 0);
  25.  
  26.         // Use any type of authentication available
  27.         SET_CURL_OPTION(curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  28.        
  29.         // Progress must be turned on for CURLOPT_PROGRESSFUNCTION to be called.
  30.         SET_CURL_OPTION(curlHandle, CURLOPT_NOPROGRESS, 0);
  31.  
  32.         this->SetRequestData();
  33.         this->SetupCurlMethodType();
  34.  
  35.         SET_CURL_OPTION(curlHandle, CURLOPT_MAXREDIRS, this->maxRedirects);
  36.         SET_CURL_OPTION(curlHandle, CURLOPT_USERAGENT,
  37.             this->GetString("userAgent").c_str());
  38.  
  39.         curlHeaders = SetRequestHeaders(curlHandle);
  40.         SetCurlProxySettings(curlHandle, ProxyConfig::GetProxyForURL(url));
  41.  
  42.         if (this->timeout > 0)
  43.         {
  44.             SET_CURL_OPTION(curlHandle, CURLOPT_TIMEOUT_MS, this->timeout);
  45.             SET_CURL_OPTION(curlHandle, CURLOPT_DNS_CACHE_TIMEOUT, this->timeout/1000);
  46.         }
  47.  
  48.         SetRequestCookies(curlHandle, this->requestCookies);
  49.  
  50.         if (!this->username.empty() || !this->password.empty())
  51.         {
  52.             std::string usernamePassword(this->username);
  53.             usernamePassword.append(":");
  54.             usernamePassword.append(this->password);
  55.             SET_CURL_OPTION(curlHandle, CURLOPT_USERPWD, usernamePassword.c_str());
  56.         }
  57.  
  58.         this->Set("connected", Value::NewBool(true));
  59.         this->HandleCurlResult(curl_easy_perform(curlHandle));
  60.         this->Set("connected", Value::NewBool(false));
  61.  
  62.         if (!responseData.empty())
  63.             this->SetObject("responseData", Bytes::Concat(this->responseData));
  64.  
  65.         CleanupCurl(curlHeaders);
  66.  
  67.         this->ChangeState(4); // Done
  68.     }
  69.     catch (ValueException& e)
  70.     {
  71.         GetLogger()->Error("Request to %s failed because: %s",
  72.         this->url.c_str(), e.ToString().c_str());
  73.  
  74.         this->CleanupCurl(curlHeaders);
  75.         if (!async)
  76.             throw e;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement