Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Response WebClient::SendRequest(const Request& Request) const
- {
- Response Result;
- CURL* curl;
- curl_global_init(CURL_GLOBAL_ALL);
- curl = curl_easy_init();
- if(curl)
- {
- std::string Buffer;
- std::string FullURL = this->Domain + Request.URL;
- curl_easy_setopt(curl, CURLOPT_URL, FullURL);
- if (!this->WebProxy.Host.empty() && !this->WebProxy.Port.empty())
- {
- std::string Temp = this->WebProxy.Host + ":" + this->WebProxy.Port;
- curl_easy_setopt(curl, CURLOPT_PROXY, Temp);
- }
- if (!this->WebProxy.Username.empty())
- {
- curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, this->WebProxy.Username.c_str());
- if (!this->WebProxy.Password.empty())
- {
- curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, this->WebProxy.Password.c_str());
- }
- }
- switch (Request.Method)
- {
- case GET:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
- break;
- }
- case POST:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- break;
- }
- case HttpMethod::DELETEE:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
- break;
- }
- case HttpMethod::HEAD:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
- break;
- }
- case HttpMethod::MERGE:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "MERGE");
- break;
- }
- case HttpMethod::OPTIONS:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
- break;
- }
- case HttpMethod::PUT:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
- break;
- }
- case HttpMethod::COPY:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "COPY");
- break;
- }
- case HttpMethod::PATCH:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
- break;
- }
- default:
- {
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
- break;
- }
- }
- if(Request.AutoFollow)
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
- struct curl_slist* hs = NULL;
- hs = curl_slist_append(hs, Request.ContentType.c_str());
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
- if (!Request.Headers.empty())
- {
- for (const auto& Header : Request.Headers)
- {
- struct curl_slist* hs = NULL; // Might be a memory leak (?)
- std::string Temp = Header.first + ":" + Header.second;
- hs = curl_slist_append(hs, Temp.c_str());
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
- }
- }
- if(!Request.Data.empty())
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Request.Data);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write);
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, &Result.Content);
- Result.CurlCode = curl_easy_perform(curl);
- if (Result.CurlCode != CURLE_OK)
- {
- fprintf(stderr, "curl_easy_perform() failed: %s\n",
- curl_easy_strerror(Result.CurlCode));
- }
- curl_easy_cleanup(curl);
- }
- curl_global_cleanup();
- return Result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement