Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. Response WebClient::SendRequest(const Request& Request) const
  2.     {
  3.         Response Result;
  4.         CURL* curl;
  5.         curl_global_init(CURL_GLOBAL_ALL);
  6.         curl = curl_easy_init();
  7.         if(curl)
  8.         {
  9.             std::string Buffer;
  10.             std::string FullURL = this->Domain + Request.URL;
  11.             curl_easy_setopt(curl, CURLOPT_URL, FullURL);
  12.             if (!this->WebProxy.Host.empty() && !this->WebProxy.Port.empty())
  13.             {
  14.                 std::string Temp = this->WebProxy.Host + ":" + this->WebProxy.Port;
  15.                 curl_easy_setopt(curl, CURLOPT_PROXY, Temp);
  16.             }
  17.  
  18.             if (!this->WebProxy.Username.empty())
  19.             {
  20.                 curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, this->WebProxy.Username.c_str());
  21.                 if (!this->WebProxy.Password.empty())
  22.                 {
  23.                     curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, this->WebProxy.Password.c_str());
  24.                 }
  25.             }
  26.             switch (Request.Method)
  27.             {
  28.                 case GET:
  29.                 {
  30.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
  31.                     break;
  32.                 }
  33.                 case POST:
  34.                 {
  35.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  36.                     break;
  37.                 }
  38.                 case HttpMethod::DELETEE:
  39.                 {
  40.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  41.                     break;
  42.                 }
  43.                 case HttpMethod::HEAD:
  44.                 {
  45.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
  46.                     break;
  47.                 }
  48.                 case HttpMethod::MERGE:
  49.                 {
  50.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "MERGE");
  51.                     break;
  52.                 }
  53.                 case HttpMethod::OPTIONS:
  54.                 {
  55.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "OPTIONS");
  56.                     break;
  57.                 }
  58.                 case HttpMethod::PUT:
  59.                 {
  60.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
  61.                     break;
  62.                 }
  63.                 case HttpMethod::COPY:
  64.                 {
  65.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "COPY");
  66.                     break;
  67.                 }
  68.                 case HttpMethod::PATCH:
  69.                 {
  70.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
  71.                     break;
  72.                 }
  73.                 default:
  74.                 {
  75.                     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
  76.                     break;
  77.                 }
  78.             }
  79.  
  80.             if(Request.AutoFollow)
  81.                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, true);
  82.  
  83.             struct curl_slist* hs = NULL;
  84.             hs = curl_slist_append(hs, Request.ContentType.c_str());
  85.             curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
  86.  
  87.             if (!Request.Headers.empty())
  88.             {
  89.                 for (const auto& Header : Request.Headers)
  90.                 {
  91.                     struct curl_slist* hs = NULL; // Might be a memory leak (?)
  92.                     std::string Temp = Header.first + ":" + Header.second;
  93.                     hs = curl_slist_append(hs, Temp.c_str());
  94.                     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, hs);
  95.                 }
  96.             }
  97.             if(!Request.Data.empty())
  98.                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Request.Data);
  99.  
  100.             curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Write);
  101.             curl_easy_setopt(curl, CURLOPT_WRITEDATA, &Result.Content);
  102.             Result.CurlCode = curl_easy_perform(curl);
  103.             if (Result.CurlCode != CURLE_OK)
  104.             {
  105.                 fprintf(stderr, "curl_easy_perform() failed: %s\n",
  106.                 curl_easy_strerror(Result.CurlCode));
  107.             }
  108.             curl_easy_cleanup(curl);
  109.         }
  110.         curl_global_cleanup();
  111.         return Result;
  112.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement