forked from MCMrARM/Google-Play-API
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp.cpp
More file actions
249 lines (229 loc) · 8.53 KB
/
http.cpp
File metadata and controls
249 lines (229 loc) · 8.53 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <playapi/util/http.h>
#include <cassert>
#include <stdexcept>
#include <zlib.h>
#include <thread>
#include <memory>
#include <sstream>
using namespace playapi;
static std::string redact_url_query(const std::string& url) {
auto pos = url.find('?');
if (pos == std::string::npos) return url;
std::string base = url.substr(0, pos + 1); // include '?'
std::string query = url.substr(pos + 1);
std::string out;
size_t i = 0;
while (i < query.size()) {
size_t amp = query.find('&', i);
std::string part = (amp == std::string::npos) ? query.substr(i) : query.substr(i, amp - i);
size_t eq = part.find('=');
if (eq == std::string::npos) {
// key without value
out += part;
} else {
// keep key and '=' then redact value
out += part.substr(0, eq + 1);
out += "[redacted]";
}
if (amp == std::string::npos) break;
out += '&';
i = amp + 1;
}
return base + out;
}
void url_encoded_entity::add_pair(const std::string& key, const std::string& val) {
pairs.push_back({key, val});
}
std::string url_encoded_entity::encode(CURL* curl) const {
std::stringstream ss;
bool f = true;
for (const auto& pair : pairs) {
if (f)
f = false;
else
ss << "&";
char* escapedKey = curl_easy_escape(curl, pair.first.c_str(), (int) pair.first.length());
char* escapedVal = curl_easy_escape(curl, pair.second.c_str(), (int) pair.second.length());
ss << escapedKey << "=" << escapedVal;
curl_free(escapedKey);
curl_free(escapedVal);
}
return ss.str();
}
std::string url_encoded_entity::encode() const {
CURL* curl = curl_easy_init();
assert(curl != nullptr);
std::string ret = encode(curl);
curl_easy_cleanup(curl);
return std::move(ret);
}
http_response::http_response(CURLcode curlCode, long statusCode, std::string body) :
curlCode(curlCode), statusCode(statusCode), body(std::move(body)) {
//
}
http_response::http_response(http_response&& r) : curlCode(r.curlCode), statusCode(r.statusCode),
body(r.body) {
r.curlCode = CURLE_FAILED_INIT;
r.statusCode = 0;
r.body = std::string();
}
http_response& http_response::operator=(http_response&& r) {
curlCode = r.curlCode;
body = r.body;
r.curlCode = CURLE_FAILED_INIT;
r.body = std::string();
return *this;
}
http_response::~http_response() {
}
void http_request::set_body(const url_encoded_entity& ent) {
set_body(ent.encode());
}
void http_request::add_header(const std::string& key, const std::string& value) {
headers[key] = value;
}
void http_request::set_gzip_body(const std::string& str) {
z_stream zs;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
int ret = deflateInit2(&zs, Z_BEST_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
assert(ret == Z_OK);
zs.avail_in = (uInt) str.length();
zs.next_in = (unsigned char*) str.data();
std::string out;
while(true) {
out.resize(out.size() + 4096);
zs.avail_out = 4096;
zs.next_out = (unsigned char*) out.data();
ret = deflate(&zs, Z_FINISH);
assert(ret != Z_STREAM_ERROR);
if (zs.avail_out != 0) {
out.resize(out.size() - zs.avail_out);
break;
}
}
deflateEnd(&zs);
body = std::move(out);
}
size_t http_request::curl_stringstream_write_func(void* ptr, size_t size, size_t nmemb, std::stringstream* s) {
s->write((char*) ptr, size * nmemb);
return size * nmemb;
}
size_t http_request::curl_write_func(void* ptr, size_t size, size_t nmemb, output_callback* s) {
return (*s)((char*) ptr, size * nmemb);
}
int http_request::curl_xferinfo(void* ptr, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) {
progress_callback* req = (progress_callback*) ptr;
(*req)(dltotal, dlnow, ultotal, ulnow);
return 0;
}
CURL* http_request::build(std::stringstream& output, bool copy_body) {
CURL* curl = curl_easy_init();
assert(curl != nullptr);
assert(url.length() > 0);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
switch (method) {
case http_method::GET:
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
break;
case http_method::POST:
curl_easy_setopt(curl, CURLOPT_POST, 1L);
break;
case http_method::PUT:
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
break;
}
if (user_agent.length() > 0)
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.c_str());
if (encoding.length() > 0)
curl_easy_setopt(curl, CURLOPT_ENCODING, encoding.c_str());
if (body.length() > 0) {
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) body.length());
curl_easy_setopt(curl, copy_body ? CURLOPT_COPYPOSTFIELDS : CURLOPT_POSTFIELDS, body.c_str());
}
if (headers.size() > 0) {
struct curl_slist* chunk = NULL;
if (method == http_method::POST)
chunk = curl_slist_append(chunk, "Expect:");
for (const auto& header : headers) {
chunk = curl_slist_append(chunk, (header.first + ": " + header.second).c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
}
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, follow_location ? 1L : 0L);
#ifndef NDEBUG
printf("http request: %s, body = %s\n", url.c_str(), body.c_str());
#endif
if (callback_output) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &callback_output);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_func);
} else {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_stringstream_write_func);
}
if (callback_progress) {
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_xferinfo);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &callback_progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
}
return curl;
}
http_response http_request::perform() {
std::stringstream output;
CURL* curl = build(output);
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
CURLcode curlerr = curl_easy_perform(curl);
if (curlerr == CURLE_OK) {
long status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
#ifndef NDEBUG
printf("http response body: %s\n", output.str().c_str());
#endif
curl_easy_cleanup(curl);
return http_response(curlerr, status, output.str());
} else {
std::stringstream errormsg;
errormsg << "Failed to perform http request to " << redact_url_query(url) << " : CURLcode " << curlerr << " Details: " << errbuf;
curl_easy_cleanup(curl);
throw std::runtime_error(errormsg.str().data());
}
}
void http_request::perform(std::function<void(http_response)> success, std::function<void(std::exception_ptr)> error) {
auto req = std::make_shared<http_request>(*this);
std::thread([req, success, error]() {
std::stringstream output;
CURL* curl = req->build(output);
char errbuf[CURL_ERROR_SIZE];
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
if (req->callback_output) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &req->callback_output);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_func);
}
if (req->callback_progress) {
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, curl_xferinfo);
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &req->callback_progress);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
}
CURLcode curlerr = curl_easy_perform(curl);
if (curlerr == CURLE_OK) {
long status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
#ifndef NDEBUG
printf("http response body: %s\n", output.str().c_str());
#endif
success(http_response(curlerr, status, output.str()));
} else {
std::stringstream errormsg;
errormsg << "Failed to perform http request to " << redact_url_query(req->url) << " : CURLcode " << curlerr << " Details: " << errbuf;
try {
throw std::runtime_error(errormsg.str().data());
} catch (...) {
error(std::current_exception());
}
}
curl_easy_cleanup(curl);
}).detach();
}