forked from pt209223/librs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Http.cpp
491 lines (425 loc) · 15.3 KB
/
Http.cpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include "Http.h"
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
using std::cerr;
using std::endl;
using std::map;
static curl_slist *attach_common_headers(void) throw()
{
struct my_curl_list {
curl_slist *slist;
my_curl_list(void) : slist(NULL)
{
if (!(slist = curl_slist_append(slist, "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071028 PLD/3.0 (Th) BonEcho/2.0.0.8")))
throw EInternal("curl_slist_append() - fix it!");
if (!(slist = curl_slist_append(slist, "Keep-Alive: 300")))
throw EInternal("curl_slist_append() - fix it!");
if (!(slist = curl_slist_append(slist, "Connection: close")))
throw EInternal("curl_slist_append() - fix it!");
}
~my_curl_list(void)
{
curl_slist_free_all(slist);
}
};
static my_curl_list list;
return list.slist;
}
const char *HttpError::what(void) const throw()
{
switch (code) {
case HTTP_CONTINUE:
return "HTTP_CONTIMUE 100";
case HTTP_SWITCHING_PROTOCOLS:
return "HTTP_SWITCHING_PROTOCOLS 101";
case HTTP_PROCESSING:
return "HTTP_PROCESSING 102";
case HTTP_OK:
return "HTTP_OK 200";
case HTTP_CREATED:
return "HTTP_CREATED 201";
case HTTP_ACCEPTED:
return "HTTP_ACCEPTED 202";
case HTTP_NON_AUTHORITATIVE:
return "HTTP_NON_AUTHORITATIVE 203";
case HTTP_NO_CONTENT:
return "HTTP_NO_CONTENT 204";
case HTTP_RESET_CONTENT:
return "HTTP_RESET_CONTENT 205";
case HTTP_PARTIAL_CONTENT:
return "HTTP_PARTIAL_CONTENT 206";
case HTTP_MULTI_STATUS:
return "HTTP_MULTI_STATUS 207";
case HTTP_MULTIPLE_CHOICES:
return "HTTP_MULTIPLE_CHOICES 300";
case HTTP_MOVED_PERMANENTLY:
return "HTTP_MOVED_PERMANENTLY 301";
case HTTP_MOVED_TEMPORARILY:
return "HTTP_MOVED_TEMPORARILY 302";
case HTTP_SEE_OTHER:
return "HTTP_SEE_OTHER 303";
case HTTP_NOT_MODIFIED:
return "HTTP_NOT_MODIFIED 304";
case HTTP_USE_PROXY:
return "HTTP_USE_PROXY 305";
case HTTP_TEMPORARY_REDIRECT:
return "HTTP_TEMPORARY_REDIRECT 307";
case HTTP_BAD_REQUEST:
return "HTTP_BAD_REQUEST 400";
case HTTP_UNAUTHORIZED:
return "HTTP_UNAUTHORIZED 401";
case HTTP_PAYMENT_REQUIRED:
return "HTTP_PAYMENT_REQUIRED 402";
case HTTP_FORBIDDEN:
return "HTTP_FORBIDDEN 403";
case HTTP_NOT_FOUND:
return "HTTP_NOT_FOUND 404";
case HTTP_METHOD_NOT_ALLOWED:
return "HTTP_METHOD_NOT_ALLOWED 405";
case HTTP_NOT_ACCEPTABLE:
return "HTTP_NOT_ACCEPTABLE 406";
case HTTP_PROXY_AUTHENTICATION_REQUIRED:
return "HTTP_PROXY_AUTHENTICATION_REQUIRED 407";
case HTTP_REQUEST_TIME_OUT:
return "HTTP_REQUEST_TIME_OUT 408";
case HTTP_CONFLICT:
return "HTTP_CONFLICT 409";
case HTTP_GONE:
return "HTTP_GONE 410";
case HTTP_LENGTH_REQUIRED:
return "HTTP_LENGTH_REQUIRED 411";
case HTTP_PRECONDITION_FAILED:
return "HTTP_PRECONDITION_FAILED 412";
case HTTP_REQUEST_ENTITY_TOO_LARGE:
return "HTTP_REQUEST_ENTITY_TOO_LARGE 413";
case HTTP_REQUEST_URI_TOO_LARGE:
return "HTTP_REQUEST_URI_TOO_LARGE 414";
case HTTP_UNSUPPORTED_MEDIA_TYPE:
return "HTTP_UNSUPPORTED_MEDIA_TYPE 415";
case HTTP_RANGE_NOT_SATISFIABLE:
return "HTTP_RANGE_NOT_SATISFIABLE 416";
case HTTP_EXPECTATION_FAILED:
return "HTTP_EXPECTATION_FAILED 417";
case HTTP_UNPROCESSABLE_ENTITY:
return "HTTP_UNPROCESSABLE_ENTITY 422";
case HTTP_LOCKED:
return "HTTP_LOCKED 423";
case HTTP_FAILED_DEPENDENCY:
return "HTTP_FAILED_DEPENDENCY 424";
case HTTP_UPGRADE_REQUIRED:
return "HTTP_UPGRADE_REQUIRED 426";
case HTTP_INTERNAL_SERVER_ERROR:
return "HTTP_INTERNAL_SERVER_ERROR 500";
case HTTP_NOT_IMPLEMENTED:
return "HTTP_NOT_IMPLEMENTED 501";
case HTTP_BAD_GATEWAY:
return "HTTP_BAD_GATEWAY 502";
case HTTP_SERVICE_UNAVAILABLE:
return "HTTP_SERVICE_UNAVAILABLE 503";
case HTTP_GATEWAY_TIME_OUT:
return "HTTP_GATEWAY_TIME_OUT 504";
case HTTP_VERSION_NOT_SUPPORTED:
return "HTTP_VERSION_NOT_SUPPORTED 505";
case HTTP_VARIANT_ALSO_VARIES:
return "HTTP_VARIANT_ALSO_VARIES 506";
case HTTP_INSUFFICIENT_STORAGE:
return "HTTP_INSUFFICIENT_STORAGE 507";
case HTTP_NOT_EXTENDED:
return "HTTP_NOT_EXTENDED 510";
default:
return "unrecognized http code";
}
}
const char *CurlError::what(void) const throw()
{
return curl_easy_strerror(code);
}
Http::Http(void) throw()
: curl(NULL),
conn_tout_(default_conn_timeout_in_msec),
rdwr_tout_(default_rdwr_timeout_in_msec),
header_(NULL), header_len_(0), header_max_(0),
verbose_(false)
{
if (!(curl = curl_easy_init()))
throw EInternal("nie mozna zaalokowac pamieci dla CURLa");
}
Http::~Http(void) throw()
{
if (curl) curl_easy_cleanup(curl);
if (header_) delete[] header_;
}
void Http::clear(void)
{
if (curl) curl_easy_reset(curl);
if (header_) {
delete[] header_;
header_ = NULL;
header_len_ = 0;
header_max_ = 0;
}
headers_map_.clear();
}
size_t Http::head_fn(void *buf, size_t sz, size_t nmemb, void *arg)
{
size_t size = sz * nmemb;
Http *h = (Http*)arg;
if (!h->header_max_) {
size_t max = (size+1) > h->header_def_len ? (size+1):h->header_def_len;
if (!(h->header_ = new(std::nothrow) char[max])) return CURLE_WRITE_ERROR;
h->header_max_ = max;
} else if (h->header_len_ + size + 1 >= h->header_max_) {
size_t max = (h->header_len_ + size + 1) > (2*h->header_max_) ?
(h->header_len_ + size + 1) : (2*h->header_max_);
char *ptr = new(std::nothrow) char[max];
if (!ptr) return CURLE_WRITE_ERROR;
strncpy(ptr, h->header_, h->header_len_+1);
assert(h->header_[h->header_len_]);
h->header_max_ = max;
}
assert(h->header_len_ + size + 1 <= h->header_max_);
if (size) memcpy(h->header_+h->header_len_, (char*)buf, size);
h->header_len_ += size;
h->header_[h->header_len_] = 0;
return size;
}
struct page_task {
Http *http;
char **page;
size_t &len;
size_t real;
Http::callback_t fn;
void *arg;
page_task(Http *http, char **page, size_t &len, Http::callback_t fn, void *arg)
: http(http), page(page), len(len), real(0), fn(fn), arg(arg)
{
(*page) = NULL; len = 0;
}
~page_task(void)
{
if (page) delete[] (*page);
}
};
size_t Http::page_fn(void *buf, size_t sz, size_t nmemb, void *arg)
{
size_t size = sz*nmemb;
page_task *task = (page_task*)arg;
if (!task->real) {
size_t max = page_def_len > (size+1) ? page_def_len:(size+1);
if (!((*task->page) = new(std::nothrow) char[max])) return CURLE_WRITE_ERROR;
task->real = max;
} else if (task->len + size + 1 > task->real) {
size_t dif = task->len + size + 1,
max = (2*task->real < dif) ? dif : (2*task->real);
char *ptr = new(std::nothrow) char[max];
if (!ptr) return CURLE_WRITE_ERROR;
memcpy(ptr, *task->page, task->len);
delete[] (*task->page);
(*task->page) = ptr;
task->real = max;
}
memcpy((*task->page) + task->len, buf, size);
task->len += size;
(*task->page)[task->len] = 0;
if (task->fn && !task->fn((const char*)buf, size, task->arg))
return CURLE_WRITE_ERROR;
return size;
}
int Http::get(char *&page, size_t &len, const char *url,
const char *post, const char *cookies,
Http::callback_t fn, void *arg) throw(Exception)
{
clear();
page_task task(this, &page, len, fn, arg);
if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK)
throw EExternal("nie mozna ustawic urla: '%s'", url);
if (post && curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post) != CURLE_OK)
throw EExternal("nie mozna ustawic POSTa: '%s'", post);
if (cookies && curl_easy_setopt(curl, CURLOPT_COOKIE, cookies) != CURLE_OK)
throw EExternal("nie mozna ustawic ciastek: '%s'", cookies);
if (rdwr_tout_ > 0 && curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, rdwr_tout_) != CURLE_OK)
throw EExternal("nie mozna ustawic timeoutu na odczyt/zapis: '%d'", rdwr_tout_);
if (conn_tout_ > 0 && curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, conn_tout_) != CURLE_OK)
throw EExternal("nie mozna ustawic timeoutu na polaczenie: '%d'", conn_tout_);
if (curl_easy_setopt(curl, CURLOPT_HTTPHEADER, attach_common_headers()) != CURLE_OK)
throw EExternal("nie mozna ustawic dodatkowych naglowkow");
if (verbose_ && curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) != CURLE_OK)
throw EInternal("nie mozna ustawic trybu glosnego dla CURLa");
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, page_fn) != CURLE_OK)
throw EInternal("nie mozna ustawic funkcji do odbioru danych");
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, &task) != CURLE_OK)
throw EInternal("nie mozna ustawic argumentu dla funkcji do odbioru danych");
if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, head_fn) != CURLE_OK)
throw EInternal("nie mozna ustawic funkcji do odbioru naglowkow");
if (curl_easy_setopt(curl, CURLOPT_HEADERDATA, this) != CURLE_OK)
throw EInternal("nie mozna ustawic argumentu dla funkcji do odbioru naglowkow");
CURLcode cd = curl_easy_perform(curl);
if (cd != CURLE_OK) {
if (cd == CURLE_OPERATION_TIMEOUTED)
throw EOperationTimeout();
if (cd == CURLE_COULDNT_CONNECT)
throw ECouldntConnect();
if (cd == CURLE_COULDNT_RESOLVE_HOST)
throw ECouldntResolveHost();
throw CurlError(cd);
}
int code = do_analyze();
task.page = NULL; // zeby ~page_task() nie zwolnil pamieci, ktora zwracamy
return code;
}
struct file_task {
Http *http;
const char *path;
int fd;
off_t &len;
Http::callback_t fn;
void *data;
file_task(Http *http, const char *path, off_t &len, Http::callback_t fn, void *data)
: http(http), path(path), fd(-1), len(len), fn(fn), data(data)
{
if ((fd = open(path, O_CREAT|O_RDWR|O_TRUNC, 0644)) < 0)
throw EExternal("nie mozna otworzyc pliku '%s': %d, %s",
path, errno, strerror(errno));
len = 0;
}
~file_task(void) throw()
{
if (fd >= 0 && close(fd))
throw EInternal("nie mozna zamknac pliku '%s': %d, %s",
path, errno, strerror(errno));
}
};
size_t Http::file_fn(void *buf, size_t sz, size_t nmemb, void *data) {
size_t size = sz*nmemb;
file_task *task = (file_task*)data;
int fd = task->fd;
size_t done = 0;
while (done < size) {
int wr = write(fd, (char*)buf+done, size-done);
if (wr < 0) {
if (errno == EINTR || errno == EAGAIN) continue;
return CURLE_WRITE_ERROR;
}
done += wr;
}
if (task->fn && !task->fn((const char*)buf, size, task->data))
return CURLE_WRITE_ERROR;
task->len += size;
return size;
}
int Http::get(const char *path, off_t &len, const char *url,
const char *post, const char *cookies,
Http::callback_t fn, void *arg) throw(Exception)
{
clear();
file_task task(this, path, len, fn, arg);
if (curl_easy_setopt(curl, CURLOPT_URL, url) != CURLE_OK)
throw EExternal("nie mozna ustawic urla: '%s'", url);
if (post && curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post) != CURLE_OK)
throw EExternal("nie mozna ustawic POSTa: '%s'", post);
if (cookies && curl_easy_setopt(curl, CURLOPT_COOKIE, cookies) != CURLE_OK)
throw EExternal("nie mozna ustawic ciastek: '%s'", cookies);
if (rdwr_tout_ > 0 && curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, rdwr_tout_) != CURLE_OK)
throw EExternal("nie mozna ustawic timeoutu na odczyt/zapis: '%d'", rdwr_tout_);
if (conn_tout_ > 0 && curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, conn_tout_) != CURLE_OK)
throw EExternal("nie mozna ustawic timeoutu na polaczenie: '%d'", conn_tout_);
if (curl_easy_setopt(curl, CURLOPT_HTTPHEADER, attach_common_headers()) != CURLE_OK)
throw EExternal("nie mozna ustawic dodatkowych naglowkow");
if (verbose_ && curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) != CURLE_OK)
throw EInternal("nie mozna ustawic trybu glosnego dla CURLa");
if (curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, file_fn) != CURLE_OK)
throw EInternal("nie mozna ustawic funkcji do odbioru danych");
if (curl_easy_setopt(curl, CURLOPT_WRITEDATA, &task) != CURLE_OK)
throw EInternal("nie mozna ustawic argumentu dla funkcji do odbioru danych");
if (curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, head_fn) != CURLE_OK)
throw EInternal("nie mozna ustawic funkcji do odbioru naglowkow");
if (curl_easy_setopt(curl, CURLOPT_HEADERDATA, this) != CURLE_OK)
throw EInternal("nie mozna ustawic argumentu dla funkcji do odbioru naglowkow");
CURLcode cd = curl_easy_perform(curl);
if (cd != CURLE_OK) {
if (cd == CURLE_OPERATION_TIMEOUTED)
throw EOperationTimeout();
if (cd == CURLE_COULDNT_CONNECT)
throw ECouldntConnect();
if (cd == CURLE_COULDNT_RESOLVE_HOST)
throw ECouldntResolveHost();
throw CurlError(cd);
}
return do_analyze();
}
int Http::do_analyze(void)
{
if (header_len_ < 10)
throw EInternal("zbyt krotki naglowek?");
char *ptr = header_, *eol;
headers_map_.clear();
int code = 0;
while ((eol = strstr(ptr, "\r\n"))) {
if (eol == ptr) break;
if (strncasecmp(ptr, "HTTP/", 5) == 0) {
ptr += 5;
while (ptr != eol && *ptr != ' ' && *ptr != '\t') ++ptr;
if (ptr == eol) throw EInternal("nie mozna odczytac naglowka 'HTTP/<wersja> <kod>'?");
while (ptr != eol && (*ptr == ' ' || *ptr == '\t')) ++ptr;
code = strtol(ptr, NULL, 10);
if (code < 100 || code >= 600) throw EInternal("odczytano nieprawidlowy numer kodu: %d", code);
} else {
char *to_change = NULL;
std::pair<const char*, const char*> ent;
ent.first = ptr;
for (size_t i = 0; ptr[0] != ':'; ++i, ++ptr) {
if (ptr == eol) throw EInternal("nie mozna odczytac naglowka?");
if (ptr[0] >= 'A' && ptr[0] <= 'Z') ptr[0] -= ('A'-'a');
}
to_change = ptr;
ptr[0] = 0; // oznaczamy koniec klucza
++ptr;
while (ptr != eol && (*ptr == ' ' || *ptr == '\t')) ++ptr;
ent.second = ptr;
if (strcmp("set-cookie", ent.first)) {
eol[0] = 0; // oznaczamy koniec wartosci
headers_map_[ent.first] = ent.second;
to_change[0] = ':';
eol[0] = '\r';
} else {
// Dla cookies robimy dodatkowo tak:
while (ptr[0] != ';')
if (ptr[0]) ++ptr;
else throw EInternal("nie mozna odczytac ciastka?");
ptr[1] = 0;
Headers::iterator it = headers_map_.find(ent.first);
if (it == headers_map_.end())
headers_map_[ent.first] = ent.second;
else
it->second = it->second + " " + ent.second;
to_change[0] = ':';
ptr[1] = ';';
}
}
ptr = eol + 2; // przewijamy na nastepny wpis
}
//for (Headers::const_iterator it = headers_map_.begin(); it != headers_map_.end(); ++it)
// cerr << "<> " << it->first << " => " << it->second << endl;
if (!code) throw EInternal("brakujaca informacja o kodzie http?");
if (code >= 400) throw HttpError(code);
return code;
}
const char *Http::get_recv_header(const char *key) const
{
Headers::const_iterator it;
if ((it = headers_map_.find(key)) == headers_map_.end())
return NULL;
return it->second.c_str();
}
const char *Http::get_recv_cookies(void) const
{
return get_recv_header("set-cookie");
}
const char *Http::get_recv_redirect(void) const
{
return get_recv_header("location");
}