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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* MIT License
*
* Copyright (c) 2019 Jeremy Williams
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* httpserver.h (0.7.0)
*
* Description:
*
* A single header C library for building non-blocking event driven HTTP
* servers
*
* Usage:
*
* Do this:
* #define HTTPSERVER_IMPL
* before you include this file in *one* C or C++ file to create the
* implementation.
*
* // i.e. it should look like this:
* #include ...
* #include ...
* #include ...
* #define HTTPSERVER_IMPL
* #include "httpserver.h"
*
* There are some #defines that can be configured. This must be done in the
* same file that you define HTTPSERVER_IMPL These defines have default values
* and will need to be #undef'd and redefined to configure them.
*
* HTTP_REQUEST_BUF_SIZE - default 1024 - The initial size in bytes of the
* read buffer for the request. This buffer grows automatically if it's
* capacity is reached but it certain environments it may be optimal to
* change this value.
*
* HTTP_RESPONSE_BUF_SIZE - default 1024 - Same as above except for the
* response buffer.
*
* HTTP_REQUEST_TIMEOUT - default 20 - The amount of seconds the request will
* wait for activity on the socket before closing. This only applies mid
* request. For the amount of time to hold onto keep-alive connections see
* below.
*
* HTTP_KEEP_ALIVE_TIMEOUT - default 120 - The amount of seconds to keep a
* connection alive a keep-alive request has completed.
*
* HTTP_MAX_TOTAL_EST_MEM_USAGE - default 4294967296 (4GB) - This is the
* amount of read/write buffer space that is allowed to be allocated across
* all requests before new requests will get 503 responses.
*
* HTTP_MAX_TOKEN_LENGTH - default 8192 (8KB) - This is the max size of any
* non body http tokens. i.e: header names, header values, url length, etc.
*
* HTTP_MAX_REQUEST_BUF_SIZE - default 8388608 (8MB) - This is the maximum
* amount of bytes that the request buffer will grow to. If the body of the
* request + headers cannot fit in this size the request body will be
* streamed in.
*
* For more details see the documentation of the interface and the example
* below.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef HTTPSERVER_H
#define HTTPSERVER_H
#ifdef __cplusplus
extern "C" {
#endif
// String type used to read the request details. The char pointer is NOT null
// terminated.
struct http_string_s {
char const * buf;
int len;
};
struct http_server_s;
struct http_request_s;
struct http_response_s;
// Returns the event loop id that the server is running on. This will be an
// epoll fd when running on Linux or a kqueue on BSD. This can be used to
// listen for activity on sockets, etc. The only caveat is that the user data
// must be set to a struct where the first member is the function pointer to
// a callback that will handle the event. i.e:
//
// For kevent:
//
// struct foo {
// void (*handler)(struct kevent*);
// ...
// }
//
// // Set ev.udata to a foo pointer when registering the event.
//
// For epoll:
//
// struct foo {
// void (*handler)(struct epoll_event*);
// ...
// }
//
// // Set ev.data.ptr to a foo pointer when registering the event.
int http_server_loop(struct http_server_s* server);
// Allocates and initializes the http server. Takes a port and a function
// pointer that is called to process requests.
struct http_server_s* http_server_init(int port, void (*handler)(struct http_request_s*));
// Stores a pointer for future retrieval. This is not used by the library in
// any way and is strictly for you, the application programmer to make use
// of.
void http_server_set_userdata(struct http_server_s* server, void* data);
// Starts the event loop and the server listening. During normal operation this
// function will not return. Return value is the error code if the server fails
// to start. By default it will listen on all interface. For the second variant
// provide the IP address of the interface to listen on, or NULL for any.
int http_server_listen(struct http_server_s* server);
int http_server_listen_addr(struct http_server_s* server, const char* ipaddr);
// Use this listen call in place of the one above when you want to integrate
// an http server into an existing application that has a loop already and you
// want to use the polling functionality instead. This works well for
// applications like games that have a constant update loop. By default it will
// listen on all interface. For the second variant provide the IP address of
// the interface to listen on, or NULL for any.
int http_server_listen_poll(struct http_server_s* server);
int http_server_listen_addr_poll(struct http_server_s* server, const char* ipaddr);
// Call this function in your update loop. It will trigger the request handler
// once if there is a request ready. Returns 1 if a request was handled and 0
// if no requests were handled. It should be called in a loop until it returns
// 0.
int http_server_poll(struct http_server_s* server);
// Returns 1 if the flag is set and false otherwise. The flags that can be
// queried are listed below
int http_request_has_flag(struct http_request_s* request, int flag);
// This flag will be set when the request body is chunked or the body is too
// large to fit in memory are once. This means that the http_request_read_chunk
// function must be used to read the body piece by piece.
#define HTTP_FLG_STREAMED 0x1
// Returns the request method as it was read from the HTTP request line.
struct http_string_s http_request_method(struct http_request_s* request);
// Returns the full request target (url) as it was read from the HTTP request
// line.
struct http_string_s http_request_target(struct http_request_s* request);
// Returns the request body. If no request body was sent buf and len of the
// string will be set to 0.
struct http_string_s http_request_body(struct http_request_s* request);
// Returns the request header value for the given header key. The key is case
// insensitive.
struct http_string_s http_request_header(struct http_request_s* request, char const * key);
// Procedure used to iterate over all the request headers. iter should be
// initialized to zero before calling. Each call will set key and val to the
// key and value of the next header. Returns 0 when there are no more headers.
int http_request_iterate_headers(
struct http_request_s* request,
struct http_string_s* key,
struct http_string_s* val,
int* iter
);
// Retrieve the opaque data pointer that was set with http_request_set_userdata.
void* http_request_userdata(struct http_request_s* request);
// Retrieve the opaque data pointer that was set with http_server_set_userdata.
void* http_request_server_userdata(struct http_request_s* request);
// Stores a pointer for future retrieval. This is not used by the library in
// any way and is strictly for you, the application programmer to make use
// of.
void http_request_set_userdata(struct http_request_s* request, void* data);
#define HTTP_KEEP_ALIVE 1
#define HTTP_CLOSE 0
// By default the server will inspect the Connection header and the HTTP
// version to determine whether the connection should be kept alive or not.
// Use this function to override that behaviour to force the connection to
// keep-alive or close by passing in the HTTP_KEEP_ALIVE or HTTP_CLOSE
// directives respectively. This may provide a minor performance improvement
// in cases where you control client and server and want to always close or
// keep the connection alive.
void http_request_connection(struct http_request_s* request, int directive);
// When reading in the HTTP request the server allocates a buffer to store
// the request details such as the headers, method, body, etc. By default this
// memory will be freed when http_respond is called. This function lets you
// free that memory before the http_respond call. This can be useful if you
// have requests that take a long time to complete and you don't require the
// request data. Accessing any http_string_s's will be invalid after this call.
void http_request_free_buffer(struct http_request_s* request);
// Allocates an http response. This memory will be freed when http_respond is
// called.
struct http_response_s* http_response_init();
// Set the response status. Accepts values between 100 and 599 inclusive. Any
// other value will map to 500.
void http_response_status(struct http_response_s* response, int status);
// Set a response header. Takes two null terminated strings.
void http_response_header(struct http_response_s* response, char const * key, char const * value);
// Set the response body. The caller is responsible for freeing any memory that
// may have been allocated for the body. It is safe to free this memory AFTER
// http_respond has been called.
void http_response_body(struct http_response_s* response, char const * body, int length);
// Starts writing the response to the client. Any memory allocated for the
// response body or response headers is safe to free after this call.
void http_respond(struct http_request_s* request, struct http_response_s* response);
// Writes a chunk to the client. The notify_done callback will be called when
// the write is complete. This call consumes the response so a new response
// will need to be initialized for each chunk. The response status of the
// request will be the response status that is set when http_respond_chunk is
// called the first time. Any headers set for the first call will be sent as
// the response headers. Headers set for subsequent calls will be ignored.
void http_respond_chunk(
struct http_request_s* request,
struct http_response_s* response,
void (*notify_done)(struct http_request_s*)
);
// Ends the chunked response. Any headers set before this call will be included
// as what the HTTP spec refers to as 'trailers' which are essentially more
// response headers.
void http_respond_chunk_end(struct http_request_s* request, struct http_response_s* response);
// If a request has Transfer-Encoding: chunked or the body is too big to fit in
// memory all at once you cannot read the body in the typical way. Instead you
// need to call this function to read one chunk at a time. To check if the
// request requires this type of reading you can call the http_request_has_flag
// function to check if the HTTP_FLG_STREAMED flag is set. To read a streamed body
// you pass a callback that will be called when the chunk is ready. When
// the callback is called you can use `http_request_chunk` to get the current
// chunk. When done with that chunk call this function again to request the
// next chunk. If the chunk has size 0 then the request body has been completely
// read and you can now respond.
void http_request_read_chunk(
struct http_request_s* request,
void (*chunk_cb)(struct http_request_s*)
);
// Returns the current chunk of the request body. This chunk is only valid until
// the next call to `http_request_read_chunk`.
struct http_string_s http_request_chunk(struct http_request_s* request);
#define http_request_read_body http_request_read_chunk
#ifdef __cplusplus
}
#endif
// Minimal example usage.
#ifdef HTTPSERVER_EXAMPLE
#define RESPONSE "Hello, World!"
void handle_request(struct http_request_s* request) {
struct http_response_s* response = http_response_init();
http_response_status(response, 200);
http_response_header(response, "Content-Type", "text/plain");
http_response_body(response, RESPONSE, sizeof(RESPONSE) - 1);
http_respond(request, response);
}
int main() {
struct http_server_s* server = http_server_init(8080, handle_request);
http_server_listen(server);
}
#endif
#endif
#ifdef HTTPSERVER_IMPL
#ifndef HTTPSERVER_IMPL_ONCE
#define HTTPSERVER_IMPL_ONCE
#ifdef __linux__
#define EPOLL
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L
#endif
#else
#define KQUEUE
#endif
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <limits.h>
#include <assert.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef KQUEUE
#include <sys/event.h>
#else
#include <sys/epoll.h>
#include <sys/timerfd.h>
#endif
// *** macro definitions
// Application configurable
#define HTTP_REQUEST_BUF_SIZE 1024
#define HTTP_RESPONSE_BUF_SIZE 1024
#define HTTP_REQUEST_TIMEOUT 20
#define HTTP_KEEP_ALIVE_TIMEOUT 120
#define HTTP_MAX_TOKEN_LENGTH 8192 // 8kb
#define HTTP_MAX_TOTAL_EST_MEM_USAGE 4294967296 // 4gb
#define HTTP_MAX_REQUEST_BUF_SIZE 8388608 // 8mb
#define HTTP_MAX_HEADER_COUNT 127
#define HTTP_FLAG_SET(var, flag) var |= flag
#define HTTP_FLAG_CLEAR(var, flag) var &= ~flag
#define HTTP_FLAG_CHECK(var, flag) (var & flag)
// stream flags
#define HS_SF_CONSUMED 0x1
// parser flags
#define HS_PF_IN_CONTENT_LEN 0x1
#define HS_PF_IN_TRANSFER_ENC 0x2
#define HS_PF_CHUNKED 0x4
#define HS_PF_CKEND 0x8
#define HS_PF_REQ_END 0x10
// http session states
#define HTTP_SESSION_INIT 0
#define HTTP_SESSION_READ 1
#define HTTP_SESSION_WRITE 2
#define HTTP_SESSION_NOP 3
// http session flags
#define HTTP_END_SESSION 0x2
#define HTTP_AUTOMATIC 0x8
#define HTTP_CHUNKED_RESPONSE 0x20
// http version indicators
#define HTTP_1_0 0
#define HTTP_1_1 1
// *** declarations ***
// structs
typedef struct {
int index;
int len;
int type;
} http_token_t;
typedef struct {
http_token_t* buf;
int capacity;
int size;
} http_token_dyn_t;
#ifdef EPOLL
typedef void (*epoll_cb_t)(struct epoll_event*);
#endif
typedef struct http_ev_cb_s {
#ifdef KQUEUE
void (*handler)(struct kevent* ev);
#else
epoll_cb_t handler;
#endif
} ev_cb_t;
typedef struct {
char* buf;
int64_t total_bytes;
int32_t capacity;
int32_t length;
int32_t index;
int32_t anchor;
http_token_t token;
uint8_t flags;
} hs_stream_t;
typedef struct {
int64_t content_length;
int64_t body_consumed;
int16_t match_index;
int16_t header_count;
int8_t state;
int8_t meta;
} http_parser_t;
typedef struct http_request_s {
#ifdef KQUEUE
void (*handler)(struct kevent* ev);
#else
epoll_cb_t handler;
epoll_cb_t timer_handler;
int timerfd;
#endif
void (*chunk_cb)(struct http_request_s*);
void* data;
hs_stream_t stream;
http_parser_t parser;
int state;
int socket;
int timeout;
struct http_server_s* server;
http_token_dyn_t tokens;
char flags;
} http_request_t;
typedef struct http_server_s {
#ifdef KQUEUE
void (*handler)(struct kevent* ev);
#else
epoll_cb_t handler;
epoll_cb_t timer_handler;
#endif
int64_t memused;
int socket;
int port;
int loop;
int timerfd;
socklen_t len;
void (*request_handler)(http_request_t*);
struct sockaddr_in addr;
void* data;
char date[32];
} http_server_t;
typedef struct http_header_s {
char const * key;
char const * value;
struct http_header_s* next;
} http_header_t;
typedef struct http_response_s {
http_header_t* headers;
char const * body;
int content_length;
int status;
} http_response_t;
typedef struct http_string_s http_string_t;
// enums
enum hs_token {
HS_TOK_NONE, HS_TOK_METHOD, HS_TOK_TARGET, HS_TOK_VERSION,
HS_TOK_HEADER_KEY, HS_TOK_HEADER_VAL, HS_TOK_CHUNK_BODY, HS_TOK_BODY,
HS_TOK_BODY_STREAM, HS_TOK_REQ_END, HS_TOK_EOF, HS_TOK_ERROR
};
enum hs_state {
ST, MT, MS, TR, TS, VN, RR, RN, HK, HS, HV, HR, HE,
ER, HN, BD, CS, CB, CE, CR, CN, CD, C1, C2, BR, HS_STATE_LEN
};
enum hs_char_type {
HS_SPC, HS_NL, HS_CR, HS_COLN, HS_TAB, HS_SCOLN,
HS_DIGIT, HS_HEX, HS_ALPHA, HS_TCHAR, HS_VCHAR, HS_ETC, HS_CHAR_TYPE_LEN
};
enum hs_meta_state {
M_WFK, M_ANY, M_MTE, M_MCL, M_CLV, M_MCK, M_SML, M_CHK, M_BIG, M_ZER, M_CSZ,
M_CBD, M_LST, M_STR, M_SEN, M_BDY, M_END, M_ERR
};
enum hs_meta_type {
HS_META_NOT_CONTENT_LEN, HS_META_NOT_TRANSFER_ENC, HS_META_END_KEY,
HS_META_END_VALUE, HS_META_END_HEADERS, HS_META_LARGE_BODY,
HS_META_TYPE_LEN
};
#define HS_META_NOT_CHUNKED 0
#define HS_META_NON_ZERO 0
#define HS_META_END_CHK_SIZE 1
#define HS_META_END_CHUNK 2
#define HS_META_NEXT 0
// prototypes
void hs_add_server_sock_events(struct http_server_s* serv);
void hs_server_init(struct http_server_s* serv);
void hs_delete_events(struct http_request_s* request);
void hs_add_events(struct http_request_s* request);
void hs_add_write_event(struct http_request_s* request);
void hs_process_tokens(http_request_t* request);
#ifdef KQUEUE
void hs_server_listen_cb(struct kevent* ev);
void hs_session_io_cb(struct kevent* ev);
#else
void hs_server_listen_cb(struct epoll_event* ev);
void hs_session_io_cb(struct epoll_event* ev);
void hs_server_timer_cb(struct epoll_event* ev);
void hs_request_timer_cb(struct epoll_event* ev);
#endif
// constants
char const * hs_status_text[] = {
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//100s
"Continue", "Switching Protocols", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//200s
"OK", "Created", "Accepted", "Non-Authoritative Information", "No Content",
"Reset Content", "Partial Content", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//300s
"Multiple Choices", "Moved Permanently", "Found", "See Other", "Not Modified",
"Use Proxy", "", "Temporary Redirect", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//400s
"Bad Request", "Unauthorized", "Payment Required", "Forbidden", "Not Found",
"Method Not Allowed", "Not Acceptable", "Proxy Authentication Required",
"Request Timeout", "Conflict",
"Gone", "Length Required", "", "Payload Too Large", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
//500s
"Internal Server Error", "Not Implemented", "Bad Gateway", "Service Unavailable",
"Gateway Timeout", "", "", "", "", ""
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", ""
};
static int const hs_transitions[] = {
// A-Z G-Z
// spc \n \r : \t ; 0-9 a-f g-z tch vch etc
/* ST start */ BR, BR, BR, BR, BR, BR, BR, MT, MT, MT, BR, BR,
/* MT method */ MS, BR, BR, BR, BR, BR, MT, MT, MT, MT, BR, BR,
/* MS methodsp */ BR, BR, BR, BR, BR, BR, TR, TR, TR, TR, TR, BR,
/* TR target */ TS, BR, BR, TR, BR, TR, TR, TR, TR, TR, TR, BR,
/* TS targetsp */ BR, BR, BR, BR, BR, BR, VN, VN, VN, VN, VN, BR,
/* VN version */ BR, BR, RR, BR, BR, BR, VN, VN, VN, VN, VN, BR,
/* RR rl \r */ BR, RN, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* RN rl \n */ BR, BR, BR, BR, BR, BR, HK, HK, HK, HK, BR, BR,
/* HK headkey */ BR, BR, BR, HS, BR, BR, HK, HK, HK, HK, BR, BR,
/* HS headspc */ HS, HS, HS, HV, HS, HV, HV, HV, HV, HV, HV, BR,
/* HV headval */ HV, BR, HR, HV, HV, HV, HV, HV, HV, HV, HV, BR,
/* HR head\r */ BR, HE, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* HE head\n */ BR, BR, ER, BR, BR, BR, HK, HK, HK, HK, BR, BR,
/* ER hend\r */ BR, HN, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* HN hend\n */ BD, BD, BD, BD, BD, BD, BD, BD, BD, BD, BD, BD,
/* BD body */ BD, BD, BD, BD, BD, BD, BD, BD, BD, BD, BD, BD,
/* CS chksz */ BR, BR, CR, BR, BR, CE, CS, CS, BR, BR, BR, BR,
/* CB chkbd */ CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB,
/* CE chkext */ BR, BR, CR, CE, CE, CE, CE, CE, CE, CE, CE, BR,
/* CR chksz\r */ BR, CN, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* CN chksz\n */ CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB, CB,
/* CD chkend */ BR, BR, C1, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* C1 chkend\r */ BR, C2, BR, BR, BR, BR, BR, BR, BR, BR, BR, BR,
/* C2 chkend\n */ BR, BR, BR, BR, BR, BR, CS, CS, BR, BR, BR, BR
};
static int const hs_meta_transitions[] = {
// no chk
// not cl not te endkey endval end h toobig
/* WFK wait */ M_WFK, M_WFK, M_WFK, M_ANY, M_END, M_ERR,
/* ANY matchkey */ M_MTE, M_MCL, M_WFK, M_ERR, M_END, M_ERR,
/* MTE matchte */ M_MTE, M_WFK, M_MCK, M_ERR, M_ERR, M_ERR,
/* MCL matchcl */ M_WFK, M_MCL, M_CLV, M_ERR, M_ERR, M_ERR,
/* CLV clvalue */ M_ERR, M_ERR, M_ERR, M_SML, M_ERR, M_ERR,
/* MCK matchchk */ M_WFK, M_ERR, M_ERR, M_CHK, M_ERR, M_ERR,
/* SML smallbdy */ M_SML, M_SML, M_SML, M_SML, M_BDY, M_BIG,
/* CHK chunkbdy */ M_CHK, M_CHK, M_CHK, M_CHK, M_ZER, M_ERR,
/* BIG bigbody */ M_BIG, M_BIG, M_BIG, M_BIG, M_STR, M_ERR,
// *** chunked body ***
// nonzer endsz endchk
/* ZER zerochk */ M_CSZ, M_LST, M_ERR, M_ERR, M_ERR, M_ERR,
/* CSZ chksize */ M_CSZ, M_CBD, M_ERR, M_ERR, M_ERR, M_ERR,
/* CBD readchk */ M_CBD, M_CBD, M_ZER, M_ERR, M_ERR, M_ERR,
/* LST lastchk */ M_LST, M_END, M_END, M_ERR, M_ERR, M_ERR,
// *** streamed body ***
// next
/* STR readstr */ M_SEN, M_ERR, M_ERR, M_ERR, M_ERR, M_ERR,
/* SEN strend */ M_END, M_ERR, M_ERR, M_ERR, M_ERR, M_ERR,
// *** small body ***
// next
/* BDY readbody */ M_END, M_ERR, M_ERR, M_ERR, M_ERR, M_ERR,
/* END reqend */ M_WFK, M_ERR, M_ERR, M_ERR, M_ERR, M_ERR
};
static int const hs_ctype[] = {
HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC,
HS_ETC, HS_ETC, HS_TAB, HS_NL, HS_ETC, HS_ETC, HS_CR,
HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC,
HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_ETC,
HS_ETC, HS_ETC, HS_ETC, HS_ETC, HS_SPC, HS_TCHAR, HS_VCHAR,
HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_VCHAR, HS_VCHAR,
HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_VCHAR, HS_DIGIT,
HS_DIGIT, HS_DIGIT, HS_DIGIT, HS_DIGIT, HS_DIGIT, HS_DIGIT, HS_DIGIT,
HS_DIGIT, HS_DIGIT, HS_COLN, HS_SCOLN, HS_VCHAR, HS_VCHAR, HS_VCHAR,
HS_VCHAR, HS_VCHAR, HS_HEX, HS_HEX, HS_HEX, HS_HEX, HS_HEX,
HS_HEX, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA,
HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA,
HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA,
HS_VCHAR, HS_VCHAR, HS_VCHAR, HS_TCHAR, HS_TCHAR, HS_TCHAR, HS_HEX,
HS_HEX, HS_HEX, HS_HEX, HS_HEX, HS_HEX, HS_ALPHA, HS_ALPHA,
HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA,
HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA,
HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_ALPHA, HS_VCHAR, HS_TCHAR, HS_VCHAR,
HS_TCHAR, HS_ETC
};
static int const hs_token_start_states[] = {
//ST MT MS TR TS VN RR RN HK
0, HS_TOK_METHOD, 0, HS_TOK_TARGET, 0, HS_TOK_VERSION, 0, 0, HS_TOK_HEADER_KEY,
//HS HV HR HE ER HN BD CS CB CE CR CN
0, HS_TOK_HEADER_VAL, 0, 0, 0, 0, HS_TOK_BODY, 0, HS_TOK_CHUNK_BODY, 0, 0, 0,
//CD C1 C2
0, 0, 0,
};
// *** input stream ***
int hs_stream_read_socket(hs_stream_t* stream, int socket, int64_t* memused) {
if (stream->index < stream->length) return 1;
if (!stream->buf) {
*memused += HTTP_REQUEST_BUF_SIZE;
stream->buf = (char*)calloc(1, HTTP_REQUEST_BUF_SIZE);
assert(stream->buf != NULL);
stream->capacity = HTTP_REQUEST_BUF_SIZE;
}
int bytes;
do {
bytes = read(
socket,
stream->buf + stream->length,
stream->capacity - stream->length
);
if (bytes > 0) {
stream->length += bytes;
stream->total_bytes += bytes;
}
if (
stream->length == stream->capacity &&
stream->capacity != HTTP_MAX_REQUEST_BUF_SIZE
) {
*memused -= stream->capacity;
stream->capacity *= 2;
if (stream->capacity > HTTP_MAX_REQUEST_BUF_SIZE) {
stream->capacity = HTTP_MAX_REQUEST_BUF_SIZE;
}
*memused += stream->capacity;
stream->buf = (char*)realloc(stream->buf, stream->capacity);
assert(stream->buf != NULL);
}
} while (bytes > 0 && stream->capacity < HTTP_MAX_REQUEST_BUF_SIZE);
return bytes == 0 ? 0 : 1;
}
int hs_stream_next(hs_stream_t* stream, char* c) {
HTTP_FLAG_CLEAR(stream->flags, HS_SF_CONSUMED);
if (stream->index >= stream->length) return 0;
*c = stream->buf[stream->index];
return 1;
}
void hs_stream_consume(hs_stream_t* stream) {
if (HTTP_FLAG_CHECK(stream->flags, HS_SF_CONSUMED)) return;
HTTP_FLAG_SET(stream->flags, HS_SF_CONSUMED);
stream->index++;
int new_len = stream->token.len + 1;
stream->token.len = stream->token.type == 0 ? 0 : new_len;
}
void hs_stream_begin_token(hs_stream_t* stream, int token_type) {
stream->token.index = stream->index;
stream->token.type = token_type;
}
int hs_stream_jump(hs_stream_t* stream, int offset) {
HTTP_FLAG_SET(stream->flags, HS_SF_CONSUMED);
if (stream->index + offset > stream->length) return 0;
stream->index += offset;
int new_len = stream->token.len + offset;
stream->token.len = stream->token.type == 0 ? 0 : new_len;
return 1;
}
int hs_stream_jumpall(hs_stream_t* stream) {
int offset = stream->length - stream->index;
stream->index += offset;
int new_len = stream->token.len + offset;
HTTP_FLAG_SET(stream->flags, HS_SF_CONSUMED);
stream->token.len = stream->token.type == 0 ? 0 : new_len;
return offset;
}
void hs_stream_anchor(hs_stream_t* stream) {
stream->anchor = stream->index;
}
http_token_t hs_stream_emit(hs_stream_t* stream) {
http_token_t token = stream->token;
http_token_t none = {0, 0, 0};
stream->token = none;
return token;
}
http_token_t hs_stream_current_token(hs_stream_t* stream) {
return stream->token;
}
int hs_stream_can_contain(hs_stream_t* stream, int64_t size) {
return HTTP_MAX_REQUEST_BUF_SIZE - stream->index + 1 >= size;
}
void hs_stream_shift(hs_stream_t* stream) {
if (stream->token.index == stream->anchor) return;
if (stream->token.len > 0) {
char* dst = stream->buf + stream->anchor;
char const* src = stream->buf + stream->token.index;
int bytes = stream->length - stream->token.index;
memcpy(dst, src, bytes);
}
stream->token.index = stream->anchor;
stream->index = stream->token.len + stream->anchor;
stream->length = stream->anchor + stream->token.len;
}
// *** http parser ***
void hs_trigger_meta(http_parser_t* parser, int event) {
int to = hs_meta_transitions[parser->meta * HS_META_TYPE_LEN + event];
parser->meta = to;
}
#define HS_MATCH(str, meta) \
in_bounds = parser->match_index < (int)sizeof(str) - 1; \
m = in_bounds ? str[parser->match_index] : m; \
low = c >= 'A' && c <= 'Z' ? c + 32 : c; \
if (low != m) hs_trigger_meta(parser, meta);
http_token_t hs_transition_action(
http_parser_t* parser,
hs_stream_t* stream,
char c,
int8_t from,
int8_t to
) {
http_token_t emitted = {0, 0, 0};
if (from == HN) {
hs_stream_anchor(stream);
}
if (from != to) {
int type = hs_token_start_states[to];
if (type != HS_TOK_NONE) hs_stream_begin_token(stream, type);
if (from == CS) hs_trigger_meta(parser, HS_META_END_CHK_SIZE);
if (to == HK) {
parser->header_count++;
if (parser->header_count > HTTP_MAX_HEADER_COUNT) {
emitted.type = HS_TOK_ERROR;
}
} else if (to == HS) {
hs_trigger_meta(parser, HS_META_END_KEY);
emitted = hs_stream_emit(stream);
}
parser->match_index = 0;
}
char low, m = '\0';
int in_bounds = 0;
int body_left = 0;
switch (to) {
case MS:
case TS:
emitted = hs_stream_emit(stream);
break;
case RR:
case HR:
hs_trigger_meta(parser, HS_META_END_VALUE);
emitted = hs_stream_emit(stream);
break;
case HK:
HS_MATCH("transfer-encoding", HS_META_NOT_TRANSFER_ENC)
HS_MATCH("content-length", HS_META_NOT_CONTENT_LEN)
parser->match_index++;
break;
case HV:
if (parser->meta == M_MCK) {
HS_MATCH("chunked", HS_META_NOT_CHUNKED)
parser->match_index++;
} else if (parser->meta == M_CLV) {
parser->content_length *= 10;
parser->content_length += c - '0';
}
break;
case HN:
if (parser->meta == M_SML && !hs_stream_can_contain(stream, parser->content_length)) {
hs_trigger_meta(parser, HS_META_LARGE_BODY);
}
if (parser->meta == M_BIG || parser->meta == M_CHK) {
emitted.type = HS_TOK_BODY_STREAM;
}
//if (parser->meta == M_CHK) parser->state = CS;
hs_trigger_meta(parser, HS_META_END_HEADERS);
if (parser->content_length == 0 && parser->meta == M_BDY) parser->meta = M_END;
if (parser->meta == M_END) {
emitted.type = HS_TOK_BODY;
}
break;
case CS:
if (c != '0') hs_trigger_meta(parser, HS_META_NON_ZERO);
if (c >= 'A' && c <= 'F') {
parser->content_length *= 0x10;
parser->content_length += c - 55;
} else if (c >= 'a' && c <= 'f') {
parser->content_length *= 0x10;
parser->content_length += c - 87;
} else if (c >= '0' && c <= '9') {
parser->content_length *= 0x10;
parser->content_length += c - '0';
}
break;
case CB:
case BD:
if (parser->meta == M_STR) hs_stream_begin_token(stream, HS_TOK_CHUNK_BODY);
body_left = parser->content_length - parser->body_consumed;
if (hs_stream_jump(stream, body_left)) {
emitted = hs_stream_emit(stream);
hs_trigger_meta(parser, HS_META_NEXT);
if (to == CB) parser->state = CD;
parser->content_length = 0;
parser->body_consumed = 0;
} else {
parser->body_consumed += hs_stream_jumpall(stream);
if (parser->meta == M_STR) {
emitted = hs_stream_emit(stream);
hs_stream_shift(stream);
}
}
break;
case C2:
hs_trigger_meta(parser, HS_META_END_CHUNK);
break;
case BR:
emitted.type = HS_TOK_ERROR;
break;
}
return emitted;
}
http_token_t hs_meta_emit(http_parser_t* parser) {
http_token_t token = {0, 0, 0};
switch (parser->meta) {
case M_SEN:
token.type = HS_TOK_CHUNK_BODY;
hs_trigger_meta(parser, HS_META_NEXT);
break;
case M_END:
token.type = HS_TOK_REQ_END;
memset(parser, 0, sizeof(http_parser_t));
break;
}
return token;
}
http_token_t http_parse(http_parser_t* parser, hs_stream_t* stream) {
char c = 0;
http_token_t token = hs_meta_emit(parser);
if (token.type != HS_TOK_NONE) return token;
while (hs_stream_next(stream, &c)) {
int type = c < 0 ? HS_ETC : hs_ctype[(int)c];
int to = hs_transitions[parser->state * HS_CHAR_TYPE_LEN + type];
if (parser->meta == M_ZER && parser->state == HN && to == BD) {
to = CS;
}
int from = parser->state;
parser->state = to;
http_token_t emitted = hs_transition_action(parser, stream, c, from, to);
hs_stream_consume(stream);
if (emitted.type != HS_TOK_NONE) return emitted;
}
if (parser->state == CB) hs_stream_shift(stream);
token = hs_meta_emit(parser);
http_token_t current = hs_stream_current_token(stream);
if (
current.type != HS_TOK_CHUNK_BODY &&
current.type != HS_TOK_BODY &&
current.len > HTTP_MAX_TOKEN_LENGTH
) {
token.type = HS_TOK_ERROR;
}
return token;
}
// *** http server ***
void http_token_dyn_push(http_token_dyn_t* dyn, http_token_t a) {
if (dyn->size == dyn->capacity) {
dyn->capacity *= 2;
dyn->buf = (http_token_t*)realloc(dyn->buf, dyn->capacity * sizeof(http_token_t));
assert(dyn->buf != NULL);
}
dyn->buf[dyn->size] = a;
dyn->size++;
}
void http_token_dyn_init(http_token_dyn_t* dyn, int capacity) {
dyn->buf = (http_token_t*)malloc(sizeof(http_token_t) * capacity);
assert(dyn->buf != NULL);
dyn->size = 0;
dyn->capacity = capacity;
}
void hs_bind_localhost(int s, struct sockaddr_in* addr, const char* ipaddr, int port) {
addr->sin_family = AF_INET;
if (ipaddr == NULL) {
addr->sin_addr.s_addr = INADDR_ANY;
} else {
addr->sin_addr.s_addr = inet_addr(ipaddr);
}
addr->sin_port = htons(port);
int rc = bind(s, (struct sockaddr *)addr, sizeof(struct sockaddr_in));
if (rc < 0) {
exit(1);
}
}
int hs_write_client_socket(http_request_t* session) {
int bytes = write(
session->socket,
session->stream.buf + session->stream.total_bytes,
session->stream.length - session->stream.total_bytes
);
if (bytes > 0) session->stream.total_bytes += bytes;
return errno == EPIPE ? 0 : 1;
}
void hs_free_buffer(http_request_t* session) {
if (session->stream.buf) {
free(session->stream.buf);
session->server->memused -= session->stream.capacity;
session->stream.buf = NULL;
}
}
void hs_init_session(http_request_t* session) {
session->flags = HTTP_AUTOMATIC;
//session->parser = (http_parser_t){ };
//session->stream = (hs_stream_t){ };
session->parser = (http_parser_t){ 0, 0, 0, 0, 0, 0 };
session->stream = (hs_stream_t){ NULL, 0, 0, 0, 0, 0, {0, 0, 0}, 0 };
//memset(&session->parser, 0, sizeof(http_parser_t));
//memset(&session->stream ,0,sizeof(hs_stream_t));
if (session->tokens.buf) {
free(session->tokens.buf);
session->tokens.buf = NULL;
}
http_token_dyn_init(&session->tokens, 32);
}
void hs_end_session(http_request_t* session) {
hs_delete_events(session);
close(session->socket);
hs_free_buffer(session);
free(session->tokens.buf);
session->tokens.buf = NULL;
free(session);
}
void hs_reset_timeout(http_request_t* request, int time) {
request->timeout = time;
}
void hs_read_and_process_request(http_request_t* request);
void hs_write_response(http_request_t* request) {
if (!hs_write_client_socket(request)) {
HTTP_FLAG_SET(request->flags, HTTP_END_SESSION);
return;
}
if (request->stream.total_bytes != request->stream.length) {
// All bytes of the body were not written and we need to wait until the
// socket is writable again to complete the write
hs_add_write_event(request);
request->state = HTTP_SESSION_WRITE;
hs_reset_timeout(request, HTTP_REQUEST_TIMEOUT);
} else if (HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
// All bytes of the chunk were written and we need to get the next chunk
// from the application.
request->state = HTTP_SESSION_WRITE;
hs_reset_timeout(request, HTTP_REQUEST_TIMEOUT);
hs_free_buffer(request);
request->chunk_cb(request);
} else {
if (HTTP_FLAG_CHECK(request->flags, HTTP_KEEP_ALIVE)) {
request->state = HTTP_SESSION_INIT;
hs_free_buffer(request);
hs_reset_timeout(request, HTTP_KEEP_ALIVE_TIMEOUT);
} else {
HTTP_FLAG_SET(request->flags, HTTP_END_SESSION);
}
}
}
void hs_error_response(http_request_t* request, int code, char const * message) {
struct http_response_s* response = http_response_init();
http_response_status(response, code);
http_response_header(response, "Content-Type", "text/plain");
http_response_body(response, message, strlen(message));
http_respond(request, response);
hs_write_response(request);
}
void hs_read_and_process_request(http_request_t* request) {
request->state = HTTP_SESSION_READ;
http_token_t token = {0, 0, 0};
hs_reset_timeout(request, HTTP_REQUEST_TIMEOUT);
int rc = hs_stream_read_socket(&request->stream, request->socket, &request->server->memused);
if (rc == 0) {
HTTP_FLAG_SET(request->flags, HTTP_END_SESSION);
return;
}
do {
token = http_parse(&request->parser, &request->stream);
if (token.type != HS_TOK_NONE) http_token_dyn_push(&request->tokens, token);
switch (token.type) {
case HS_TOK_ERROR:
hs_error_response(request, 400, "Bad Request");
break;
case HS_TOK_BODY:
case HS_TOK_BODY_STREAM:
if (token.type == HS_TOK_BODY_STREAM) {
HTTP_FLAG_SET(request->flags, HTTP_FLG_STREAMED);
}
request->state = HTTP_SESSION_NOP;
request->server->request_handler(request);
break;
case HS_TOK_CHUNK_BODY:
request->state = HTTP_SESSION_NOP;
request->chunk_cb(request);
break;
}
} while (token.type != HS_TOK_NONE && request->state == HTTP_SESSION_READ);
}
// Application requesting next chunk of request body.
void http_request_read_chunk(
struct http_request_s* request,
void (*chunk_cb)(struct http_request_s*)
) {
request->chunk_cb = chunk_cb;
hs_read_and_process_request(request);
}
// This is the heart of the request logic. This is the state machine that
// controls what happens when an IO event is received.
void http_session(http_request_t* request) {
switch (request->state) {
case HTTP_SESSION_INIT:
hs_init_session(request);
request->state = HTTP_SESSION_READ;
if (request->server->memused > HTTP_MAX_TOTAL_EST_MEM_USAGE) {
return hs_error_response(request, 503, "Service Unavailable");
}
// fallthrough
case HTTP_SESSION_READ:
hs_read_and_process_request(request);
break;
case HTTP_SESSION_WRITE:
hs_write_response(request);
break;
}
if (HTTP_FLAG_CHECK(request->flags, HTTP_END_SESSION)) {
hs_end_session(request);
}
}
void hs_accept_connections(http_server_t* server) {
int sock = 0;
do {
sock = accept(server->socket, (struct sockaddr *)&server->addr, &server->len);
if (sock > 0) {
http_request_t* session = (http_request_t*)calloc(1, sizeof(http_request_t));
assert(session != NULL);
session->socket = sock;
session->server = server;
session->timeout = HTTP_REQUEST_TIMEOUT;
session->handler = hs_session_io_cb;
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
hs_add_events(session);
http_session(session);
}
} while (sock > 0);
}
void hs_generate_date_time(char* datetime) {
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = gmtime(&rawtime);
strftime(datetime, 32, "%a, %d %b %Y %T GMT", timeinfo);
}
http_server_t* http_server_init(int port, void (*handler)(http_request_t*)) {
http_server_t* serv = (http_server_t*)malloc(sizeof(http_server_t));
assert(serv != NULL);
serv->port = port;
serv->memused = 0;
serv->handler = hs_server_listen_cb;
hs_server_init(serv);
hs_generate_date_time(serv->date);
serv->request_handler = handler;
return serv;
}
void http_server_set_userdata(struct http_server_s* serv, void* data) {
serv->data = data;
}
void http_listen(http_server_t* serv, const char* ipaddr) {
// Ignore SIGPIPE. We handle these errors at the call site.
signal(SIGPIPE, SIG_IGN);
serv->socket = socket(AF_INET, SOCK_STREAM, 0);
int flag = 1;
setsockopt(serv->socket, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag));
hs_bind_localhost(serv->socket, &serv->addr, ipaddr, serv->port);
serv->len = sizeof(serv->addr);
int flags = fcntl(serv->socket, F_GETFL, 0);
fcntl(serv->socket, F_SETFL, flags | O_NONBLOCK);
listen(serv->socket, 128);
hs_add_server_sock_events(serv);
}
int http_server_listen_addr_poll(http_server_t* serv, const char* ipaddr) {
http_listen(serv, ipaddr);
return 0;
}
int http_server_listen_poll(http_server_t* serv) {
return http_server_listen_addr_poll(serv, NULL);
}
int http_server_loop(http_server_t* server) {
return server->loop;
}
// *** http request ***
http_string_t http_get_token_string(http_request_t* request, int token_type) {
http_string_t str = {0, 0};
if (request->tokens.buf == NULL) return str;
for (int i = 0; i < request->tokens.size; i++) {
http_token_t token = request->tokens.buf[i];
if (token.type == token_type) {
str.buf = &request->stream.buf[token.index];
str.len = token.len;
return str;
}
}
return str;
}
int http_request_has_flag(http_request_t* request, int flag) {
return HTTP_FLAG_CHECK(request->flags, flag);
}
int hs_case_insensitive_cmp(char const * a, char const * b, int len) {
for (int i = 0; i < len; i++) {
char c1 = a[i] >= 'A' && a[i] <= 'Z' ? a[i] + 32 : a[i];
char c2 = b[i] >= 'A' && b[i] <= 'Z' ? b[i] + 32 : b[i];
if (c1 != c2) return 0;
}
return 1;
}
http_string_t http_request_method(http_request_t* request) {
return http_get_token_string(request, HS_TOK_METHOD);
}
http_string_t http_request_target(http_request_t* request) {
return http_get_token_string(request, HS_TOK_TARGET);
}
http_string_t http_request_body(http_request_t* request) {
return http_get_token_string(request, HS_TOK_BODY);
}
int hs_assign_iteration_headers(
http_request_t* request,
http_string_t* key,
http_string_t* val,
int* iter
) {
http_token_t token = request->tokens.buf[*iter];
if (request->tokens.buf[*iter].type == HS_TOK_BODY) return 0;
*key = (http_string_t) {
.buf = &request->stream.buf[token.index],
.len = token.len
};
(*iter)++;
token = request->tokens.buf[*iter];
*val = (http_string_t) {
.buf = &request->stream.buf[token.index],
.len = token.len
};
return 1;
}
int http_request_iterate_headers(
http_request_t* request,
http_string_t* key,
http_string_t* val,
int* iter
) {
if (*iter == 0) {
for ( ; *iter < request->tokens.size; (*iter)++) {
http_token_t token = request->tokens.buf[*iter];
if (token.type == HS_TOK_HEADER_KEY) {
return hs_assign_iteration_headers(request, key, val, iter);
}
}
return 0;
} else {
(*iter)++;
return hs_assign_iteration_headers(request, key, val, iter);
}
}
http_string_t http_request_header(http_request_t* request, char const * key) {
int len = strlen(key);
for (int i = 0; i < request->tokens.size; i++) {
http_token_t token = request->tokens.buf[i];
if (token.type == HS_TOK_HEADER_KEY && token.len == len) {
if (hs_case_insensitive_cmp(&request->stream.buf[token.index], key, len)) {
token = request->tokens.buf[i + 1];
return (http_string_t) {
.buf = &request->stream.buf[token.index],
.len = token.len
};
}
}
}
return (http_string_t) {NULL, 0};
}
void http_request_free_buffer(http_request_t* request) {
hs_free_buffer(request);
}
void* http_request_userdata(http_request_t* request) {
return request->data;
}
void http_request_set_userdata(http_request_t* request, void* data) {
request->data = data;
}
void* http_request_server_userdata(struct http_request_s* request) {
return request->server->data;
}
void hs_auto_detect_keep_alive(http_request_t* request) {
http_string_t str = http_get_token_string(request, HS_TOK_VERSION);
if (str.buf == NULL) return;
int version = str.buf[str.len - 1] == '1';
str = http_request_header(request, "Connection");
if (
(str.len == 5 && hs_case_insensitive_cmp(str.buf, "close", 5)) ||
(str.len == 0 && version == HTTP_1_0)
) {
HTTP_FLAG_CLEAR(request->flags, HTTP_KEEP_ALIVE);
} else {
HTTP_FLAG_SET(request->flags, HTTP_KEEP_ALIVE);
}
}
void http_request_connection(http_request_t* request, int directive) {
if (directive == HTTP_KEEP_ALIVE) {
HTTP_FLAG_CLEAR(request->flags, HTTP_AUTOMATIC);
HTTP_FLAG_SET(request->flags, HTTP_KEEP_ALIVE);
} else if (directive == HTTP_CLOSE) {
HTTP_FLAG_CLEAR(request->flags, HTTP_AUTOMATIC);
HTTP_FLAG_CLEAR(request->flags, HTTP_KEEP_ALIVE);
}
}
http_string_t http_request_chunk(struct http_request_s* request) {
http_token_t token = request->tokens.buf[request->tokens.size - 1];
return (http_string_t) {
.buf = &request->stream.buf[token.index],
.len = token.len
};
}
// *** http response ***
http_response_t* http_response_init() {
http_response_t* response = (http_response_t*)calloc(1, sizeof(http_response_t));
assert(response != NULL);
response->status = 200;
return response;
}
void http_response_header(http_response_t* response, char const * key, char const * value) {
http_header_t* header = (http_header_t*)malloc(sizeof(http_header_t));
assert(header != NULL);
header->key = key;
header->value = value;
http_header_t* prev = response->headers;
header->next = prev;
response->headers = header;
}
void http_response_status(http_response_t* response, int status) {
response->status = status > 599 || status < 100 ? 500 : status;
}
void http_response_body(http_response_t* response, char const * body, int length) {
response->body = body;
response->content_length = length;
}
typedef struct {
char* buf;
int capacity;
int size;
int64_t* memused;
} grwprintf_t;
void grwprintf_init(grwprintf_t* ctx, int capacity, int64_t* memused) {
ctx->memused = memused;
ctx->size = 0;
ctx->buf = (char*)malloc(capacity);
*ctx->memused += capacity;
assert(ctx->buf != NULL);
ctx->capacity = capacity;
}
void grwmemcpy(grwprintf_t* ctx, char const * src, int size) {
if (ctx->size + size > ctx->capacity) {
*ctx->memused -= ctx->capacity;
ctx->capacity = ctx->size + size;
*ctx->memused += ctx->capacity;
ctx->buf = (char*)realloc(ctx->buf, ctx->capacity);
assert(ctx->buf != NULL);
}
memcpy(ctx->buf + ctx->size, src, size);
ctx->size += size;
}
void grwprintf(grwprintf_t* ctx, char const * fmt, ...) {
va_list args;
va_start(args, fmt);
int bytes = vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args);
if (bytes + ctx->size > ctx->capacity) {
*ctx->memused -= ctx->capacity;
while (bytes + ctx->size > ctx->capacity) ctx->capacity *= 2;
*ctx->memused += ctx->capacity;
ctx->buf = (char*)realloc(ctx->buf, ctx->capacity);
assert(ctx->buf != NULL);
bytes += vsnprintf(ctx->buf + ctx->size, ctx->capacity - ctx->size, fmt, args);
}
ctx->size += bytes;
va_end(args);
}
void http_buffer_headers(
http_request_t* request,
http_response_t* response,
grwprintf_t* printctx
) {
http_header_t* header = response->headers;
while (header) {
grwprintf(printctx, "%s: %s\r\n", header->key, header->value);
header = header->next;
}
if (!HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
grwprintf(printctx, "Content-Length: %d\r\n", response->content_length);
}
grwprintf(printctx, "\r\n");
}
void http_respond_headers(
http_request_t* request,
http_response_t* response,
grwprintf_t* printctx
) {
if (HTTP_FLAG_CHECK(request->flags, HTTP_AUTOMATIC)) {
hs_auto_detect_keep_alive(request);
}
if (HTTP_FLAG_CHECK(request->flags, HTTP_KEEP_ALIVE)) {
http_response_header(response, "Connection", "keep-alive");
} else {
http_response_header(response, "Connection", "close");
}
grwprintf(
printctx, "HTTP/1.1 %d %s\r\nDate: %s\r\n",
response->status, hs_status_text[response->status], request->server->date
);
http_buffer_headers(request, response, printctx);
}
void http_end_response(http_request_t* request, http_response_t* response, grwprintf_t* printctx) {
http_header_t* header = response->headers;
while (header) {
http_header_t* tmp = header;
header = tmp->next;
free(tmp);
}
hs_free_buffer(request);
free(response);
request->stream.buf = printctx->buf;
request->stream.total_bytes = 0;
request->stream.length = printctx->size;
request->stream.capacity = printctx->capacity;
request->state = HTTP_SESSION_WRITE;
hs_write_response(request);
}
void http_respond(http_request_t* request, http_response_t* response) {
grwprintf_t printctx;
grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
http_respond_headers(request, response, &printctx);
if (response->body) {
grwmemcpy(&printctx, response->body, response->content_length);
}
http_end_response(request, response, &printctx);
}
void http_respond_chunk(
http_request_t* request,
http_response_t* response,
void (*cb)(http_request_t*)
) {
grwprintf_t printctx;
grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
if (!HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
HTTP_FLAG_SET(request->flags, HTTP_CHUNKED_RESPONSE);
http_response_header(response, "Transfer-Encoding", "chunked");
http_respond_headers(request, response, &printctx);
}
request->chunk_cb = cb;
grwprintf(&printctx, "%X\r\n", response->content_length);
grwmemcpy(&printctx, response->body, response->content_length);
grwprintf(&printctx, "\r\n");
http_end_response(request, response, &printctx);
}
void http_respond_chunk_end(http_request_t* request, http_response_t* response) {
grwprintf_t printctx;
grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
grwprintf(&printctx, "0\r\n");
http_buffer_headers(request, response, &printctx);
grwprintf(&printctx, "\r\n");
HTTP_FLAG_CLEAR(request->flags, HTTP_CHUNKED_RESPONSE);
http_end_response(request, response, &printctx);
}
// *** kqueue platform specific ***
#ifdef KQUEUE
void hs_server_listen_cb(struct kevent* ev) {
http_server_t* server = (http_server_t*)ev->udata;
if (ev->filter == EVFILT_TIMER) {
hs_generate_date_time(server->date);
} else {
hs_accept_connections(server);
}
}
void hs_session_io_cb(struct kevent* ev) {
http_request_t* request = (http_request_t*)ev->udata;
if (ev->filter == EVFILT_TIMER) {
request->timeout -= 1;
if (request->timeout == 0) hs_end_session(request);
} else {
http_session(request);
}
}
void hs_server_init(http_server_t* serv) {
serv->loop = kqueue();
struct kevent ev_set;
EV_SET(&ev_set, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 1000, serv);
kevent(serv->loop, &ev_set, 1, NULL, 0, NULL);
}
void hs_add_server_sock_events(http_server_t* serv) {
struct kevent ev_set;
EV_SET(&ev_set, serv->socket, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, serv);
kevent(serv->loop, &ev_set, 1, NULL, 0, NULL);
}
int http_server_listen_addr(http_server_t* serv, const char* ipaddr) {
http_listen(serv, ipaddr);
struct kevent ev_list[1];
while (1) {
int nev = kevent(serv->loop, NULL, 0, ev_list, 1, NULL);
for (int i = 0; i < nev; i++) {
ev_cb_t* ev_cb = (ev_cb_t*)ev_list[i].udata;
ev_cb->handler(&ev_list[i]);
}
}
return 0;
}
int http_server_listen(http_server_t* serv) {
return http_server_listen_addr(serv, NULL);
}
void hs_delete_events(http_request_t* request) {
struct kevent ev_set;
EV_SET(&ev_set, request->socket, EVFILT_TIMER, EV_DELETE, 0, 0, request);
kevent(request->server->loop, &ev_set, 1, NULL, 0, NULL);
}
int http_server_poll(http_server_t* serv) {
struct kevent ev;
struct timespec ts = {0, 0};
int nev = kevent(serv->loop, NULL, 0, &ev, 1, &ts);
if (nev <= 0) return nev;
ev_cb_t* ev_cb = (ev_cb_t*)ev.udata;
ev_cb->handler(&ev);
return nev;
}
void hs_add_events(http_request_t* request) {
struct kevent ev_set[2];
EV_SET(&ev_set[0], request->socket, EVFILT_READ, EV_ADD, 0, 0, request);
EV_SET(&ev_set[1], request->socket, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 1000, request);
kevent(request->server->loop, ev_set, 2, NULL, 0, NULL);
}
void hs_add_write_event(http_request_t* request) {
struct kevent ev_set[2];
EV_SET(&ev_set[0], request->socket, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, request);
kevent(request->server->loop, ev_set, 2, NULL, 0, NULL);
}
#else
// *** epoll platform specific ***
void hs_server_listen_cb(struct epoll_event* ev) {
hs_accept_connections((http_server_t*)ev->data.ptr);
}
void hs_session_io_cb(struct epoll_event* ev) {
http_session((http_request_t*)ev->data.ptr);
}
void hs_server_timer_cb(struct epoll_event* ev) {
http_server_t* server = (http_server_t*)((char*)ev->data.ptr - sizeof(epoll_cb_t));
uint64_t res;
int bytes = read(server->timerfd, &res, sizeof(res));
(void)bytes; // suppress warning
hs_generate_date_time(server->date);
}
void hs_request_timer_cb(struct epoll_event* ev) {
http_request_t* request = (http_request_t*)((char*)ev->data.ptr - sizeof(epoll_cb_t));
uint64_t res;
int bytes = read(request->timerfd, &res, sizeof(res));
(void)bytes; // suppress warning
request->timeout -= 1;
if (request->timeout == 0) hs_end_session(request);
}
void hs_add_server_sock_events(http_server_t* serv) {
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = serv;
epoll_ctl(serv->loop, EPOLL_CTL_ADD, serv->socket, &ev);
}
void hs_server_init(http_server_t* serv) {
serv->loop = epoll_create1(0);
serv->timer_handler = hs_server_timer_cb;
int tfd = timerfd_create(CLOCK_MONOTONIC, 0);
struct itimerspec ts = {};
ts.it_value.tv_sec = 1;
ts.it_interval.tv_sec = 1;
timerfd_settime(tfd, 0, &ts, NULL);
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = &serv->timer_handler;
epoll_ctl(serv->loop, EPOLL_CTL_ADD, tfd, &ev);
serv->timerfd = tfd;
}
int http_server_listen_addr(http_server_t* serv, const char* ipaddr) {
http_listen(serv, ipaddr);
struct epoll_event ev_list[1];
while (1) {
int nev = epoll_wait(serv->loop, ev_list, 1, -1);
for (int i = 0; i < nev; i++) {
ev_cb_t* ev_cb = (ev_cb_t*)ev_list[i].data.ptr;
ev_cb->handler(&ev_list[i]);
}
}
return 0;
}
int http_server_listen(http_server_t* serv) {
return http_server_listen_addr(serv, NULL);
}
void hs_delete_events(http_request_t* request) {
epoll_ctl(request->server->loop, EPOLL_CTL_DEL, request->socket, NULL);
epoll_ctl(request->server->loop, EPOLL_CTL_DEL, request->timerfd, NULL);
close(request->timerfd);
}
int http_server_poll(http_server_t* serv) {
struct epoll_event ev;
int nev = epoll_wait(serv->loop, &ev, 1, 0);
if (nev <= 0) return nev;
ev_cb_t* ev_cb = (ev_cb_t*)ev.data.ptr;
ev_cb->handler(&ev);
return nev;
}
void hs_add_events(http_request_t* request) {
request->timer_handler = hs_request_timer_cb;
// Watch for read events
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = request;
epoll_ctl(request->server->loop, EPOLL_CTL_ADD, request->socket, &ev);
// Add timer to timeout requests.
int tfd = timerfd_create(CLOCK_MONOTONIC, 0);
struct itimerspec ts = {};
ts.it_value.tv_sec = 1;
ts.it_interval.tv_sec = 1;
timerfd_settime(tfd, 0, &ts, NULL);
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = &request->timer_handler;
epoll_ctl(request->server->loop, EPOLL_CTL_ADD, tfd, &ev);
request->timerfd = tfd;
}
void hs_add_write_event(http_request_t* request) {
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
ev.data.ptr = request;
epoll_ctl(request->server->loop, EPOLL_CTL_MOD, request->socket, &ev);
}
#endif
#endif
#endif
|