-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalicenet.hpp
152 lines (125 loc) · 4.48 KB
/
alicenet.hpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <cstddef>
#include <cstring>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <type_traits>
#include <curl/curl.h>
#include <stdexcept>
#include <vector>
#include <map>
#pragma cling load("curl") // for cling
#pragma comment(lib, "curl")
class alicenetresponse {
public:
std::string data;
long code;
std::map<std::string, std::string> headers;
double timeElapsed;
};
struct alicenetoptions {
std::string userAgent;
std::map<std::string, std::string> headers;
std::string type;
std::string authentication;
std::string postOptions;
};
class alicenet { // The class
public: // Access specifier
alicenet(std::string url) {
this->url = url;
this->makeRequest();
}
alicenet(std::string url, alicenetoptions options) {
this->url = url;
headers = options.headers;
type = options.type;
postOptions = options.postOptions;
userAgent = options.userAgent;
authentication = options.authentication;
this->makeRequest();
}
alicenet();
std::string url;
std::map<std::string, std::string> headers;
std::string type;
std::string postOptions;
std::string userAgent;
std::string authentication;
double timeElapsed;
alicenetresponse response;
void makeRequest();
};
inline size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
inline std::vector<std::string> makeHeaderStringIntoVector(std::string header_string) {
std::vector<std::string> result;
size_t pos = 0;
while ((pos = header_string.find("\n")) != std::string::npos) {
result.push_back(header_string.substr(0, pos));
header_string.erase(0, pos + 1);
}
return result;
}
inline std::map<std::string, std::string> makeHeaderVectorIntoMap(std::vector<std::string> header_vector) {
std::map<std::string, std::string> result;
for (std::string header: header_vector) {
size_t pos = 0;
while ((pos = header.find(": ")) != std::string::npos) {
std::string headerp1 = header.substr(0, pos);
header.erase(0, pos + 2);
result[headerp1] = header;
}
}
return result;
}
inline void alicenet::makeRequest() {
if (userAgent.empty())
userAgent = "curl/7.42.0";
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
if (!authentication.empty())
curl_easy_setopt(curl, CURLOPT_USERPWD, authentication.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, userAgent.c_str());
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
if (type == "POST")
curl_easy_setopt(curl, CURLOPT_POST, 1L);
if (!postOptions.empty())
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postOptions.c_str());
if (!headers.empty()) {
struct curl_slist* headersList = NULL;
for (auto header: headers) {
std::string headerstring = header.first + ": " + header.second; // make the header string
headersList = curl_slist_append(headersList, headerstring.c_str());
}
}
// std::string response_string;
std::string header_string;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response.data);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header_string);
CURLcode curlPref = curl_easy_perform(curl);
char* url;
long response_code = 0;
double elapsed;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (curlPref != CURLE_OK) {
std::cerr << "CURL failed to execute: " << curl_easy_strerror(curlPref);
throw curl_easy_strerror(curlPref);
}
curl_easy_cleanup(curl);
curl = NULL;
// responseData = response_string;
response.code = response_code;
response.headers = makeHeaderVectorIntoMap(makeHeaderStringIntoVector(header_string));
timeElapsed = elapsed;
}
}