-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.cc
125 lines (101 loc) · 3.98 KB
/
http.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "http.h"
#include <boost/log/trivial.hpp>
#include <memory>
#include <sstream>
#include "errors.h"
namespace taranis {
std::string
HttpClient::encode_query_parameter(const std::string ¶meter) const {
std::shared_ptr<char> escaped{
curl_easy_escape(nullptr, parameter.c_str(), parameter.size()),
&curl_free};
if (escaped == nullptr) {
return "";
}
std::string value{escaped.get()};
return value;
}
Json::Value HttpClient::get(const std::string &url) {
BOOST_LOG_TRIVIAL(debug) << "Sending GET request " << url;
this->ensure_network();
this->response_data.clear();
this->response_data.reserve(10 * CURLOPT_BUFFERSIZE);
auto curl = this->prepare_curl();
curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &this->response_data);
const CURLcode code = curl_easy_perform(curl.get());
if (code != CURLE_OK) {
BOOST_LOG_TRIVIAL(error) << "Error in GET request " << code << " " << url;
throw RequestError{code};
}
long response_code;
curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
BOOST_LOG_TRIVIAL(error)
<< "Unexpected response " << response_code << " " << url;
throw HttpError{response_code};
}
BOOST_LOG_TRIVIAL(debug) << "Received " << this->response_data.size()
<< " bytes";
Json::Value root;
Json::CharReaderBuilder reader;
reader["collectComments"] = false;
std::string json_errors;
std::stringstream input_stream{this->response_data};
try {
if (not Json::parseFromStream(reader, input_stream, &root, &json_errors)) {
BOOST_LOG_TRIVIAL(error)
<< "JSON parser error " << json_errors << " " << url;
}
} catch (const Json::Exception &error) {
BOOST_LOG_TRIVIAL(error) << error.what();
}
return root;
}
std::unique_ptr<CURL, void (*)(CURL *)> HttpClient::prepare_curl() {
std::unique_ptr<CURL, void (*)(CURL *)> curl{curl_easy_init(),
&curl_easy_cleanup};
curl_slist *headers = nullptr;
headers = curl_slist_append(headers,
"Content-Type: application/json; charset=UTF-8");
headers = curl_slist_append(headers, "X-Accept: application/json");
curl_easy_setopt(curl.get(), CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl.get(), CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl.get(), CURLOPT_USERAGENT, "taranis/0.0.1");
curl_easy_setopt(curl.get(), CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl.get(), CURLOPT_HTTP_VERSION,
static_cast<long>(CURL_HTTP_VERSION_2TLS));
curl_easy_setopt(curl.get(), CURLOPT_FTP_SKIP_PASV_IP, 1L);
curl_easy_setopt(curl.get(), CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, write_callback);
return curl;
}
void HttpClient::ensure_network() const {
const auto *netinfo = NetInfo();
if (netinfo == nullptr or not netinfo->connected) {
BOOST_LOG_TRIVIAL(debug) << "Will try to establish connection";
const char *network_name = nullptr;
const auto result = NetConnect2(network_name, true);
// This shows hourglass pointer and may popup a connection
// dialog
if (result != 0) {
BOOST_LOG_TRIVIAL(warning) << "Failed to establish connection";
throw ConnectionError{};
}
netinfo = NetInfo();
if (netinfo == nullptr or not netinfo->connected) {
BOOST_LOG_TRIVIAL(warning) << "Still not connected!";
throw ConnectionError{};
}
}
}
size_t HttpClient::write_callback(void *contents, size_t size, size_t nmemb,
void *userp) {
BOOST_LOG_TRIVIAL(debug) << "Writing " << size * nmemb << " bytes to buffer";
static_cast<std::string *>(userp)->append(static_cast<char *>(contents),
size * nmemb);
return size * nmemb;
}
} // namespace taranis