Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void HTTPClient::ExecuteRequest()
- {
- struct curl_slist* curlHeaders = 0;
- char curlErrorBuffer[CURL_ERROR_SIZE];
- try
- {
- this->curlHandle = curl_easy_init();
- SetStandardCurlHandleOptions(curlHandle);
- // This error buffer cannot be shared, because it's not protected by a mutex.
- SET_CURL_OPTION(curlHandle, CURLOPT_URL, url.c_str());
- SET_CURL_OPTION(curlHandle, CURLOPT_ERRORBUFFER, &curlErrorBuffer);
- SET_CURL_OPTION(curlHandle, CURLOPT_HEADERFUNCTION, &CurlHeaderCallback);
- SET_CURL_OPTION(curlHandle, CURLOPT_WRITEFUNCTION, &CurlWriteCallback);
- SET_CURL_OPTION(curlHandle, CURLOPT_PROGRESSFUNCTION, &CurlProgressCallback);
- SET_CURL_OPTION(curlHandle, CURLOPT_WRITEHEADER, this);
- SET_CURL_OPTION(curlHandle, CURLOPT_WRITEDATA, this);
- SET_CURL_OPTION(curlHandle, CURLOPT_PROGRESSDATA, this);
- // Zero means don't verify peer cert - we might want to
- // make this configurable in the future
- SET_CURL_OPTION(curlHandle, CURLOPT_SSL_VERIFYPEER, 0);
- SET_CURL_OPTION(curlHandle, CURLOPT_SSL_VERIFYHOST, 0);
- // Use any type of authentication available
- SET_CURL_OPTION(curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
- // Progress must be turned on for CURLOPT_PROGRESSFUNCTION to be called.
- SET_CURL_OPTION(curlHandle, CURLOPT_NOPROGRESS, 0);
- this->SetRequestData();
- this->SetupCurlMethodType();
- SET_CURL_OPTION(curlHandle, CURLOPT_MAXREDIRS, this->maxRedirects);
- SET_CURL_OPTION(curlHandle, CURLOPT_USERAGENT,
- this->GetString("userAgent").c_str());
- curlHeaders = SetRequestHeaders(curlHandle);
- SetCurlProxySettings(curlHandle, ProxyConfig::GetProxyForURL(url));
- if (this->timeout > 0)
- {
- SET_CURL_OPTION(curlHandle, CURLOPT_TIMEOUT_MS, this->timeout);
- SET_CURL_OPTION(curlHandle, CURLOPT_DNS_CACHE_TIMEOUT, this->timeout/1000);
- }
- SetRequestCookies(curlHandle, this->requestCookies);
- if (!this->username.empty() || !this->password.empty())
- {
- std::string usernamePassword(this->username);
- usernamePassword.append(":");
- usernamePassword.append(this->password);
- SET_CURL_OPTION(curlHandle, CURLOPT_USERPWD, usernamePassword.c_str());
- }
- this->Set("connected", Value::NewBool(true));
- this->HandleCurlResult(curl_easy_perform(curlHandle));
- this->Set("connected", Value::NewBool(false));
- if (!responseData.empty())
- this->SetObject("responseData", Bytes::Concat(this->responseData));
- CleanupCurl(curlHeaders);
- this->ChangeState(4); // Done
- }
- catch (ValueException& e)
- {
- GetLogger()->Error("Request to %s failed because: %s",
- this->url.c_str(), e.ToString().c_str());
- this->CleanupCurl(curlHeaders);
- if (!async)
- throw e;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement