GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/notify/publisher_http.cc
Date: 2024-04-28 02:33:07
Exec Total Coverage
Lines: 0 32 0.0%
Branches: 0 12 0.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "publisher_http.h"
6
7 #include "duplex_curl.h"
8
9 #include "cvmfs_config.h"
10
11 #include "util/logging.h"
12 #include "util/string.h"
13
14 namespace {
15
16 const LogFacilities& kLogError = DefaultLogging::error;
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
41 PublisherHTTP::~PublisherHTTP() {}
42
43 bool PublisherHTTP::Publish(const std::string& msg, const std::string& topic) {
44 const char* user_agent_string = "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
82