CernVM-FS  2.13.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
publisher_http.cc
Go to the documentation of this file.
1 
5 #include "publisher_http.h"
6 
7 #include "duplex_curl.h"
8 #include "util/logging.h"
9 #include "util/string.h"
10 
11 namespace {
12 
14 
15 struct CurlBuffer {
16  std::string data;
17 };
18 
19 size_t RecvCB(void *buffer, size_t size, size_t nmemb, void *userp) {
20  CurlBuffer *my_buffer = static_cast<CurlBuffer *>(userp);
21 
22  if (size * nmemb < 1) {
23  return 0;
24  }
25 
26  my_buffer->data = static_cast<char *>(buffer);
27 
28  return my_buffer->data.size();
29 }
30 
31 } // namespace
32 
33 namespace notify {
34 
35 PublisherHTTP::PublisherHTTP(const std::string &server_url)
36  : server_url_(server_url + "/notifications/publish") { }
37 
39 
40 bool PublisherHTTP::Publish(const std::string &msg, const std::string &topic) {
41  const char *user_agent_string = "cvmfs/" CVMFS_VERSION;
42 
43  CURL *h_curl = curl_easy_init();
44 
45  if (h_curl) {
46  curl_easy_setopt(h_curl, CURLOPT_NOPROGRESS, 1L);
47  curl_easy_setopt(h_curl, CURLOPT_USERAGENT, user_agent_string);
48  curl_easy_setopt(h_curl, CURLOPT_MAXREDIRS, 50L);
49  curl_easy_setopt(h_curl, CURLOPT_CUSTOMREQUEST, "POST");
50  }
51 
52  if (!h_curl) {
53  LogCvmfs(kLogCvmfs, kLogError, "Error initializing CURL context.");
54  return false;
55  }
56 
57  CurlBuffer buffer;
58  // Make request to acquire lease from repo services
59  curl_easy_setopt(h_curl, CURLOPT_URL, server_url_.c_str());
60  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
61  static_cast<curl_off_t>(msg.length()));
62  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, msg.c_str());
63  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
64  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, &buffer);
65 
66  const CURLcode ret = curl_easy_perform(h_curl);
67  if (ret) {
68  LogCvmfs(kLogCvmfs, kLogError, "POST request failed: %d. Reply: %s", ret,
69  buffer.data.c_str());
70  }
71 
72  curl_easy_cleanup(h_curl);
73  h_curl = NULL;
74 
75  return !ret;
76 }
77 
78 } // namespace notify
const LogFacilities & kLogError
Definition: cmd_pub.cc:23
PublisherHTTP(const std::string &server_url)
virtual bool Publish(const std::string &msg, const std::string &topic)
size_t RecvCB(void *buffer, size_t size, size_t nmemb, void *userp)
std::string data
LogFacilities
static void size_t size
Definition: smalloc.h:54
static LogFacilities error
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545