Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:36:25

0001 #include "CondCore/CondDB/interface/WebUtils.h"
0002 //
0003 #include <curl/curl.h>
0004 #include <cstdio>
0005 #include <cstring>
0006 
0007 namespace cond {
0008 
0009   // callback to obtain the Get result
0010   static size_t getBodyCallback(void* contents, size_t size, size_t nmemb, void* ptr) {
0011     // Cast ptr to std::string pointer and append contents to that string
0012     ((std::string*)ptr)->append((char*)contents, size * nmemb);
0013     return size * nmemb;
0014   }
0015 
0016   unsigned long httpGet(const std::string& urlString, std::string& info) {
0017     CURL* curl;
0018     CURLcode res;
0019     std::string body;
0020     char errbuf[CURL_ERROR_SIZE];
0021 
0022     curl = curl_easy_init();
0023     unsigned long ret = false;
0024     if (curl) {
0025       struct curl_slist* chunk = nullptr;
0026       chunk = curl_slist_append(chunk, "content-type:application/json");
0027       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
0028       curl_easy_setopt(curl, CURLOPT_URL, urlString.c_str());
0029       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, getBodyCallback);
0030       curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
0031       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
0032       res = curl_easy_perform(curl);
0033       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &ret);
0034       if (CURLE_OK == res) {
0035         info = body;
0036       } else {
0037         size_t len = strlen(errbuf);
0038         fprintf(stderr, "\nlibcurl: (%d) ", res);
0039         if (len)
0040           fprintf(stderr, "%s%s", errbuf, ((errbuf[len - 1] != '\n') ? "\n" : ""));
0041         else
0042           fprintf(stderr, "%s\n", curl_easy_strerror(res));
0043       }
0044       curl_easy_cleanup(curl);
0045     }
0046     return ret;
0047   }
0048 
0049 }  // namespace cond