CernVM-FS  2.12.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 
9 
10 
11 #include "util/logging.h"
12 #include "util/string.h"
13 
14 namespace {
15 
17 
18 struct CurlBuffer {
19  std::string data;
20 };
21 
22 size_t RecvCB(void* buffer, size_t size, size_t nmemb, void* userp) {
23  CurlBuffer* my_buffer = static_cast<CurlBuffer*>(userp);
24 
25  if (size * nmemb < 1) {
26  return 0;
27  }
28 
29  my_buffer->data = static_cast<char*>(buffer);
30 
31  return my_buffer->data.size();
32 }
33 
34 } // namespace
35 
36 namespace notify {
37 
38 PublisherHTTP::PublisherHTTP(const std::string& server_url)
39  : server_url_(server_url + "/notifications/publish") {}
40 
42 
43 bool PublisherHTTP::Publish(const std::string& msg, const std::string& topic) {
44  const char* user_agent_string = "cvmfs/" CVMFS_VERSION;
45 
46  CURL* h_curl = curl_easy_init();
47 
48  if (h_curl) {
49  curl_easy_setopt(h_curl, CURLOPT_NOPROGRESS, 1L);
50  curl_easy_setopt(h_curl, CURLOPT_USERAGENT, user_agent_string);
51  curl_easy_setopt(h_curl, CURLOPT_MAXREDIRS, 50L);
52  curl_easy_setopt(h_curl, CURLOPT_CUSTOMREQUEST, "POST");
53  }
54 
55  if (!h_curl) {
56  LogCvmfs(kLogCvmfs, kLogError, "Error initializing CURL context.");
57  return false;
58  }
59 
60  CurlBuffer buffer;
61  // Make request to acquire lease from repo services
62  curl_easy_setopt(h_curl, CURLOPT_URL, server_url_.c_str());
63  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDSIZE_LARGE,
64  static_cast<curl_off_t>(msg.length()));
65  curl_easy_setopt(h_curl, CURLOPT_POSTFIELDS, msg.c_str());
66  curl_easy_setopt(h_curl, CURLOPT_WRITEFUNCTION, RecvCB);
67  curl_easy_setopt(h_curl, CURLOPT_WRITEDATA, &buffer);
68 
69  CURLcode ret = curl_easy_perform(h_curl);
70  if (ret) {
71  LogCvmfs(kLogCvmfs, kLogError, "POST request failed: %d. Reply: %s", ret,
72  buffer.data.c_str());
73  }
74 
75  curl_easy_cleanup(h_curl);
76  h_curl = NULL;
77 
78  return !ret;
79 }
80 
81 } // namespace notify
const LogFacilities & kLogError
Definition: cmd_pub.cc:23
PublisherHTTP(const std::string &server_url)
std::string data
virtual bool Publish(const std::string &msg, const std::string &topic)
size_t RecvCB(void *buffer, size_t size, size_t nmemb, void *userp)
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:528