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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
|
#include "uvh.h"
#include "sds.h"
#include "http_parser.h"
#include <sys/queue.h>
#ifndef offsetof
#ifdef __GNUC__
#define offsetof(type, member) __builtin_offsetof (type, member)
#endif
#endif
#ifndef container_of
#ifdef __GNUC__
#define member_type(type, member) __typeof__ (((type *)0)->member)
#else
#define member_type(type, member) const void
#endif
#define container_of(ptr, type, member) ((type *)( \
(char *)(member_type(type, member) *){ ptr } - offsetof(type, member)))
#endif
#define LOG(LEVEL, FMT, args...) \
fprintf(stderr, FMT "\n" , ##args)
#define LOG_DEBUG(FMT, args...) LOG(0, "DEBUG " FMT , ##args)
#define LOG_WARNING(FMT, args...) LOG(0, "WARNING " FMT , ##args)
#define LOG_ERROR(FMT, args...) LOG(0, "ERROR " FMT , ##args)
#define LOG_INFO(FMT, args...) LOG(0, "INFO " FMT , ##args)
struct http_status_code_def
{
int code;
const char *str;
};
static struct http_status_code_def http_status_code_defs[] =
{
#define XX(CODE, NAME, STR) { CODE, STR },
HTTP_STATUS_CODE_MAP(XX)
#undef XX
{ -1, NULL }
};
static void on_connection(uv_stream_t *stream, int status);
static uv_buf_t alloc_cb(uv_handle_t *, size_t size);
static void read_cb(uv_stream_t *stream, ssize_t nread, uv_buf_t buf);
static void close_cb(uv_handle_t *handle);
static int on_message_begin(http_parser *parser);
static int on_url(http_parser *parser, const char *at, size_t len);
// static int on_status(http_parser *parser, const char *at, size_t len);
static int on_header_field(http_parser *parser, const char *at,
size_t len);
static int on_header_value(http_parser *parser, const char *at,
size_t len);
static int on_headers_complete(http_parser *parser);
static int on_body(http_parser *parser, const char *at, size_t len);
static int on_message_complete(http_parser *parser);
static void uvh_request_write_chunk(struct uvh_request *req, sds chunk);
static struct http_parser_settings parser_settings =
{
&on_message_begin,
&on_url,
NULL,
&on_header_field,
&on_header_value,
&on_headers_complete,
&on_body,
&on_message_complete
};
struct uvh_request_private;
struct uvh_connection;
struct uvh_server
{
uv_tcp_t stream;
struct sockaddr_storage addr;
socklen_t addr_len;
uv_loop_t *loop;
char stop;
uvh_request_handler_cb request_handler;
void *userdata;
SLIST_HEAD(connection_list, uvh_connection) connections;
};
struct uvh_connection
{
uv_tcp_t stream;
struct uvh_server *server;
struct http_parser parser;
SLIST_ENTRY(uvh_connection) siblings;
SLIST_HEAD(request_list, uvh_request_private) requests;
};
struct uvh_request_private
{
struct uvh_request req;
struct uvh_connection *connection;
char keepalive;
int send_status;
sds send_headers;
sds send_body;
int streaming;
uvh_stream_cb stream_cb;
void *stream_userdata;
SLIST_ENTRY(uvh_request_private) siblings;
};
static struct uvh_request_private *uvh_request_new(struct uvh_connection *conn);
static void uvh_request_free(struct uvh_request_private *req);
static struct uvh_connection *uvh_connection_new(struct uvh_server *server);
static void uvh_connection_free(struct uvh_connection *conn);
UVH_EXTERN struct uvh_server *uvh_server_init(uv_loop_t *loop, void *data,
uvh_request_handler_cb request_handler)
{
struct uvh_server *server;
int rc;
server = calloc(1, sizeof(*server));
if (!server)
goto error;
server->loop = loop;
server->userdata = data;
server->request_handler = request_handler;
SLIST_INIT(&server->connections);
rc = uv_tcp_init(loop, &server->stream);
if (rc < 0)
goto error;
server->stream.data = server;
return server;
error:
if (server)
{
free(server);
}
return NULL;
}
UVH_EXTERN void uvh_server_free(struct uvh_server *server)
{
free(server);
}
UVH_EXTERN int uvh_server_listen(struct uvh_server *server, const char *address,
short port)
{
struct sockaddr_in addr = uv_ip4_addr(address, port);
memcpy(&server->addr, &addr, sizeof(addr));
server->addr_len = sizeof(addr);
uv_tcp_bind(&server->stream, addr);
int r = uv_listen((uv_stream_t *) &server->stream, 128,
on_connection);
if (r)
return r;
return 0;
}
static void on_server_close(uv_handle_t *handle)
{
struct uvh_server *server = (struct uvh_server *) handle;
struct uvh_connection *conn;
LOG_DEBUG("%s", __FUNCTION__);
SLIST_FOREACH(conn, &server->connections, siblings)
{
uv_close((uv_handle_t *) &conn->stream, &close_cb);
}
}
UVH_EXTERN void uvh_server_stop(struct uvh_server *server)
{
server->stop = 1;
uv_close((uv_handle_t *) &server->stream, &on_server_close);
}
static void on_connection(uv_stream_t *stream, int status)
{
struct uvh_server *server = (struct uvh_server *) stream;
struct uvh_connection *connection;
uv_tcp_t *client;
LOG_DEBUG("%s", __FUNCTION__);
if (status == -1)
{
LOG_WARNING("on_connection: status = -1");
return;
}
if (server->stop)
{
LOG_WARNING("on_connection: stop bit set");
goto close_conn;
}
connection = uvh_connection_new(server);
if (!connection)
goto close_conn;
SLIST_INSERT_HEAD(&server->connections, connection, siblings);
if (uv_accept(stream, (uv_stream_t *) &connection->stream) == 0)
{
LOG_DEBUG("starting read on connection %p", connection);
uv_read_start((uv_stream_t *) &connection->stream, alloc_cb, read_cb);
}
else
{
// TODO: free the connection
uv_close((uv_handle_t *) &connection->stream, NULL);
LOG_WARNING("failed to accept");
}
return;
close_conn:
client = calloc(1, sizeof(*client));
uv_tcp_init(server->loop, client);
uv_accept(stream, (uv_stream_t *) client);
// TODO: free the client
uv_close((uv_handle_t *) client, NULL);
}
static uv_buf_t alloc_cb(uv_handle_t *handle, size_t size)
{
(void) handle;
return uv_buf_init(calloc(1, size), size);
}
static void read_cb(uv_stream_t *stream, ssize_t nread, uv_buf_t buf)
{
struct uvh_connection *connection = (struct uvh_connection *) stream;
size_t nparsed;
LOG_DEBUG("read_cb: nread: %d, buf.len: %d", (int)nread, (int)buf.len);
if (nread == 0)
goto out;
if (nread < 0)
{
uv_err_t err = uv_last_error(stream->loop);
if (err.code == UV_EOF)
{
LOG_DEBUG("EOF");
nread = 0;
}
else
{
uv_close((uv_handle_t *) stream, &close_cb);
goto out;
}
}
nparsed = http_parser_execute(&connection->parser,
&parser_settings,
buf.base, nread);
LOG_DEBUG("nparsed:%d", (int) nparsed);
if ((ssize_t) nparsed != nread)
{
LOG_ERROR("http parse error, closing connection");
uv_close((uv_handle_t *) stream, &close_cb);
}
else if (nread == 0)
{
uv_close((uv_handle_t *) stream, &close_cb);
}
out:
free(buf.base);
}
static void close_cb(uv_handle_t *handle)
{
struct uvh_connection *connection = (struct uvh_connection *) handle;
struct uvh_server *server = connection->server;
LOG_DEBUG("%s", __FUNCTION__);
SLIST_REMOVE(&server->connections, connection, uvh_connection, siblings);
uvh_connection_free(connection);
}
static int on_message_begin(http_parser *parser)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req;
LOG_DEBUG("%s", __FUNCTION__);
req = uvh_request_new(connection);
SLIST_INSERT_HEAD(&connection->requests, req, siblings);
return 0;
}
static int on_url(http_parser *parser, const char *at, size_t len)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req = SLIST_FIRST(&connection->requests);
LOG_DEBUG("%s", __FUNCTION__);
if (!req->req.url.full)
{
req->req.url.full = sdsnewlen(at, len);
}
else
{
req->req.url.full = sdscatlen((sds) req->req.url.full, at, len);
}
return 0;
}
#define HEADER_COUNT req->req.header_count
#define CURRENT_HEADER req->req.headers[HEADER_COUNT]
static int on_header_field(http_parser *parser, const char *at, size_t len)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req = SLIST_FIRST(&connection->requests);
LOG_DEBUG("%s", __FUNCTION__);
start:
if (!CURRENT_HEADER.name)
{
CURRENT_HEADER.name = sdsnewlen(at, len);
}
else
{
if (!CURRENT_HEADER.value)
{
CURRENT_HEADER.name = sdscatlen((sds) CURRENT_HEADER.name, at, len);
}
else
{
++HEADER_COUNT;
goto start;
}
}
return 0;
}
static int on_header_value(http_parser *parser, const char *at, size_t len)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req = SLIST_FIRST(&connection->requests);
LOG_DEBUG("%s", __FUNCTION__);
if (!CURRENT_HEADER.value)
{
CURRENT_HEADER.value = sdsnewlen(at, len);
}
else
{
CURRENT_HEADER.value = sdscatlen((sds) CURRENT_HEADER.value, at, len);
}
return 0;
}
#undef CURRENT_HEADER
#undef HEADER_COUNT
static int on_headers_complete(http_parser *parser)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *priv = SLIST_FIRST(&connection->requests);
struct uvh_request *req = &priv->req;
struct http_parser_url url;
const char *full = req->url.full;
LOG_DEBUG("%s", __FUNCTION__);
++req->header_count;
http_parser_parse_url(req->url.full, sdslen((sds) req->url.full),
1, &url);
#define UF_OFFSET(X) url.field_data[X].off
#define UF_LEN(X) url.field_data[X].len
#define UF_SET(X) (url.field_set & (1 << (X)))
#define UF_CHECK_AND_SET(X, DST) \
if (UF_SET(X)) \
(DST) = sdsnewlen(full + UF_OFFSET(X), UF_LEN(X))
UF_CHECK_AND_SET(UF_SCHEMA, req->url.schema);
UF_CHECK_AND_SET(UF_HOST, req->url.host);
UF_CHECK_AND_SET(UF_PORT, req->url.port);
UF_CHECK_AND_SET(UF_PATH, req->url.path);
UF_CHECK_AND_SET(UF_QUERY, req->url.query);
UF_CHECK_AND_SET(UF_FRAGMENT, req->url.fragment);
UF_CHECK_AND_SET(UF_USERINFO, req->url.userinfo);
#undef UF_CHECK_AND_SET
#undef UF_SET
#undef UF_LEN
#undef UF_OFFSET
return 0;
}
static int on_body(http_parser *parser, const char *at, size_t len)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req = SLIST_FIRST(&connection->requests);
LOG_DEBUG("%s", __FUNCTION__);
if (!req->req.content)
{
req->req.content = sdsnewlen(at, len);
}
else
{
req->req.content = sdscatlen((sds) req->req.content, at, len);
}
return 0;
}
static int on_message_complete(http_parser *parser)
{
struct uvh_connection *connection = (struct uvh_connection *) parser->data;
struct uvh_request_private *req = SLIST_FIRST(&connection->requests);
LOG_DEBUG("%s", __FUNCTION__);
req->keepalive = http_should_keep_alive(parser);
if (req->req.content)
req->req.content_length = sdslen((sds) req->req.content);
else
req->req.content_length = 0;
req->req.method = sdsnew(http_method_str(parser->method));
req->req.version = sdsempty();
req->req.version = sdscatprintf((sds) req->req.version,
"HTTP/%d.%d", parser->http_major, parser->http_minor);
if (req->req.server->request_handler)
req->req.server->request_handler(&req->req);
return 0;
}
static struct uvh_request_private *uvh_request_new(struct uvh_connection *conn)
{
struct uvh_request_private *req = calloc(1, sizeof(*req));
req->connection = conn;
req->req.server = conn->server;
req->req.data = conn->server->userdata;
req->send_body = sdsempty();
req->send_headers = sdsempty();
req->send_status = HTTP_OK;
return req;
}
static void uvh_request_free(struct uvh_request_private *req)
{
int i;
LOG_DEBUG("%s", __FUNCTION__);
for (i = 0; i < req->req.header_count; ++i)
{
sdsfree((sds) req->req.headers[i].name);
sdsfree((sds) req->req.headers[i].value);
}
sdsfree((sds) req->req.method);
sdsfree((sds) req->req.version);
sdsfree((sds) req->req.url.full);
sdsfree((sds) req->req.url.schema);
sdsfree((sds) req->req.url.host);
sdsfree((sds) req->req.url.port);
sdsfree((sds) req->req.url.path);
sdsfree((sds) req->req.url.query);
sdsfree((sds) req->req.url.fragment);
sdsfree((sds) req->req.url.userinfo);
sdsfree((sds) req->req.content);
// TODO: what about send_body, send_headers
free(req);
}
static struct uvh_connection *uvh_connection_new(struct uvh_server *server)
{
struct uvh_connection *connection = calloc(1, sizeof(*connection));
connection->server = server;
http_parser_init(&connection->parser, HTTP_REQUEST);
connection->parser.data = connection;
SLIST_INIT(&connection->requests);
if (uv_tcp_init(server->loop, &connection->stream) < 0)
{
LOG_WARNING("failed to initialize uv_tcp_t");
goto error;
}
return connection;
error:
if (connection)
uvh_connection_free(connection);
return NULL;
}
static void uvh_connection_free(struct uvh_connection *conn)
{
struct uvh_request_private *req;
LOG_DEBUG("%s", __FUNCTION__);
SLIST_FOREACH(req, &conn->requests, siblings)
{
uvh_request_free(req);
}
free(conn);
}
struct uvh_write_request
{
uv_write_t wreq;
uv_buf_t buf;
struct uvh_request_private *req;
};
static void uvh_write_request_free(struct uvh_write_request *req)
{
sdsfree((sds) req->buf.base);
free(req);
}
static void after_request_write(uv_write_t *req, int status)
{
LOG_DEBUG("%s", __FUNCTION__);
struct uvh_write_request *wreq = (struct uvh_write_request *) req;
(void) status;
uvh_write_request_free(wreq);
}
static void uvh_request_write_sds(struct uvh_request *req, sds data,
uv_write_cb cb)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
struct uvh_write_request *wreq = calloc(1, sizeof(*wreq));
wreq->buf.base = (char *) data;
wreq->buf.len = sdslen(data);
wreq->req = p;
uv_write(&wreq->wreq, (uv_stream_t *) &p->connection->stream, &wreq->buf,
1, cb);
}
UVH_EXTERN void uvh_request_write(struct uvh_request *req,
const char *data, size_t len)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
if (p->streaming)
{
uvh_request_write_chunk(req, sdsnewlen(data, len));
}
else
p->send_body = sdscatlen(p->send_body, data, len);
}
UVH_EXTERN void uvh_request_writef(struct uvh_request *req, const char *fmt,
...)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
va_list ap;
sds result;
va_start(ap, fmt);
result = sdscatvprintf(sdsempty(), fmt, ap);
va_end(ap);
if (p->streaming)
{
uvh_request_write_chunk(req, result);
}
else
p->send_body = sdscatsds(p->send_body, result);
sdsfree(result);
}
UVH_EXTERN void uvh_request_write_status(struct uvh_request *req, int status)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
p->send_status = status;
}
UVH_EXTERN void uvh_request_write_header(struct uvh_request *req,
const char *name, const char *value)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
if (p->streaming)
return;
p->send_headers = sdscatprintf(p->send_headers, "%s: %s\r\n", name, value);
}
UVH_EXTERN const char *http_status_code_str(int code)
{
struct http_status_code_def *def = http_status_code_defs;
while (def->code != -1)
{
if (def->code == code)
{
return def->str;
}
++def;
}
return NULL;
}
UVH_EXTERN const char *uvh_request_get_header(struct uvh_request *req,
const char *name)
{
int i;
for (i = 0; i < req->header_count; ++i)
{
if (strcasecmp(name, req->headers[i].name) == 0)
return req->headers[i].value;
}
return NULL;
}
UVH_EXTERN void uvh_request_end(struct uvh_request *req)
{
LOG_DEBUG("%s", __FUNCTION__);
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
uvh_request_write_sds(req, sdscatprintf(sdsempty(),
"%s %d %s\r\n", p->req.version, p->send_status,
http_status_code_str(p->send_status)), &after_request_write);
if (!p->streaming)
{
sds content_len = sdscatprintf(sdsempty(), "%d", (int)sdslen(p->send_body));
uvh_request_write_header(req, "Content-Length", content_len);
sdsfree(content_len);
}
LOG_DEBUG("keepalive: %d", p->keepalive);
if (!p->keepalive)
{
uvh_request_write_header(req, "Connection", "close");
}
uvh_request_write_sds(req, p->send_headers, &after_request_write);
uvh_request_write_sds(req, sdsnew("\r\n"), &after_request_write);
if (!p->streaming)
uvh_request_write_sds(req, p->send_body, &after_request_write);
else
sdsfree(p->send_body);
if (!p->streaming)
{
struct uvh_connection *connection = p->connection;
SLIST_REMOVE(&connection->requests, p, uvh_request_private, siblings);
uvh_request_free(p);
}
}
static void after_last_chunk_write(uv_write_t *req, int status)
{
LOG_DEBUG("%s", __FUNCTION__);
struct uvh_write_request *wreq = container_of(req, struct uvh_write_request,
wreq);
(void)status;
SLIST_REMOVE(&wreq->req->connection->requests, wreq->req,
uvh_request_private, siblings);
uvh_request_free(wreq->req);
uvh_write_request_free(wreq);
}
static void after_chunk_write(uv_write_t *req, int status)
{
LOG_DEBUG("%s", __FUNCTION__);
(void)status;
struct uvh_write_request *wreq = container_of(req, struct uvh_write_request,
wreq);
struct uvh_request_private *p = wreq->req;
uvh_write_request_free(wreq);
if (p->stream_cb)
{
char *chunk;
int chunklen = p->stream_cb(&chunk, p->stream_userdata);
if (chunklen == 0)
{
uvh_request_write_chunk(&p->req, NULL);
}
else
{
uvh_request_write_chunk(&p->req, sdsnewlen(chunk, chunklen));
free(chunk);
}
}
}
static void uvh_request_write_chunk(struct uvh_request *req, sds chunk)
{
unsigned int len = chunk != NULL ? sdslen(chunk) : 0;
LOG_DEBUG("%s len:%u", __FUNCTION__, len);
sds chunklen = sdscatprintf(sdsempty(), "%X\r\n", len);
uvh_request_write_sds(req, chunklen, &after_request_write);
uv_write_cb callback;
if (len > 0)
{
uvh_request_write_sds(req, chunk, &after_request_write);
callback = &after_chunk_write;
}
else
{
sdsfree(chunk);
callback = &after_last_chunk_write;
}
uvh_request_write_sds(req, sdsnew("\r\n"), callback);
}
UVH_EXTERN void uvh_request_stream(struct uvh_request *req, uvh_stream_cb cb,
void *data)
{
struct uvh_request_private *p = container_of(req,
struct uvh_request_private, req);
uvh_request_write_header(req, "Transfer-Encoding", "chunked");
p->streaming = 1;
p->stream_cb = cb;
p->stream_userdata = data;
uvh_request_end(req);
if (cb)
{
char *chunk;
int chunklen = cb(&chunk, data);
uvh_request_write_chunk(req, sdsnewlen(chunk, chunklen));
free(chunk);
}
}
|