-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpRequest.cpp
More file actions
354 lines (284 loc) · 9.72 KB
/
HttpRequest.cpp
File metadata and controls
354 lines (284 loc) · 9.72 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "HttpRequest.h"
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
bool HttpRequest::GlobalInit()
{
return curl_global_init(CURL_GLOBAL_ALL) != 0;
}
void HttpRequest::GlobalCleanup()
{
curl_global_cleanup();
}
HttpRequest::HttpRequest()
{
fCurlHandle = curl_easy_init();
}
HttpRequest::~HttpRequest()
{
if (fCurlHandle)
{
curl_easy_cleanup(fCurlHandle);
}
}
struct MemoryStruct {
char *memory;
size_t size;
};
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(!ptr) {
/* out of memory! */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->memory = ptr;
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
static size_t writeFileCallback(void * contents, size_t size, size_t nitems, FILE *file)
{
return fwrite(contents, size, nitems, file);
}
bool HttpRequest::Post(const char * url, const char * postdata, int postlength, BString &response, BString * authToken, bool addExpectJson)
{
CURLcode res;
bool result = false;
bool retry = false;
struct MemoryStruct chunk;
struct curl_slist *headers = NULL;
do
{
if (fCurlHandle)
{
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_easy_reset(fCurlHandle);
curl_easy_setopt(fCurlHandle, CURLOPT_URL, url);
if (postlength>0) {
curl_easy_setopt(fCurlHandle, CURLOPT_POSTFIELDSIZE, postlength);
curl_easy_setopt(fCurlHandle, CURLOPT_POSTFIELDS, postdata);
}
if (authToken->Length() > 0) {
curl_easy_setopt(fCurlHandle, CURLOPT_XOAUTH2_BEARER, authToken->String());
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
}
if (addExpectJson) {
headers = curl_slist_append(headers, "Expect:");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPHEADER, headers);
}
/* send all data to this function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEDATA, (void *)&chunk);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(fCurlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(fCurlHandle);
/* check for errors */
if(res == CURLE_OK) {
response = BString(chunk.memory, chunk.size);
curl_off_t wait = 0;
curl_easy_getinfo(fCurlHandle, CURLINFO_RETRY_AFTER, &wait);
if (wait>0)
{
retry = true;
sleep(wait);
}
result = true;
}
curl_slist_free_all(headers);
free(chunk.memory);
}
}
while (retry);
return result;
}
bool HttpRequest::Download(const char * url, const char * headerdata, const char * fullPath, BString * authToken)
{
FILE * file;
CURLcode res;
struct curl_slist *headers = NULL;
bool result = false;
file = fopen(fullPath, "w");
if (!file) {
return false;
}
if (fCurlHandle)
{
curl_easy_reset(fCurlHandle);
curl_easy_setopt(fCurlHandle, CURLOPT_URL, url);
curl_easy_setopt(fCurlHandle, CURLOPT_XOAUTH2_BEARER, authToken->String());
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
//all bearer requests send json
headers = curl_slist_append(headers, "Expect:");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, headerdata);
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPHEADER, headers);
/* send all data to this function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEFUNCTION, writeFileCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEDATA, (void *)file);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(fCurlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(fCurlHandle);
/* check for errors */
if(res == CURLE_OK) {
result = true;
}
fclose(file);
curl_slist_free_all(headers);
}
return result;
}
size_t readFileCallback(char * buffer, size_t size, size_t nitems, void *instream)
{
size_t bytes_read;
bytes_read = fread(buffer, 1, (size * nitems), (FILE *)instream);
return bytes_read;
}
size_t HttpRequest::_ReadFileCallbackLimit(char * buffer, size_t size, size_t nitems)
{
size_t bytes_read;
size_t to_read;
if (fReadLimitCallbackCount > fMaxChunkSize)
return 0;
to_read = size * nitems;
if (to_read > fMaxChunkSize)
{
to_read = fMaxChunkSize;
}
if ((to_read + fReadLimitCallbackCount) > fMaxChunkSize)
{
to_read = fMaxChunkSize - fReadLimitCallbackCount;
}
bytes_read = fread(buffer, 1, to_read, fReadFileCallbackFile);
fReadLimitCallbackCount += bytes_read;
return bytes_read;
}
bool HttpRequest::Upload(const char * url, const char * headerdata, const char * fullPath, BString &response, BString * authToken, off_t size)
{
FILE * file;
CURLcode res;
struct MemoryStruct chunk;
struct curl_slist *headers = NULL;
bool result = false;
bool retry = false;
do
{
file = fopen(fullPath, "rb");
if (!file) {
return false;
}
if (fCurlHandle)
{
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
curl_easy_reset(fCurlHandle);
curl_easy_setopt(fCurlHandle, CURLOPT_URL, url);
curl_easy_setopt(fCurlHandle, CURLOPT_XOAUTH2_BEARER, authToken->String());
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
//all bearer requests send json
headers = curl_slist_append(headers, "Expect:");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, headerdata);
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(fCurlHandle, CURLOPT_POST, 1L);
curl_easy_setopt(fCurlHandle, CURLOPT_READDATA, (void *)file);
curl_easy_setopt(fCurlHandle, CURLOPT_READFUNCTION, readFileCallback);
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEDATA, (void *)&chunk);
//curl_easy_setopt(fCurlHandle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) size);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(fCurlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(fCurlHandle);
/* check for errors */
if(res == CURLE_OK) {
response = BString(chunk.memory, chunk.size);
curl_off_t wait = 0;
curl_easy_getinfo(fCurlHandle, CURLINFO_RETRY_AFTER, &wait);
if (wait>0)
{
retry = true;
sleep(wait);
}
result = true;
}
fclose(file);
free(chunk.memory);
curl_slist_free_all(headers);
}
}
while (retry);
return result;
}
size_t HttpRequest::_CallMemberReadFileCallback(void * buffer, size_t sz, size_t n, void *f)
{
return static_cast<HttpRequest*>(f)->_ReadFileCallbackLimit((char *)buffer, sz, n);
}
bool HttpRequest::UploadChunked(const char * url, const char * headerdata, const char * fullPath, BString * authToken, size_t maxchunksize, off_t offset, BString & response)
{
CURLcode res;
struct MemoryStruct chunk;
struct curl_slist *headers = NULL;
fReadLimitCallbackCount = 0;
chunk.memory = (char*)malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
fMaxChunkSize = maxchunksize;
bool result = false;
fReadFileCallbackFile = fopen(fullPath, "rb");
if (!fReadFileCallbackFile) {
response = BString("Could not open file: ");
response << fullPath;
return false;
}
// bump file forward to offset
fseek(fReadFileCallbackFile, offset, SEEK_SET);
if (fCurlHandle)
{
curl_easy_reset(fCurlHandle);
curl_easy_setopt(fCurlHandle, CURLOPT_URL, url);
curl_easy_setopt(fCurlHandle, CURLOPT_XOAUTH2_BEARER, authToken->String());
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
//all bearer requests send json
headers = curl_slist_append(headers, "Expect:");
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, headerdata);
curl_easy_setopt(fCurlHandle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(fCurlHandle, CURLOPT_POST, 1L);
curl_easy_setopt(fCurlHandle, CURLOPT_READDATA, (void *)this);
curl_easy_setopt(fCurlHandle, CURLOPT_READFUNCTION, _CallMemberReadFileCallback);
/* send all data to this function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
/* we pass our 'chunk' struct to the callback function */
curl_easy_setopt(fCurlHandle, CURLOPT_WRITEDATA, (void *)&chunk);
//curl_easy_setopt(fCurlHandle, CURLOPT_INFILESIZE_LARGE, (curl_off_t) size);
/* some servers don't like requests that are made without a user-agent
field, so we provide one */
curl_easy_setopt(fCurlHandle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
/* get it! */
res = curl_easy_perform(fCurlHandle);
/* check for errors */
if(res == CURLE_OK) {
result = true;
response = BString(chunk.memory, chunk.size);
}
fclose(fReadFileCallbackFile);
free(chunk.memory);
curl_slist_free_all(headers);
}
return result;
}