forked from jiiksteri/yt_history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.c
557 lines (454 loc) · 12.4 KB
/
https.c
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* So libevent 2.1 would ship a sane api for running evhttp_requests on
* top of SSL sockets.
*
* Sadly, that's alpha. 2.0, which is stable, doesn't ship such goodies.
* Fortunately libevent bufferevents still make things like this a
* breeze.
*/
#include "https.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include "verbose.h"
#include "conn_stash.h"
struct https_engine {
struct conn_stash *conn_stash;
struct event_base *event_base;
};
int https_engine_init(struct https_engine **httpsp, struct event_base *event_base,
int no_keepalive)
{
int err;
struct https_engine *https = malloc(sizeof(*https));
if (https == NULL) {
return errno;
}
memset(https, 0, sizeof(*https));
err = conn_stash_init(&https->conn_stash, event_base, no_keepalive);
if (err != 0) {
free(https);
return errno;
}
https->event_base = event_base;
*httpsp = https;
return 0;
}
void https_engine_destroy(struct https_engine *https)
{
conn_stash_destroy(https->conn_stash);
free(https);
}
struct request_ctx {
const char *host;
int port;
const char *method;
const char *path;
const char *access_token;
char *request_body;
size_t request_body_length;
struct https_cb_ops *cb_ops;
void *cb_arg;
char *error;
enum { READ_NONE, READ_STATUS, READ_HEADERS, READ_BODY, READ_DONE } read_state;
int status;
char *status_line;
int content_length;
int consumed;
int chunked;
ssize_t chunk_size;
size_t chunk_left;
struct conn_stash *conn_stash;
};
static void cb_write(struct bufferevent *bev, void *arg)
{
verbose(FIREHOSE, "%s(): Write exhausted\n", __func__);
bufferevent_disable(bev, EV_WRITE);
bufferevent_enable(bev, EV_READ);
}
static char *read_line(struct bufferevent *bev, size_t *n)
{
return evbuffer_readln(bufferevent_get_input(bev),
n, EVBUFFER_EOL_CRLF_STRICT);
}
static void parse_status(struct request_ctx *req, const char *line, size_t len)
{
int i;
req->status_line = strdup(line);
for (i = 0; line[i] != ' ' && i < len; i++) {
;
}
if (i == len) {
verbose(ERROR, "%s(): Invalid status line '%s'\n", __func__, line);
} else {
req->status = atoi(&line[i+1]);
}
}
static int read_chunk_size(struct request_ctx *req, struct bufferevent *bev)
{
char *line;
char *eptr;
size_t n;
unsigned long val;
int err;
/* A chunk ends with CRLF that's not accounted for by chunk size.
* So we should have an "empty" line followed by next chunk size
*/
line = NULL;
do {
free(line);
line = read_line(bev, &n);
} while (line != NULL && *line == '\0');
err = 0;
if (line != NULL) {
errno = 0;
val = strtoul(line, &eptr, 16);
if (errno != 0) {
err = errno;
verbose(ERROR, "%s(): bad chunk size '%s': %s\n",
__func__, line, strerror(errno));
} else {
req->chunk_size = val;
req->chunk_left = val;
verbose(FIREHOSE,
"%s(): chunk size: %zd, from '%s'\n",
__func__, val, line);
err = 0;
}
free(line);
}
return err;
}
static const char *pretty_state(char *buf, size_t len, int state)
{
switch (state) {
case READ_NONE: snprintf(buf, len, "READ_NONE"); break;
case READ_STATUS: snprintf(buf, len, "READ_STATUS"); break;
case READ_HEADERS: snprintf(buf, len, "READ_HEADERS"); break;
case READ_BODY: snprintf(buf, len, "READ_BODY"); break;
case READ_DONE: snprintf(buf, len, "READ_DONE"); break;
default: snprintf(buf, len, "UNKNWN(%d)", state); break;
}
return buf;
}
static void set_read_state(struct request_ctx *req, int state)
{
char s1[32];
char s2[32];
if (verbose_adjust_level(0) >= VERBOSE) {
verbose(VERBOSE, "%s(): %s -> %s\n", __func__,
pretty_state(s1, sizeof(s1), req->read_state),
pretty_state(s2, sizeof(s2), state));
}
req->read_state = state;
}
static void flush_input(struct request_ctx *req, struct evbuffer *buf)
{
int len;
while ((len = evbuffer_get_length(buf)) > 0) {
verbose(VERBOSE, "%s(): input bytes left: %d\n",
__func__, len);
req->cb_ops->read(buf, req->cb_arg);
}
}
static void request_done(struct request_ctx *req, struct bufferevent *bev)
{
/* Force the remaining bytes down our consumer's throat. */
flush_input(req, bufferevent_get_input(bev));
req->cb_ops->done(req->error, req->cb_arg);
conn_stash_put_bev(req->conn_stash, bev);
free(req->status_line);
free(req->request_body);
free(req);
}
static void drain_body(struct request_ctx *req, struct bufferevent *bev)
{
struct evbuffer *buf;
struct evbuffer *saved;
int before, after;
if (req->chunked && req->chunk_size == -1) {
/* Transfer-Encoding: chunked but we don't have size yet. */
if (read_chunk_size(req, bev) != 0) {
verbose(ERROR, "%s(): could not read chunk size!\n", __func__);
}
}
buf = bufferevent_get_input(bev);
saved = NULL;
before = evbuffer_get_length(buf);
if (req->chunked && req->chunk_size > 0 && req->chunk_left < before) {
/*
* Save the real buffer away and give the callback
* a temporary one, just the chunk that remains.
*/
saved = buf;
buf = evbuffer_new();
evbuffer_remove_buffer(saved, buf, req->chunk_left);
before = evbuffer_get_length(buf);
}
req->cb_ops->read(buf, req->cb_arg);
after = evbuffer_get_length(buf);
req->consumed += before - after;
req->chunk_left -= (before - after);
if (req->chunk_size > 0 && req->chunk_left == 0) {
/* We've consumed the whole chunk.
* Signal that we need another one.
*/
req->chunk_size = -1;
}
if (saved != NULL) {
/* saved is actually the buffer in bev */
evbuffer_prepend_buffer(saved, buf);
evbuffer_free(buf);
}
if ((req->chunked && req->chunk_size == 0) ||
(req->content_length > 0 && req->consumed == req->content_length)) {
set_read_state(req, READ_DONE);
}
}
static void header_keyval(char **key, char **val, char *line)
{
int i;
*key = line;
for (i = 0; line[i] != '\0' && line[i] != ':'; i++) {
;
}
if (line[i] == ':') {
line[i] = '\0';
while (line[++i] == ' ') {
;
}
}
*val = &line[i];
}
static void handle_header(struct request_ctx *req, const char *key, const char *val)
{
if (strcmp(key, "Content-Length") == 0) {
req->content_length = atoi(val);
} else if (strcmp(key, "Transfer-Encoding") == 0 &&
strcmp(val, "chunked") == 0) {
req->chunked = 1;
req->chunk_size = -1;
req->chunk_left = -1;
}
if (req->cb_ops->response_header) {
req->cb_ops->response_header(key, val, req->cb_arg);
}
}
static void cb_read(struct bufferevent *bev, void *arg)
{
struct request_ctx *req = arg;
char *line;
size_t n;
if (req->read_state == READ_NONE) {
req->read_state = READ_STATUS;
}
while (req->read_state == READ_STATUS || req->read_state == READ_HEADERS) {
line = read_line(bev, &n);
if (line == NULL) {
return;
}
if (req->read_state == READ_STATUS) {
verbose(VERBOSE, "%s(): status line: '%s'\n", __func__, line);
parse_status(req, line, n);
set_read_state(req, READ_HEADERS);
} else {
while (line && req->read_state == READ_HEADERS) {
if (*line == '\0') {
set_read_state(req, READ_BODY);
} else {
char *key, *val;
verbose(VERBOSE, "%s(): header line '%s'\n", __func__, line);
header_keyval(&key, &val, line);
handle_header(req, key, val);
free(line);
line = read_line(bev, &n);
}
}
}
free(line);
}
while (req->read_state == READ_BODY && evbuffer_get_length(bufferevent_get_input(bev)) > 0) {
drain_body(req, bev);
}
if (req->read_state == READ_DONE) {
request_done(req, bev);
}
}
static __attribute__((format(printf,2,3))) void store_request_error(struct request_ctx *req,
const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
req->error = strdup(buf);
}
static void submit_request(struct bufferevent *bev, struct request_ctx *req)
{
evbuffer_add_printf(bufferevent_get_output(bev),
"%s %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: %s\r\n",
req->method,
req->path,
req->host,
conn_stash_is_keepalive(req->conn_stash)
? "Keep-Alive"
: "close");
if (req->access_token != NULL) {
evbuffer_add_printf(bufferevent_get_output(bev),
"Authorization: Bearer %s\r\n",
req->access_token);
}
if (strcmp(req->method, "POST") == 0) {
evbuffer_add_printf(bufferevent_get_output(bev),
"Content-Type: application/x-www-form-urlencoded\r\n");
}
if (req->request_body != NULL) {
evbuffer_add_printf(bufferevent_get_output(bev),
"Content-Length: %zd\r\n",
req->request_body_length);
}
evbuffer_add_printf(bufferevent_get_output(bev), "\r\n");
if (req->request_body != NULL) {
evbuffer_add_reference(bufferevent_get_output(bev),
req->request_body,
req->request_body_length,
(evbuffer_ref_cleanup_cb)NULL,
NULL);
}
bufferevent_enable(bev, EV_READ|EV_WRITE);
}
static void reset_read_state(struct request_ctx *req)
{
req->read_state = READ_NONE;
req->chunked = 0;
req->chunk_size = 0;
req->chunk_left = 0;
req->content_length = 0;
}
static void restart_request(struct request_ctx *req, struct bufferevent *bev);
static void do_store_request_error(struct request_ctx *req, int sock_err)
{
if (req->status != 0) {
store_request_error(req, "%s hates me: %s",
req->host, req->status_line);
} else {
store_request_error(req, "Socket error. errno %d (%s)",
sock_err, evutil_socket_error_to_string(sock_err));
}
}
static void cb_event(struct bufferevent *bev, short what, void *arg)
{
struct request_ctx *req = arg;
int sock_err;
switch (what & ~(BEV_EVENT_READING|BEV_EVENT_WRITING)) {
case BEV_EVENT_CONNECTED:
break;
case BEV_EVENT_ERROR:
sock_err = EVUTIL_SOCKET_ERROR();
verbose(ERROR,
"%s(): last socket error is %d (%s)\n",
__func__, sock_err,
evutil_socket_error_to_string(sock_err));
if (req->read_state == READ_NONE) {
verbose(NORMAL,
"%s(): error reported before nothing read."
" Restarting request\n", __func__);
restart_request(req, bev);
return;
} else if (req->status != 200) {
/* This needs better heuristics. We just
* handle the case of "error when we have
* read response" differently :(
*
* We should at least be able to determine
* that we've consumed the whole response.
*/
do_store_request_error(req, sock_err);
}
request_done(req, bev);
break;
case BEV_EVENT_EOF:
request_done(req, bev);
break;
default:
verbose(ERROR, "%s(): Unhandled event: %d\n", __func__, what);
break;
}
}
static void clear_buffer(struct evbuffer *buf)
{
evbuffer_drain(buf, evbuffer_get_length(buf));
}
static void restart_request(struct request_ctx *req, struct bufferevent *bev)
{
int err;
bufferevent_disable(bev, EV_READ|EV_WRITE);
err = conn_stash_reconnect(req->conn_stash, &bev);
if (err == 0) {
/* This is rather fragile when someone decides
* to add bits into struct request_ctx. We'll need
* to know what to clear. So it could use a bit of
* restructuring
*/
clear_buffer(bufferevent_get_output(bev));
clear_buffer(bufferevent_get_input(bev));
reset_read_state(req);
bufferevent_setcb(bev, cb_read, cb_write, cb_event, req);
submit_request(bev, req);
} else {
store_request_error(req, "%s(): %s", __func__, strerror(err));
request_done(req, bev);
}
}
static int setup_request_body(struct request_ctx *req, struct evbuffer *body)
{
size_t len;
if (body != NULL) {
len = evbuffer_get_length(body);
req->request_body = malloc(len);
if (req->request_body == NULL) {
return ENOMEM;
}
evbuffer_copyout(body, req->request_body, len);
req->request_body_length = len;
}
return 0;
}
void https_request(struct https_engine *https,
const char *host, int port,
const char *method, const char *path,
const char *access_token,
struct evbuffer *body,
struct https_cb_ops *cb_ops,
void *cb_arg)
{
struct request_ctx *request;
struct bufferevent *bev;
if ((request = malloc(sizeof(*request))) == NULL) {
cb_ops->done("Out of memory", cb_arg);
return;
}
memset(request, 0, sizeof(*request));
request->method = method;
request->host = host;
request->port = port;
request->path = path;
request->access_token = access_token;
setup_request_body(request, body);
request->cb_ops = cb_ops;
request->cb_arg = cb_arg;
request->conn_stash = https->conn_stash;
bev = conn_stash_get_bev(https->conn_stash, host, port);
if (bev == NULL) {
cb_ops->done("Failed to set up connection", cb_arg);
free(request);
return;
}
bufferevent_setcb(bev, cb_read, cb_write, cb_event, request);
submit_request(bev, request);
}