|
| 1 | +/// |
| 2 | +/// \file HTTP.cxx |
| 3 | +/// \author Adam Wegrzynek <[email protected]> |
| 4 | +/// |
| 5 | + |
| 6 | +#include "HTTP.h" |
| 7 | +#include "../MonLogger.h" |
| 8 | +#include "../Exceptions/MonitoringException.h" |
| 9 | +#include <boost/algorithm/string.hpp> |
| 10 | + |
| 11 | +namespace o2 |
| 12 | +{ |
| 13 | +/// ALICE O2 Monitoring system |
| 14 | +namespace monitoring |
| 15 | +{ |
| 16 | +/// Monitoring transports |
| 17 | +namespace transports |
| 18 | +{ |
| 19 | + |
| 20 | +HTTP::HTTP(const std::string& url) |
| 21 | +{ |
| 22 | + mHeaders = NULL; |
| 23 | + mCurl = curl_easy_init(); |
| 24 | + curl_easy_setopt(mCurl, CURLOPT_URL, url.c_str()); |
| 25 | + curl_easy_setopt(mCurl, CURLOPT_SSL_VERIFYPEER, 0); |
| 26 | + curl_easy_setopt(mCurl, CURLOPT_CONNECTTIMEOUT, 10); |
| 27 | + curl_easy_setopt(mCurl, CURLOPT_TIMEOUT, 10); |
| 28 | + curl_easy_setopt(mCurl, CURLOPT_POST, 1); |
| 29 | + curl_easy_setopt(mCurl, CURLOPT_TCP_KEEPIDLE, 120L); |
| 30 | + curl_easy_setopt(mCurl, CURLOPT_TCP_KEEPINTVL, 60L); |
| 31 | + FILE *devnull = fopen("/dev/null", "w+"); |
| 32 | + curl_easy_setopt(mCurl, CURLOPT_WRITEDATA, devnull); |
| 33 | + |
| 34 | + MonLogger::Get() << "HTTP transport initialized (" << url << ")" << MonLogger::End(); |
| 35 | +} |
| 36 | + |
| 37 | +HTTP::~HTTP() |
| 38 | +{ |
| 39 | + curl_slist_free_all(mHeaders); |
| 40 | + curl_easy_cleanup(mCurl); |
| 41 | + curl_global_cleanup(); |
| 42 | +} |
| 43 | + |
| 44 | +void HTTP::addHeader(const std::string& header) |
| 45 | +{ |
| 46 | + mHeaders = curl_slist_append(mHeaders, header.c_str()); |
| 47 | + curl_easy_setopt(mCurl, CURLOPT_HTTPHEADER, mHeaders); |
| 48 | +} |
| 49 | + |
| 50 | +void HTTP::send(std::string&& post) |
| 51 | +{ |
| 52 | + CURLcode response; |
| 53 | + long responseCode; |
| 54 | + curl_easy_setopt(mCurl, CURLOPT_POSTFIELDS, post.c_str()); |
| 55 | + curl_easy_setopt(mCurl, CURLOPT_POSTFIELDSIZE, (long) post.length()); |
| 56 | + response = curl_easy_perform(mCurl); |
| 57 | + curl_easy_getinfo(mCurl, CURLINFO_RESPONSE_CODE, &responseCode); |
| 58 | + if (response != CURLE_OK) { |
| 59 | + MonLogger::Get() << "HTTP Tranport " << curl_easy_strerror(response) << MonLogger::End(); |
| 60 | + } |
| 61 | + if (responseCode < 200 || responseCode > 206) { |
| 62 | + MonLogger::Get() << "HTTP Transport: Response code : " << std::to_string(responseCode) << MonLogger::End(); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +} // namespace transports |
| 67 | +} // namespace monitoring |
| 68 | +} // namespace o2 |
0 commit comments