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
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
|
// httpserver.h has been automatically generated from httpserver.m4 and the
// source files under /src
#ifndef HTTPSERVER_H
#define HTTPSERVER_H
#line 1 "api.h"
#ifndef HS_API_H
#define HS_API_H
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* @file api.h
*
* 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.9.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.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef __cplusplus
extern "C" {
#endif
// String type used to read the request details. The char pointer is NOT null
// terminated.
struct http_string_s;
struct http_server_s;
struct http_request_s;
struct http_response_s;
/**
* Get the event loop descriptor 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.
*
* @param server The server.
*
* @return The descriptor of the event loop.
*/
int http_server_loop(struct http_server_s *server);
/**
* Allocates and initializes the http server.
*
* @param port The port to listen on.
* @param handler The callback that will fire to handle requests.
*
* @return Pointer to the allocated server.
*/
struct http_server_s *
http_server_init(int port, void (*handler)(struct http_request_s *));
/**
* Listens on the server socket and starts an event loop.
*
* During normal operation this function will not return.
*
* @param server The server.
* @param ipaddr The ip to bind to if NULL binds to all interfaces.
*
* @return Error code if the server fails.
*/
int http_server_listen_addr(struct http_server_s *server, const char *ipaddr);
/**
* See http_server_listen_addr
*/
int http_server_listen(struct http_server_s *server);
/**
* Poll the server socket on specific interface.
*
* 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.
*
* @param server The server.
* @param ipaddr The ip to bind to if NULL bind to all.
*
* @return Error code if the poll fails.
*/
int http_server_listen_addr_poll(struct http_server_s *server,
const char *ipaddr);
/**
* Poll the server socket on all interfaces. See http_server_listen_addr_poll
*
* @param server The server.
*
* @return Error code if the poll fails.
*/
int http_server_listen_poll(struct http_server_s *server);
/**
* Poll of the request sockets.
*
* Call this function in your update loop. It will trigger the request handler
* once if there is a request ready. It should be called in a loop until it
* returns 0.
*
* @param server The server.
*
* @return Returns 1 if a request was handled and 0 if no requests were handled.
*/
int http_server_poll(struct http_server_s *server);
/**
* Check if a request flag is set.
*
* The flags that can be queried are listed below:
*
* HTTP_FLG_STREAMED
*
* 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.
*
* @param request The request.
* @param flag One of the flags listed above.
*
* @return 1 or 0 if the flag is set or not respectively.
*/
int http_request_has_flag(struct http_request_s *request, int flag);
/**
* Returns the request method as it was read from the HTTP request line.
*
* @param request The request.
*
* @return The HTTP method.
*/
struct http_string_s http_request_method(struct http_request_s *request);
/**
* Returns the full request target (url) from the HTTP request line.
*
* @param request The request.
*
* @return The target.
*/
struct http_string_s http_request_target(struct http_request_s *request);
/**
* Retrieves the request body.
*
* @param request The request.
*
* @return 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.
*
* @param request The request.
* @param key The case insensitive header key to search for.
*
* @return The value for the header matching the key. Will be length 0 if not
* found.
*/
struct http_string_s http_request_header(struct http_request_s *request,
char const *key);
/**
* Iterate over the request headers.
*
* Each call will set key and val to the key and value of the next header.
*
* @param request The request.
* @param[out] key The key of the header.
* @param[out] value The key of the header.
* @param[inout] iter Should be initialized to 0 before calling. Pass back in
* with each consecutive call.
*
* @return 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);
/**
* Stores an arbitrary userdata pointer for this request.
*
* This is not used by the library in any way and is strictly for you, the
* application programmer to make use of.
*
* @param request The request.
* @param data Opaque pointer to user data.
*/
void http_request_set_userdata(struct http_request_s *request, void *data);
/**
* Retrieve the opaque data pointer that was set with http_request_set_userdata.
*
* @param request The request.
*/
void *http_request_userdata(struct http_request_s *request);
/**
* Stores a server wide opaque 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.
*
* @param server The server.
* @param data Opaque data pointer.
*/
void http_server_set_userdata(struct http_server_s *server, void *data);
/**
* Retrieve the server wide userdata pointer.
*
* @param request The request.
*/
void *http_request_server_userdata(struct http_request_s *request);
/**
* Sets how the request will handle it's connection
*
* 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.
*
* @param request The request.
* @param directive One of HTTP_KEEP_ALIVE or HTTP_CLOSE
*/
void http_request_connection(struct http_request_s *request, int directive);
/**
* Frees the buffer of a request.
*
* 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.
*
* @param request The request to free the buffer of.
*/
void http_request_free_buffer(struct http_request_s *request);
/**
* Allocates an http response.
*
* This memory will be freed when http_respond is called.
*
* @return Allocated response.
*/
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.
*
* @param response The response struct to set status on.
* @param status The HTTP status code.
*/
void http_response_status(struct http_response_s *response, int status);
/**
* Sets an HTTP response header.
*
* @param response The response struct to set the header on.
* @param key The null-terminated key of the header eg: Content-Type
* @param value The null-terminated value of the header eg: application/json
*/
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. If responding with chunked transfer encoding
* this will become a single chunk. This procedure can be used again to set
* subsequent chunks.
*
* @param response The response struct to set the body for.
* @param body The body of the response.
* @param length The length of the body
*/
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. Adds the default HTTP response headers, Date and
* Connection.
*
* @param request The request to respond to.
* @param response The response to respond with.
*/
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. Transfer-Encoding header
* will automatically be set to chunked. Headers set for subsequent calls will
* be ignored.
*
* @param request The request to respond to.
* @param response The response to respond with.
* @param notify_done The callback that's used to signal user code that another
* chunk is ready to be written out.
*/
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.
*
* Used to signal end of transmission on chunked requests. Any headers set
* before this call will be included as what the HTTP spec refers to as
* 'trailers' which are essentially more response headers.
*
* @param request The request to respond to.
* @param response The response to respond with.
*/
void http_respond_chunk_end(struct http_request_s *request,
struct http_response_s *response);
/**
* Read a chunk of the request body.
*
* 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.
*
* @param request The request.
* @param chunk_cb Callback for when the chunk is ready.
*/
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'.
*
* @param request The request.
*
* @return The chunk data.
*/
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
#line 1 "common.h"
#ifndef HS_COMMON_H
#define HS_COMMON_H
// http session states
#define HTTP_SESSION_INIT 0
#define HTTP_SESSION_READ 1
#define HTTP_SESSION_WRITE 2
#define HTTP_SESSION_NOP 3
#define HTTP_REQUEST_TIMEOUT 20
#define HTTP_FLAG_SET(var, flag) var |= flag
#define HTTP_FLAG_CLEAR(var, flag) var &= ~flag
#define HTTP_FLAG_CHECK(var, flag) (var & flag)
#define HTTP_AUTOMATIC 0x8
#define HTTP_CHUNKED_RESPONSE 0x20
#define HTTP_KEEP_ALIVE 1
#define HTTP_CLOSE 0
#include <arpa/inet.h>
#include <sys/socket.h>
#ifdef KQUEUE
#include <sys/event.h>
#else
#include <sys/epoll.h>
#endif
#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;
struct hsh_buffer_s {
char *buf;
int32_t capacity;
int32_t length;
int32_t index;
int32_t after_headers_index;
int8_t sequence_id;
};
enum hsh_token_e {
HSH_TOK_METHOD,
HSH_TOK_TARGET,
HSH_TOK_VERSION,
HSH_TOK_HEADER_KEY,
HSH_TOK_HEADER_VALUE,
HSH_TOK_HEADERS_DONE,
HSH_TOK_BODY,
HSH_TOK_NONE,
HSH_TOK_ERR
};
struct hsh_token_s {
enum hsh_token_e type;
uint8_t flags;
int len;
int index;
};
struct hsh_parser_s {
int64_t content_length;
int64_t content_remaining;
struct hsh_token_s token;
int16_t limit_count;
int16_t limit_max;
int8_t state;
int8_t flags;
int8_t sequence_id;
};
struct hs_token_array_s {
struct hsh_token_s *buf;
int capacity;
int size;
};
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;
struct hsh_buffer_s buffer;
struct hsh_parser_s parser;
struct hs_token_array_s tokens;
int state;
int socket;
int timeout;
int64_t bytes_written;
struct http_server_s *server;
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;
#endif
#line 1 "buffer_util.h"
#ifndef HS_BUFFER_UTIL_H
#define HS_BUFFER_UTIL_H
#include <stdlib.h>
#ifndef HTTPSERVER_IMPL
// #include "common.h"
#endif
static inline void _hs_buffer_free(struct hsh_buffer_s *buffer,
int64_t *memused) {
if (buffer->buf) {
free(buffer->buf);
*memused -= buffer->capacity;
buffer->buf = NULL;
}
}
#endif
#line 1 "request_util.h"
#ifndef HS_REQUEST_UTIL_H
#define HS_REQUEST_UTIL_H
// #include "common.h"
// http version indicators
#define HTTP_1_0 0
#define HTTP_1_1 1
struct http_string_s {
char const *buf;
int len;
};
typedef struct http_string_s http_string_t;
http_string_t hs_get_token_string(http_request_t *request,
enum hsh_token_e token_type);
http_string_t hs_request_header(http_request_t *request, char const *key);
void hs_request_detect_keep_alive_flag(http_request_t *request);
int hs_request_iterate_headers(http_request_t *request, http_string_t *key,
http_string_t *val, int *iter);
void hs_request_set_keep_alive_flag(http_request_t *request, int directive);
http_string_t hs_request_chunk(struct http_request_s *request);
#endif
#line 1 "parser.h"
#ifndef HTTP_PARSER_H
#define HTTP_PARSER_H
// HSH_TOK_HEADERS_DONE flags
#define HSH_TOK_FLAG_NO_BODY 0x1
#define HSH_TOK_FLAG_STREAMED_BODY 0x2
// HSH_TOK_BODY flags
#define HSH_TOK_FLAG_BODY_FINAL 0x1
#define HSH_TOK_FLAG_SMALL_BODY 0x2
struct hsh_token_s hsh_parser_exec(struct hsh_parser_s *parser,
struct hsh_buffer_s *buffer,
int max_buf_capacity);
void hsh_parser_init(struct hsh_parser_s *parser);
#endif
#line 1 "read_socket.h"
#ifndef HS_READ_SOCKET_H
#define HS_READ_SOCKET_H
#define HTTP_FLG_STREAMED 0x1
#include <stdint.h>
struct http_request_s;
// Response code for hs_read_socket
enum hs_read_rc_e {
// Execution was successful
HS_READ_RC_SUCCESS,
// There was an error parsing the HTTP request
HS_READ_RC_PARSE_ERR,
// There was an error reading the socket
HS_READ_RC_SOCKET_ERR
};
// Holds configuration options for the hs_read_socket procedure.
struct hs_read_opts_s {
// Restricts the request buffer from ever growing larger than this size
int64_t max_request_buf_capacity;
// The value to be compared to the return of the read call to determine if
// the connection has been closed. Should generally be 0 in normal operation
// using sockets but can be useful to change if you want to use files instead
// of sockets for testing.
int eof_rc;
// The initial capacity that is allocated for the request buffer
int initial_request_buf_capacity;
};
enum hs_read_rc_e
hs_read_request_and_exec_user_cb(struct http_request_s *request,
struct hs_read_opts_s opts);
#endif
#line 1 "respond.h"
#ifndef HS_RESPOND_H
#define HS_RESPOND_H
#define HTTP_RESPONSE_BUF_SIZE 1024
struct http_request_s;
typedef void (*hs_req_fn_t)(struct http_request_s *);
// Represents a single header of an HTTP response.
typedef struct http_header_s {
// The key of the header eg: Content-Type
char const *key;
// The value of the header eg: application/json
char const *value;
// Pointer to the next header in the linked list.
struct http_header_s *next;
} http_header_t;
// Represents the response of an HTTP request before it is serialized on the
// wire.
typedef struct http_response_s {
// Head of the linked list of response headers
http_header_t *headers;
// The complete body of the response or the chunk if generating a chunked
// response.
char const *body;
// The length of the body or chunk.
int content_length;
// The HTTP status code for the response.
int status;
} http_response_t;
http_response_t *hs_response_init();
void hs_response_set_header(http_response_t *response, char const *key,
char const *value);
void hs_response_set_status(http_response_t *response, int status);
void hs_response_set_body(http_response_t *response, char const *body,
int length);
void hs_request_respond(struct http_request_s *request,
http_response_t *response, hs_req_fn_t http_write);
void hs_request_respond_chunk(struct http_request_s *request,
http_response_t *response, hs_req_fn_t cb,
hs_req_fn_t http_write);
void hs_request_respond_chunk_end(struct http_request_s *request,
http_response_t *response,
hs_req_fn_t http_write);
void hs_request_respond_error(struct http_request_s *request, int code,
char const *message, hs_req_fn_t http_write);
#endif
#line 1 "server.h"
#ifndef HS_SERVER_H
#define HS_SERVER_H
#ifdef KQUEUE
struct kevent;
typedef void (*hs_evt_cb_t)(struct kevent *ev);
#else
struct epoll_event;
typedef void (*hs_evt_cb_t)(struct epoll_event *ev);
#endif
struct http_request_s;
struct http_server_s;
void hs_server_listen_on_addr(struct http_server_s *serv, const char *ipaddr);
int hs_server_run_event_loop(struct http_server_s *serv, const char *ipaddr);
void hs_generate_date_time(char *datetime);
struct http_server_s *hs_server_init(int port,
void (*handler)(struct http_request_s *),
hs_evt_cb_t accept_cb,
hs_evt_cb_t timer_cb);
int hs_server_poll_events(struct http_server_s *serv);
#endif
#line 1 "write_socket.h"
#ifndef HS_WRITE_SOCKET_H
#define HS_WRITE_SOCKET_H
#define HTTP_KEEP_ALIVE_TIMEOUT 120
struct http_request_s;
// Response code for hs_write_socket
enum hs_write_rc_e {
// Successful and has written the full response
HS_WRITE_RC_SUCCESS,
// Successful and has written the full chunk
HS_WRITE_RC_SUCCESS_CHUNK,
// Successful, has written the full response and the socket should be closed
HS_WRITE_RC_SUCCESS_CLOSE,
// Successful but has not written the full response, wait for write ready
HS_WRITE_RC_CONTINUE,
// Error writing to the socket
HS_WRITE_RC_SOCKET_ERR
};
enum hs_write_rc_e hs_write_socket(struct http_request_s *request);
#endif
#line 1 "connection.h"
#ifndef HS_CONNECTION_H
#define HS_CONNECTION_H
// Forward declarations
struct http_request_s;
struct http_server_s;
#ifdef KQUEUE
struct kevent;
typedef void (*hs_io_cb_t)(struct kevent *ev);
#else
struct epoll_event;
typedef void (*hs_io_cb_t)(struct epoll_event *ev);
#endif
/* Closes the requests socket and frees its resources.
*
* Removes all event watchers from the request socket and frees any allocated
* buffers associated with the request struct.
*
* @param request The request to close
*/
void hs_request_terminate_connection(struct http_request_s *request);
/* Accepts connections on the server socket in a loop until it would block.
*
* When a connection is accepted a request struct is allocated and initialized
* and the request socket is set to non-blocking mode. Event watchers are set
* on the socket to call io_cb with a read/write ready event occurs. If the
* server has reached max_mem_usage the err_responder function is called to
* handle the issue.
*
* @param server The http server struct.
* @param io_cb The callback function to respond to events on the request socket
* @param epoll_timer_cb The callback function to respond to timer events for
* epoll. Can be NULL if not using epoll.
* @param err_responder The procedure to call when memory usage has reached the
* given limit. Typically this could respond with a 503 error and close the
* connection.
* @param max_mem_usage The limit at which err_responder should be called
* instead of regular operation.
*/
struct http_request_s *hs_server_accept_connection(struct http_server_s *server,
hs_io_cb_t io_cb,
hs_io_cb_t epoll_timer_cb);
#endif
#line 1 "io_events.h"
#ifndef HS_IO_EVENTS_H
#define HS_IO_EVENTS_H
#define HTTP_REQUEST_BUF_SIZE 1024
#define HTTP_MAX_REQUEST_BUF_SIZE 8388608 // 8mb
#define HTTP_MAX_TOTAL_EST_MEM_USAGE 4294967296 // 4gb
struct http_request_s;
void hs_request_begin_write(struct http_request_s *request);
void hs_request_begin_read(struct http_request_s *request);
#ifdef KQUEUE
struct kevent;
void hs_on_kqueue_server_event(struct kevent *ev);
#else
struct epoll_event;
void hs_on_epoll_server_connection_event(struct epoll_event *ev);
void hs_on_epoll_server_timer_event(struct epoll_event *ev);
#endif
#endif
#ifdef HTTPSERVER_IMPL
#ifndef HTTPSERVER_IMPL_ONCE
#define HTTPSERVER_IMPL_ONCE
#line 1 "api.c"
#include <stdlib.h>
#ifndef HTTPSERVER_IMPL
#include "api.h"
#include "buffer_util.h"
#include "common.h"
#include "io_events.h"
#include "request_util.h"
#include "respond.h"
#include "server.h"
#endif
int http_request_has_flag(http_request_t *request, int flag) {
return HTTP_FLAG_CHECK(request->flags, flag);
}
int http_server_loop(http_server_t *server) { return server->loop; }
http_server_t *http_server_init(int port, void (*handler)(http_request_t *)) {
#ifdef KQUEUE
return hs_server_init(port, handler, hs_on_kqueue_server_event, NULL);
#else
return hs_server_init(port, handler, hs_on_epoll_server_connection_event,
hs_on_epoll_server_timer_event);
#endif
}
void http_request_free_buffer(http_request_t *request) {
_hs_buffer_free(&request->buffer, &request->server->memused);
}
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_server_set_userdata(struct http_server_s *serv, void *data) {
serv->data = data;
}
void *http_request_server_userdata(struct http_request_s *request) {
return request->server->data;
}
int http_request_iterate_headers(http_request_t *request, http_string_t *key,
http_string_t *val, int *iter) {
return hs_request_iterate_headers(request, key, val, iter);
}
http_string_t http_request_header(http_request_t *request, char const *key) {
return hs_request_header(request, key);
}
void http_request_connection(http_request_t *request, int directive) {
hs_request_set_keep_alive_flag(request, directive);
}
http_string_t http_request_chunk(struct http_request_s *request) {
return hs_request_chunk(request);
}
http_response_t *http_response_init() { return hs_response_init(); }
void http_response_header(http_response_t *response, char const *key,
char const *value) {
return hs_response_set_header(response, key, value);
}
void http_response_status(http_response_t *response, int status) {
hs_response_set_status(response, status);
}
void http_response_body(http_response_t *response, char const *body,
int length) {
hs_response_set_body(response, body, length);
}
void http_respond(http_request_t *request, http_response_t *response) {
hs_request_respond(request, response, hs_request_begin_write);
}
void http_respond_chunk(http_request_t *request, http_response_t *response,
void (*cb)(http_request_t *)) {
hs_request_respond_chunk(request, response, cb, hs_request_begin_write);
}
void http_respond_chunk_end(http_request_t *request,
http_response_t *response) {
hs_request_respond_chunk_end(request, response, hs_request_begin_write);
}
http_string_t http_request_method(http_request_t *request) {
return hs_get_token_string(request, HSH_TOK_METHOD);
}
http_string_t http_request_target(http_request_t *request) {
return hs_get_token_string(request, HSH_TOK_TARGET);
}
http_string_t http_request_body(http_request_t *request) {
return hs_get_token_string(request, HSH_TOK_BODY);
}
int http_server_listen(http_server_t *serv) {
return hs_server_run_event_loop(serv, NULL);
}
int http_server_listen_addr(http_server_t *serv, const char *ipaddr) {
return hs_server_run_event_loop(serv, ipaddr);
}
int http_server_poll(http_server_t *serv) {
return hs_server_poll_events(serv);
}
int http_server_listen_poll(http_server_t *serv) {
hs_server_listen_on_addr(serv, NULL);
return 0;
}
int http_server_listen_addr_poll(http_server_t *serv, const char *ipaddr) {
hs_server_listen_on_addr(serv, ipaddr);
return 0;
}
void http_request_read_chunk(struct http_request_s *request,
void (*chunk_cb)(struct http_request_s *)) {
request->state = HTTP_SESSION_READ;
request->chunk_cb = chunk_cb;
hs_request_begin_read(request);
}
#line 1 "request_util.c"
#include <stdlib.h>
#include <string.h>
#ifndef HTTPSERVER_IMPL
#include "common.h"
#include "request_util.h"
#endif
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 hs_get_token_string(http_request_t *request,
enum hsh_token_e token_type) {
http_string_t str = {0, 0};
if (request->tokens.buf == NULL)
return str;
for (int i = 0; i < request->tokens.size; i++) {
struct hsh_token_s token = request->tokens.buf[i];
if (token.type == token_type) {
str.buf = &request->buffer.buf[token.index];
str.len = token.len;
return str;
}
}
return str;
}
http_string_t hs_request_header(http_request_t *request, char const *key) {
int len = strlen(key);
for (int i = 0; i < request->tokens.size; i++) {
struct hsh_token_s token = request->tokens.buf[i];
if (token.type == HSH_TOK_HEADER_KEY && token.len == len) {
if (_hs_case_insensitive_cmp(&request->buffer.buf[token.index], key,
len)) {
token = request->tokens.buf[i + 1];
return (http_string_t){.buf = &request->buffer.buf[token.index],
.len = token.len};
}
}
}
return (http_string_t){};
}
void hs_request_detect_keep_alive_flag(http_request_t *request) {
http_string_t str = hs_get_token_string(request, HSH_TOK_VERSION);
if (str.buf == NULL)
return;
int version = str.buf[str.len - 1] == '1';
str = hs_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);
}
}
int _hs_get_header_key_val(http_request_t *request, http_string_t *key,
http_string_t *val, int iter) {
struct hsh_token_s token = request->tokens.buf[iter];
if (request->tokens.buf[iter].type == HSH_TOK_HEADERS_DONE)
return 0;
*key = (http_string_t){.buf = &request->buffer.buf[token.index],
.len = token.len};
token = request->tokens.buf[iter + 1];
*val = (http_string_t){.buf = &request->buffer.buf[token.index],
.len = token.len};
return 1;
}
int hs_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)++) {
struct hsh_token_s token = request->tokens.buf[*iter];
if (token.type == HSH_TOK_HEADER_KEY) {
int more = _hs_get_header_key_val(request, key, val, *iter);
(*iter)++;
return more;
}
}
return 0;
} else {
(*iter)++;
int more = _hs_get_header_key_val(request, key, val, *iter);
(*iter)++;
return more;
}
}
void hs_request_set_keep_alive_flag(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 hs_request_chunk(struct http_request_s *request) {
struct hsh_token_s token = request->tokens.buf[request->tokens.size - 1];
return (http_string_t){.buf = &request->buffer.buf[token.index],
.len = token.len};
}
#line 1 "parser.c"
#line 1 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
#include <string.h>
#include <stdlib.h>
#ifndef HTTPSERVER_IMPL
#include "common.h"
#include "parser.h"
#endif
#define HSH_P_FLAG_CHUNKED 0x1
#define HSH_P_FLAG_TOKEN_READY 0x2
#define HSH_P_FLAG_DONE 0x4
#define HSH_ENTER_TOKEN(tok_type, max_len) \
parser->token.type = tok_type; \
parser->token.index = p - buffer->buf; \
parser->token.flags = 0; \
parser->limit_count = 0; \
parser->limit_max = max_len;
#line 232 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
#line 28 "/Users/jeremywilliams/code/httpserver.h/build/src/parser.c"
static const char _hsh_http_actions[] = {
0, 1, 2, 1, 6, 1, 10, 1,
13, 1, 14, 1, 15, 1, 16, 1,
17, 1, 18, 2, 0, 10, 2, 1,
10, 2, 4, 10, 2, 5, 14, 2,
5, 16, 2, 5, 17, 2, 7, 10,
2, 8, 10, 2, 10, 11, 2, 12,
13, 2, 13, 14, 3, 3, 9, 10,
3, 4, 10, 6, 3, 7, 4, 10,
3, 9, 3, 10, 3, 13, 5, 14,
3, 13, 14, 12, 3, 14, 12, 13,
4, 5, 14, 12, 13, 4, 13, 5,
14, 12, 4, 14, 12, 13, 15
};
static const short _hsh_http_key_offsets[] = {
0, 0, 4, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 20, 21, 22,
39, 53, 55, 58, 60, 61, 79, 94,
110, 126, 142, 158, 174, 190, 205, 221,
237, 253, 269, 285, 301, 315, 317, 322,
324, 328, 344, 360, 376, 392, 408, 424,
440, 455, 471, 487, 503, 519, 535, 551,
567, 583, 597, 599, 603, 606, 609, 612,
615, 618, 621, 622, 623, 624, 631, 632,
632, 633, 634, 635, 642, 643, 643, 644,
652, 661, 669, 677, 686, 688, 697, 698,
699, 699, 699, 713, 713, 713, 721, 729,
729, 729
};
static const char _hsh_http_trans_keys[] = {
65, 90, 97, 122, 32, 65, 90, 97,
122, 32, 32, 72, 84, 84, 80, 47,
49, 46, 48, 49, 13, 10, 9, 32,
34, 44, 47, 67, 84, 99, 116, 123,
125, 40, 41, 58, 64, 91, 93, 9,
32, 34, 44, 47, 58, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 9,
13, 32, 10, 13, 10, 9, 13, 32,
34, 44, 47, 67, 84, 99, 116, 123,
125, 40, 41, 58, 64, 91, 93, 9,
10, 32, 34, 44, 47, 58, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 79, 111, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 78, 110, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 84, 116, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 69, 101, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 78, 110, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 47, 58, 84, 116, 123, 125,
40, 41, 59, 64, 91, 93, 9, 32,
34, 44, 45, 47, 58, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 76, 108, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 69, 101, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 78, 110, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 71, 103, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 84, 116, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 72, 104, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 34,
44, 47, 58, 123, 125, 40, 41, 59,
64, 91, 93, 9, 32, 9, 13, 32,
48, 57, 10, 13, 10, 13, 48, 57,
9, 32, 34, 44, 47, 58, 82, 114,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 65, 97,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 78, 110,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 83, 115,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 70, 102,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 69, 101,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 47, 58, 82, 114,
123, 125, 40, 41, 59, 64, 91, 93,
9, 32, 34, 44, 45, 47, 58, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 69, 101, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 78, 110, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 67, 99, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 79, 111, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 68, 100, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 73, 105, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 78, 110, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 71, 103, 123,
125, 40, 41, 59, 64, 91, 93, 9,
32, 34, 44, 47, 58, 123, 125, 40,
41, 59, 64, 91, 93, 9, 32, 9,
13, 32, 99, 10, 13, 104, 10, 13,
117, 10, 13, 110, 10, 13, 107, 10,
13, 101, 10, 13, 100, 13, 10, 48,
13, 48, 57, 65, 70, 97, 102, 10,
13, 10, 48, 13, 48, 57, 65, 70,
97, 102, 10, 48, 13, 48, 49, 57,
65, 70, 97, 102, 10, 13, 48, 49,
57, 65, 70, 97, 102, 13, 48, 49,
57, 65, 70, 97, 102, 13, 48, 49,
57, 65, 70, 97, 102, 10, 13, 48,
49, 57, 65, 70, 97, 102, 13, 48,
10, 13, 48, 49, 57, 65, 70, 97,
102, 13, 10, 9, 32, 34, 44, 47,
58, 123, 125, 40, 41, 59, 64, 91,
93, 13, 48, 49, 57, 65, 70, 97,
102, 13, 48, 49, 57, 65, 70, 97,
102, 0
};
static const char _hsh_http_single_lengths[] = {
0, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 11,
8, 2, 3, 2, 1, 12, 9, 10,
10, 10, 10, 10, 10, 9, 10, 10,
10, 10, 10, 10, 8, 2, 3, 2,
2, 10, 10, 10, 10, 10, 10, 10,
9, 10, 10, 10, 10, 10, 10, 10,
10, 8, 2, 4, 3, 3, 3, 3,
3, 3, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 0, 1, 2,
3, 2, 2, 3, 2, 3, 1, 1,
0, 0, 8, 0, 0, 2, 2, 0,
0, 0
};
static const char _hsh_http_range_lengths[] = {
0, 2, 2, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 3,
3, 0, 0, 0, 0, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 0, 1, 0,
1, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 3, 0, 0,
0, 0, 0, 3, 0, 0, 0, 3,
3, 3, 3, 3, 0, 3, 0, 0,
0, 0, 3, 0, 0, 3, 3, 0,
0, 0
};
static const short _hsh_http_index_offsets[] = {
0, 0, 3, 7, 9, 11, 13, 15,
17, 19, 21, 23, 25, 27, 29, 31,
46, 58, 61, 65, 68, 70, 86, 99,
113, 127, 141, 155, 169, 183, 196, 210,
224, 238, 252, 266, 280, 292, 295, 300,
303, 307, 321, 335, 349, 363, 377, 391,
405, 418, 432, 446, 460, 474, 488, 502,
516, 530, 542, 545, 550, 554, 558, 562,
566, 570, 574, 576, 578, 580, 585, 587,
588, 590, 592, 594, 599, 601, 602, 604,
610, 617, 623, 629, 636, 639, 646, 648,
650, 651, 652, 664, 665, 666, 672, 678,
679, 680
};
static const char _hsh_http_indicies[] = {
1, 1, 0, 2, 3, 3, 0, 0,
4, 6, 5, 7, 0, 8, 0, 9,
0, 10, 0, 11, 0, 12, 0, 13,
0, 14, 0, 15, 0, 16, 0, 0,
0, 0, 0, 0, 18, 19, 18, 19,
0, 0, 0, 0, 0, 17, 0, 0,
0, 0, 0, 21, 0, 0, 0, 0,
0, 20, 22, 22, 0, 24, 25, 24,
23, 0, 27, 26, 28, 0, 0, 30,
0, 0, 0, 0, 31, 32, 31, 32,
0, 0, 0, 0, 0, 29, 0, 33,
0, 0, 0, 0, 21, 0, 0, 0,
0, 0, 20, 0, 0, 0, 0, 0,
21, 34, 34, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 21, 35,
35, 0, 0, 0, 0, 0, 20, 0,
0, 0, 0, 0, 21, 36, 36, 0,
0, 0, 0, 0, 20, 0, 0, 0,
0, 0, 21, 37, 37, 0, 0, 0,
0, 0, 20, 0, 0, 0, 0, 0,
21, 38, 38, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 21, 39,
39, 0, 0, 0, 0, 0, 20, 0,
0, 0, 0, 40, 0, 21, 0, 0,
0, 0, 0, 20, 0, 0, 0, 0,
0, 21, 41, 41, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 21,
42, 42, 0, 0, 0, 0, 0, 20,
0, 0, 0, 0, 0, 21, 43, 43,
0, 0, 0, 0, 0, 20, 0, 0,
0, 0, 0, 21, 44, 44, 0, 0,
0, 0, 0, 20, 0, 0, 0, 0,
0, 21, 45, 45, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 21,
46, 46, 0, 0, 0, 0, 0, 20,
0, 0, 0, 0, 0, 47, 0, 0,
0, 0, 0, 20, 48, 48, 0, 49,
25, 49, 50, 23, 28, 27, 26, 0,
27, 51, 26, 0, 0, 0, 0, 0,
21, 52, 52, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 21, 53,
53, 0, 0, 0, 0, 0, 20, 0,
0, 0, 0, 0, 21, 54, 54, 0,
0, 0, 0, 0, 20, 0, 0, 0,
0, 0, 21, 55, 55, 0, 0, 0,
0, 0, 20, 0, 0, 0, 0, 0,
21, 56, 56, 0, 0, 0, 0, 0,
20, 0, 0, 0, 0, 0, 21, 57,
57, 0, 0, 0, 0, 0, 20, 0,
0, 0, 0, 0, 21, 58, 58, 0,
0, 0, 0, 0, 20, 0, 0, 0,
0, 59, 0, 21, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 21,
60, 60, 0, 0, 0, 0, 0, 20,
0, 0, 0, 0, 0, 21, 61, 61,
0, 0, 0, 0, 0, 20, 0, 0,
0, 0, 0, 21, 62, 62, 0, 0,
0, 0, 0, 20, 0, 0, 0, 0,
0, 21, 63, 63, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 21,
64, 64, 0, 0, 0, 0, 0, 20,
0, 0, 0, 0, 0, 21, 65, 65,
0, 0, 0, 0, 0, 20, 0, 0,
0, 0, 0, 21, 66, 66, 0, 0,
0, 0, 0, 20, 0, 0, 0, 0,
0, 21, 67, 67, 0, 0, 0, 0,
0, 20, 0, 0, 0, 0, 0, 68,
0, 0, 0, 0, 0, 20, 69, 69,
0, 70, 25, 70, 71, 23, 0, 27,
72, 26, 0, 27, 73, 26, 0, 27,
74, 26, 0, 27, 75, 26, 0, 27,
76, 26, 0, 27, 77, 26, 78, 0,
79, 0, 81, 80, 82, 83, 83, 83,
0, 84, 0, 85, 86, 0, 87, 0,
89, 88, 90, 91, 91, 91, 0, 92,
0, 93, 95, 94, 96, 97, 98, 98,
98, 94, 99, 96, 97, 98, 98, 98,
94, 101, 102, 103, 103, 103, 100, 104,
97, 98, 98, 98, 94, 105, 96, 97,
98, 98, 98, 94, 106, 95, 94, 107,
96, 97, 98, 98, 98, 94, 108, 0,
109, 0, 110, 111, 0, 0, 0, 0,
0, 21, 0, 0, 0, 0, 0, 20,
112, 0, 101, 102, 103, 103, 103, 100,
96, 97, 98, 98, 98, 94, 0, 113,
114, 0
};
static const char _hsh_http_trans_targs[] = {
0, 2, 3, 2, 4, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 23, 41, 16, 17, 18, 19,
18, 39, 19, 20, 21, 16, 22, 23,
41, 90, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35, 36, 37,
38, 38, 40, 40, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 59, 60,
61, 62, 63, 64, 65, 19, 67, 68,
69, 72, 70, 69, 71, 91, 73, 92,
75, 86, 76, 75, 77, 78, 79, 84,
80, 82, 79, 81, 79, 80, 82, 79,
83, 93, 85, 94, 87, 95, 96, 97,
91, 96, 97
};
static const char _hsh_http_trans_actions[] = {
17, 19, 3, 5, 22, 5, 3, 1,
0, 0, 0, 0, 0, 0, 0, 3,
0, 64, 64, 64, 5, 3, 0, 25,
25, 56, 5, 3, 5, 52, 52, 52,
52, 43, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 3,
0, 25, 60, 37, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 3, 0, 25, 25,
5, 5, 5, 5, 5, 40, 0, 0,
46, 0, 0, 7, 0, 28, 0, 11,
46, 0, 0, 7, 0, 28, 76, 9,
76, 49, 72, 76, 80, 80, 68, 85,
76, 90, 76, 90, 0, 11, 31, 34,
9, 13, 15
};
static const char _hsh_http_eof_actions[] = {
0, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0
};
static const int hsh_http_start = 1;
static const int hsh_http_first_final = 90;
static const int hsh_http_error = 0;
static const int hsh_http_en_chunk_end = 66;
static const int hsh_http_en_chunked_body = 74;
static const int hsh_http_en_small_body = 88;
static const int hsh_http_en_large_body = 89;
static const int hsh_http_en_main = 1;
#line 235 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
void hsh_parser_init(struct hsh_parser_s* parser) {
memset(parser, 0, sizeof(struct hsh_parser_s));
parser->state = hsh_http_start;
}
struct hsh_token_s hsh_parser_exec(struct hsh_parser_s* parser, struct hsh_buffer_s* buffer, int max_buf_capacity) {
struct hsh_token_s none = {};
none.type = HSH_TOK_NONE;
if (HTTP_FLAG_CHECK(parser->flags, HSH_P_FLAG_DONE) || parser->sequence_id == buffer->sequence_id) {
return none;
}
int cs = parser->state;
char* eof = NULL;
char *p = buffer->buf + buffer->index;
char *pe = buffer->buf + buffer->length;
#line 373 "/Users/jeremywilliams/code/httpserver.h/build/src/parser.c"
{
int _klen;
unsigned int _trans;
const char *_acts;
unsigned int _nacts;
const char *_keys;
if ( p == pe )
goto _test_eof;
if ( cs == 0 )
goto _out;
_resume:
_keys = _hsh_http_trans_keys + _hsh_http_key_offsets[cs];
_trans = _hsh_http_index_offsets[cs];
_klen = _hsh_http_single_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + _klen - 1;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + ((_upper-_lower) >> 1);
if ( (*p) < *_mid )
_upper = _mid - 1;
else if ( (*p) > *_mid )
_lower = _mid + 1;
else {
_trans += (unsigned int)(_mid - _keys);
goto _match;
}
}
_keys += _klen;
_trans += _klen;
}
_klen = _hsh_http_range_lengths[cs];
if ( _klen > 0 ) {
const char *_lower = _keys;
const char *_mid;
const char *_upper = _keys + (_klen<<1) - 2;
while (1) {
if ( _upper < _lower )
break;
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
if ( (*p) < _mid[0] )
_upper = _mid - 2;
else if ( (*p) > _mid[1] )
_lower = _mid + 2;
else {
_trans += (unsigned int)((_mid - _keys)>>1);
goto _match;
}
}
_trans += _klen;
}
_match:
_trans = _hsh_http_indicies[_trans];
cs = _hsh_http_trans_targs[_trans];
if ( _hsh_http_trans_actions[_trans] == 0 )
goto _again;
_acts = _hsh_http_actions + _hsh_http_trans_actions[_trans];
_nacts = (unsigned int) *_acts++;
while ( _nacts-- > 0 )
{
switch ( *_acts++ )
{
case 0:
#line 23 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{ HSH_ENTER_TOKEN(HSH_TOK_METHOD, 32) }
break;
case 1:
#line 24 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{ HSH_ENTER_TOKEN(HSH_TOK_TARGET, 1024) }
break;
case 2:
#line 25 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{ HSH_ENTER_TOKEN(HSH_TOK_VERSION, 16) }
break;
case 3:
#line 26 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{ HSH_ENTER_TOKEN(HSH_TOK_HEADER_KEY, 256) }
break;
case 4:
#line 27 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{ HSH_ENTER_TOKEN(HSH_TOK_HEADER_VALUE, 4096) }
break;
case 5:
#line 28 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->token.type = HSH_TOK_BODY;
parser->token.flags = 0;
parser->token.index = p - buffer->buf;
}
break;
case 6:
#line 33 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->token.len = p - (buffer->buf + parser->token.index);
// hsh_token_array_push(&parser->tokens, parser->token);
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
{p++; goto _out; }
}
break;
case 7:
#line 40 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->content_length *= 10;
parser->content_length += (*p) - '0';
}
break;
case 8:
#line 45 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_CHUNKED);
}
break;
case 9:
#line 49 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->limit_count = 0;
parser->limit_max = 256;
}
break;
case 10:
#line 54 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->limit_count++;
if (parser->limit_count > parser->limit_max) {
// parser->rc = (int8_t)HSH_PARSER_ERR;
{p++; goto _out; }
}
}
break;
case 11:
#line 62 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
buffer->after_headers_index = p - buffer->buf + 1;
parser->content_remaining = parser->content_length;
parser->token = (struct hsh_token_s){ };
parser->token.type = HSH_TOK_HEADERS_DONE;
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
if (HTTP_FLAG_CHECK(parser->flags, HSH_P_FLAG_CHUNKED)) {
HTTP_FLAG_SET(parser->token.flags, HSH_TOK_FLAG_STREAMED_BODY);
cs = 74;
{p++; goto _out; }
} else if (parser->content_length == 0) {
HTTP_FLAG_SET(parser->token.flags, HSH_TOK_FLAG_NO_BODY);
{p++; goto _out; }
// The body won't fit into the buffer at maximum capacity.
} else if (parser->content_length > max_buf_capacity - buffer->after_headers_index) {
HTTP_FLAG_SET(parser->token.flags, HSH_TOK_FLAG_STREAMED_BODY);
cs = 89;
{p++; goto _out; }
} else {
// Resize the buffer to hold the full body
if (parser->content_length + buffer->after_headers_index > buffer->capacity) {
buffer->buf = (char*)realloc(buffer->buf, parser->content_length + buffer->after_headers_index);
buffer->capacity = parser->content_length + buffer->after_headers_index;
}
cs = 88;
{p++; goto _out; }
}
}
break;
case 12:
#line 91 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->content_length = 0;
}
break;
case 13:
#line 95 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
if ((*p) >= 'A' && (*p) <= 'F') {
parser->content_length *= 0x10;
parser->content_length += (*p) - 55;
} else if ((*p) >= 'a' && (*p) <= 'f') {
parser->content_length *= 0x10;
parser->content_length += (*p) - 87;
} else if ((*p) >= '0' && (*p) <= '9') {
parser->content_length *= 0x10;
parser->content_length += (*p) - '0';
}
}
break;
case 14:
#line 108 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
char* last_body_byte = buffer->buf + parser->token.index + parser->content_length - 1;
if (pe >= last_body_byte) {
p = last_body_byte;
parser->token.len = parser->content_length;
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
cs = 66;
{p++; goto _out; }
// The current chunk is at the end of the buffer and the buffer cannot be expanded.
// Move the remaining contents of the buffer to just after the headers to free up
// capacity in the buffer.
} else if (p - buffer->buf + parser->content_length > max_buf_capacity) {
memcpy(buffer->buf + buffer->after_headers_index, p, pe - p);
buffer->length = buffer->after_headers_index + pe - p;
p = buffer->buf + buffer->after_headers_index;
parser->token.index = buffer->after_headers_index;
parser->sequence_id = buffer->sequence_id;
p--;
{p++; goto _out; }
}
}
break;
case 15:
#line 130 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
// write 0 byte body to tokens
parser->token.type = HSH_TOK_BODY;
parser->token.index = 0;
parser->token.len = 0;
parser->token.flags = HSH_TOK_FLAG_BODY_FINAL;
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_DONE);
{p++; goto _out; }
}
break;
case 16:
#line 141 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->token.index = buffer->after_headers_index;
parser->token.len = parser->content_length;
HTTP_FLAG_SET(parser->token.flags, HSH_TOK_FLAG_SMALL_BODY);
char* last_body_byte = buffer->buf + parser->token.index + parser->content_length - 1;
if (pe >= last_body_byte) {
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_DONE);
}
p = pe;
p--;
{p++; goto _out; }
}
break;
case 17:
#line 155 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
parser->token.index = buffer->after_headers_index;
char* last_body_byte = buffer->buf + buffer->after_headers_index + parser->content_remaining - 1;
if (pe >= last_body_byte) {
parser->token.flags = HSH_TOK_FLAG_BODY_FINAL;
parser->token.len = parser->content_remaining;
parser->content_remaining = 0;
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_DONE);
} else {
parser->token.len = pe - p;
parser->content_remaining -= parser->token.len;
HTTP_FLAG_SET(parser->flags, HSH_P_FLAG_TOKEN_READY);
p = buffer->buf + buffer->after_headers_index;
buffer->length = buffer->after_headers_index;
parser->sequence_id = buffer->sequence_id;
}
p--;
{p++; goto _out; }
}
break;
case 18:
#line 176 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
// parser->rc = (int8_t)HSH_PARSER_ERR;
{p++; goto _out; }
}
break;
#line 649 "/Users/jeremywilliams/code/httpserver.h/build/src/parser.c"
}
}
_again:
if ( cs == 0 )
goto _out;
if ( ++p != pe )
goto _resume;
_test_eof: {}
if ( p == eof )
{
const char *__acts = _hsh_http_actions + _hsh_http_eof_actions[cs];
unsigned int __nacts = (unsigned int) *__acts++;
while ( __nacts-- > 0 ) {
switch ( *__acts++ ) {
case 18:
#line 176 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
{
// parser->rc = (int8_t)HSH_PARSER_ERR;
{p++; goto _out; }
}
break;
#line 672 "/Users/jeremywilliams/code/httpserver.h/build/src/parser.c"
}
}
}
_out: {}
}
#line 252 "/Users/jeremywilliams/code/httpserver.h/src/parser.rl"
parser->state = cs;
buffer->index = p - buffer->buf;
if (HTTP_FLAG_CHECK(parser->flags, HSH_P_FLAG_TOKEN_READY)) {
HTTP_FLAG_CLEAR(parser->flags, HSH_P_FLAG_TOKEN_READY);
return parser->token;
} else {
parser->sequence_id = buffer->sequence_id;
return none;
}
}
#line 1 "read_socket.c"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef HTTPSERVER_IMPL
#include "common.h"
#include "parser.h"
#include "read_socket.h"
#endif
void _hs_token_array_push(struct hs_token_array_s *array,
struct hsh_token_s a) {
if (array->size == array->capacity) {
array->capacity *= 2;
array->buf = (struct hsh_token_s *)realloc(
array->buf, array->capacity * sizeof(struct hsh_token_s));
assert(array->buf != NULL);
}
array->buf[array->size] = a;
array->size++;
}
void _hs_buffer_init(struct hsh_buffer_s *buffer, int initial_capacity,
int64_t *memused) {
*buffer = (struct hsh_buffer_s){0};
buffer->buf = (char *)calloc(1, initial_capacity);
*memused += initial_capacity;
assert(buffer->buf != NULL);
buffer->capacity = initial_capacity;
}
int _hs_read_into_buffer(struct hsh_buffer_s *buffer, int request_socket,
int64_t *server_memused,
int64_t max_request_buf_capacity) {
int bytes;
do {
bytes = read(request_socket, buffer->buf + buffer->length,
buffer->capacity - buffer->length);
if (bytes > 0)
buffer->length += bytes;
if (buffer->length == buffer->capacity &&
buffer->capacity != max_request_buf_capacity) {
*server_memused -= buffer->capacity;
buffer->capacity *= 2;
if (buffer->capacity > max_request_buf_capacity) {
buffer->capacity = max_request_buf_capacity;
}
*server_memused += buffer->capacity;
buffer->buf = (char *)realloc(buffer->buf, buffer->capacity);
assert(buffer->buf != NULL);
}
} while (bytes > 0 && buffer->capacity < max_request_buf_capacity);
buffer->sequence_id++;
return bytes;
}
int _hs_buffer_requires_read(struct hsh_buffer_s *buffer) {
return buffer->index >= buffer->length;
}
void _hs_exec_callback(http_request_t *request,
void (*cb)(struct http_request_s *)) {
request->state = HTTP_SESSION_NOP;
cb(request);
}
enum hs_read_rc_e
_hs_parse_buffer_and_exec_user_cb(http_request_t *request,
int max_request_buf_capacity) {
enum hs_read_rc_e rc = HS_READ_RC_SUCCESS;
do {
struct hsh_token_s token = hsh_parser_exec(
&request->parser, &request->buffer, max_request_buf_capacity);
switch (token.type) {
case HSH_TOK_HEADERS_DONE:
_hs_token_array_push(&request->tokens, token);
if (HTTP_FLAG_CHECK(token.flags, HSH_TOK_FLAG_STREAMED_BODY) ||
HTTP_FLAG_CHECK(token.flags, HSH_TOK_FLAG_NO_BODY)) {
HTTP_FLAG_SET(request->flags, HTTP_FLG_STREAMED);
_hs_exec_callback(request, request->server->request_handler);
return rc;
}
break;
case HSH_TOK_BODY:
_hs_token_array_push(&request->tokens, token);
if (HTTP_FLAG_CHECK(token.flags, HSH_TOK_FLAG_SMALL_BODY)) {
_hs_exec_callback(request, request->server->request_handler);
} else {
if (HTTP_FLAG_CHECK(token.flags, HSH_TOK_FLAG_BODY_FINAL) &&
token.len > 0) {
_hs_exec_callback(request, request->chunk_cb);
// A zero length body is used to indicate to the user code that the
// body has finished streaming. This is natural when dealing with
// chunked request bodies but requires us to inject a zero length
// body for non-chunked requests.
struct hsh_token_s token = {};
memset(&token, 0, sizeof(struct hsh_token_s));
token.type = HSH_TOK_BODY;
_hs_token_array_push(&request->tokens, token);
_hs_exec_callback(request, request->chunk_cb);
} else {
_hs_exec_callback(request, request->chunk_cb);
}
}
return rc;
case HSH_TOK_ERR:
return HS_READ_RC_PARSE_ERR;
case HSH_TOK_NONE:
return rc;
default:
_hs_token_array_push(&request->tokens, token);
break;
}
} while (1);
}
// Reads the request socket if required and parses HTTP in a non-blocking
// manner.
//
// It should be called when a new connection is established and when a read
// ready event occurs for the request socket. It parses the HTTP request and
// fills the tokens array of the request struct. It will also invoke the
// request_hander callback and the chunk_cb callback in the appropriate
// scenarios.
enum hs_read_rc_e hs_read_request_and_exec_user_cb(http_request_t *request,
struct hs_read_opts_s opts) {
request->state = HTTP_SESSION_READ;
request->timeout = HTTP_REQUEST_TIMEOUT;
if (request->buffer.buf == NULL) {
_hs_buffer_init(&request->buffer, opts.initial_request_buf_capacity,
&request->server->memused);
hsh_parser_init(&request->parser);
}
if (_hs_buffer_requires_read(&request->buffer)) {
int bytes = _hs_read_into_buffer(&request->buffer, request->socket,
&request->server->memused,
opts.max_request_buf_capacity);
if (bytes == opts.eof_rc) {
return HS_READ_RC_SOCKET_ERR;
}
}
return _hs_parse_buffer_and_exec_user_cb(request,
opts.max_request_buf_capacity);
}
#line 1 "respond.c"
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef HTTPSERVER_IMPL
#include "buffer_util.h"
#include "common.h"
#include "request_util.h"
#include "respond.h"
#endif
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", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "", "", "", "", "", "", ""};
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_serialize_headers_list(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;
}
_grwprintf(printctx, "\r\n");
}
void _http_serialize_headers(http_request_t *request, http_response_t *response,
grwprintf_t *printctx) {
if (HTTP_FLAG_CHECK(request->flags, HTTP_AUTOMATIC)) {
hs_request_detect_keep_alive_flag(request);
}
if (HTTP_FLAG_CHECK(request->flags, HTTP_KEEP_ALIVE)) {
hs_response_set_header(response, "Connection", "keep-alive");
} else {
hs_response_set_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);
if (!HTTP_FLAG_CHECK(request->flags, HTTP_CHUNKED_RESPONSE)) {
_grwprintf(printctx, "Content-Length: %d\r\n", response->content_length);
}
_http_serialize_headers_list(response, printctx);
}
void _http_perform_response(http_request_t *request, http_response_t *response,
grwprintf_t *printctx, hs_req_fn_t http_write) {
http_header_t *header = response->headers;
while (header) {
http_header_t *tmp = header;
header = tmp->next;
free(tmp);
}
_hs_buffer_free(&request->buffer, &request->server->memused);
free(response);
request->buffer.buf = printctx->buf;
request->buffer.length = printctx->size;
request->buffer.capacity = printctx->capacity;
request->bytes_written = 0;
request->state = HTTP_SESSION_WRITE;
http_write(request);
}
// See api.h http_response_header
void hs_response_set_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;
}
// Serializes the response into the request buffer and calls http_write.
// See api.h http_respond for more details
void hs_request_respond(http_request_t *request, http_response_t *response,
hs_req_fn_t http_write) {
grwprintf_t printctx;
_grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
_http_serialize_headers(request, response, &printctx);
if (response->body) {
_grwmemcpy(&printctx, response->body, response->content_length);
}
_http_perform_response(request, response, &printctx, http_write);
}
// Serializes a chunk into the request buffer and calls http_write.
// See api.h http_respond_chunk for more details.
void hs_request_respond_chunk(http_request_t *request,
http_response_t *response, hs_req_fn_t cb,
hs_req_fn_t http_write) {
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);
hs_response_set_header(response, "Transfer-Encoding", "chunked");
_http_serialize_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_perform_response(request, response, &printctx, http_write);
}
// Serializes the zero sized final chunk into the request buffer and calls
// http_write. See api.h http_respond_chunk_end for more details.
void hs_request_respond_chunk_end(http_request_t *request,
http_response_t *response,
hs_req_fn_t http_write) {
grwprintf_t printctx;
_grwprintf_init(&printctx, HTTP_RESPONSE_BUF_SIZE, &request->server->memused);
_grwprintf(&printctx, "0\r\n");
_http_serialize_headers_list(response, &printctx);
_grwprintf(&printctx, "\r\n");
HTTP_FLAG_CLEAR(request->flags, HTTP_CHUNKED_RESPONSE);
_http_perform_response(request, response, &printctx, http_write);
}
// See api.h http_response_status
void hs_response_set_status(http_response_t *response, int status) {
response->status = status > 599 || status < 100 ? 500 : status;
}
// See api.h http_response_body
void hs_response_set_body(http_response_t *response, char const *body,
int length) {
response->body = body;
response->content_length = length;
}
// See api.h http_response_init
http_response_t *hs_response_init() {
http_response_t *response =
(http_response_t *)calloc(1, sizeof(http_response_t));
assert(response != NULL);
response->status = 200;
return response;
}
// Simple less flexible interface for responses, used for errors.
void hs_request_respond_error(http_request_t *request, int code,
char const *message, hs_req_fn_t http_write) {
struct http_response_s *response = hs_response_init();
hs_response_set_status(response, code);
hs_response_set_header(response, "Content-Type", "text/plain");
hs_response_set_body(response, message, strlen(message));
hs_request_respond(request, response, http_write);
http_write(request);
}
#line 1 "server.c"
#include <arpa/inet.h>
#include <assert.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <time.h>
#ifdef EPOLL
#include <sys/epoll.h>
#include <sys/timerfd.h>
#else
#include <sys/event.h>
#endif
#ifndef HTTPSERVER_IMPL
#include "common.h"
#include "server.h"
#endif
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);
}
}
#ifdef KQUEUE
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);
}
void _hs_server_init_events(http_server_t *serv, hs_evt_cb_t unused) {
(void)unused;
serv->loop = kqueue();
struct kevent ev_set;
EV_SET(&ev_set, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, NOTE_SECONDS, 1, serv);
kevent(serv->loop, &ev_set, 1, NULL, 0, NULL);
}
int hs_server_run_event_loop(http_server_t *serv, const char *ipaddr) {
hs_server_listen_on_addr(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 hs_server_poll_events(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;
}
#else
void _hs_server_init_events(http_server_t *serv, hs_evt_cb_t timer_cb) {
serv->loop = epoll_create1(0);
serv->timer_handler = 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;
}
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);
}
int hs_server_run_event_loop(http_server_t *serv, const char *ipaddr) {
hs_server_listen_on_addr(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 hs_server_poll_events(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;
}
#endif
void hs_server_listen_on_addr(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);
}
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 *hs_server_init(int port, void (*handler)(http_request_t *),
hs_evt_cb_t accept_cb,
hs_evt_cb_t epoll_timer_cb) {
http_server_t *serv = (http_server_t *)malloc(sizeof(http_server_t));
assert(serv != NULL);
serv->port = port;
serv->memused = 0;
serv->handler = accept_cb;
_hs_server_init_events(serv, epoll_timer_cb);
hs_generate_date_time(serv->date);
serv->request_handler = handler;
return serv;
}
#line 1 "write_socket.c"
#include <errno.h>
#include <unistd.h>
#ifndef HTTPSERVER_IMPL
#include "common.h"
#include "write_socket.h"
#endif
#ifdef DEBUG
#define write hs_test_write
ssize_t hs_test_write(int fd, char const *data, size_t size);
#endif
// Writes response bytes from the buffer out to the socket.
//
// Runs when we get a socket ready to write event or when initiating an HTTP
// response and writing to the socket for the first time. If the response is
// chunked the chunk_cb callback will be invoked signalling to the user code
// that another chunk is ready to be written.
enum hs_write_rc_e hs_write_socket(http_request_t *request) {
int bytes =
write(request->socket, request->buffer.buf + request->bytes_written,
request->buffer.length - request->bytes_written);
if (bytes > 0)
request->bytes_written += bytes;
enum hs_write_rc_e rc = HS_WRITE_RC_SUCCESS;
if (errno == EPIPE) {
rc = HS_WRITE_RC_SOCKET_ERR;
} else {
if (request->bytes_written != request->buffer.length) {
// All bytes of the body were not written and we need to wait until the
// socket is writable again to complete the write
rc = HS_WRITE_RC_CONTINUE;
} 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.
rc = HS_WRITE_RC_SUCCESS_CHUNK;
} else {
if (HTTP_FLAG_CHECK(request->flags, HTTP_KEEP_ALIVE)) {
rc = HS_WRITE_RC_SUCCESS;
} else {
rc = HS_WRITE_RC_SUCCESS_CLOSE;
}
}
}
return rc;
}
#line 1 "connection.c"
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#ifdef KQUEUE
#include <sys/event.h>
#else
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <time.h>
#endif
#ifndef HTTPSERVER_IMPL
#include "buffer_util.h"
#include "common.h"
#include "connection.h"
#endif
#ifdef KQUEUE
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);
}
void _hs_add_timer_event(http_request_t *request, hs_io_cb_t unused) {
(void)unused;
struct kevent ev_set;
EV_SET(&ev_set, request->socket, EVFILT_TIMER, EV_ADD | EV_ENABLE, 0, 1000,
request);
kevent(request->server->loop, &ev_set, 1, NULL, 0, NULL);
}
#else
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);
}
void _hs_add_timer_event(http_request_t *request, hs_io_cb_t timer_cb) {
request->timer_handler = 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;
}
#endif
void hs_request_terminate_connection(http_request_t *request) {
_hs_delete_events(request);
close(request->socket);
_hs_buffer_free(&request->buffer, &request->server->memused);
free(request->tokens.buf);
request->tokens.buf = NULL;
free(request);
}
void _hs_token_array_init(struct hs_token_array_s *array, int capacity) {
array->buf =
(struct hsh_token_s *)malloc(sizeof(struct hsh_token_s) * capacity);
assert(array->buf != NULL);
array->size = 0;
array->capacity = capacity;
}
http_request_t *_hs_request_init(int sock, http_server_t *server,
hs_io_cb_t io_cb) {
http_request_t *request = (http_request_t *)calloc(1, sizeof(http_request_t));
assert(request != NULL);
request->socket = sock;
request->server = server;
request->handler = io_cb;
request->timeout = HTTP_REQUEST_TIMEOUT;
request->flags = HTTP_AUTOMATIC;
request->parser = (struct hsh_parser_s){};
request->buffer = (struct hsh_buffer_s){};
request->tokens.buf = NULL;
_hs_token_array_init(&request->tokens, 32);
return request;
}
http_request_t *hs_server_accept_connection(http_server_t *server,
hs_io_cb_t io_cb,
hs_io_cb_t epoll_timer_cb) {
http_request_t *request = NULL;
int sock = 0;
sock = accept(server->socket, (struct sockaddr *)&server->addr, &server->len);
if (sock > 0) {
int flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
request = _hs_request_init(sock, server, io_cb);
_hs_add_timer_event(request, epoll_timer_cb);
}
return request;
}
#line 1 "io_events.c"
#include <stdlib.h>
#ifdef KQUEUE
#include <sys/event.h>
#else
#include <stdint.h>
#include <sys/epoll.h>
#include <unistd.h>
#endif
#ifndef HTTPSERVER_IMPL
#include "buffer_util.h"
#include "common.h"
#include "connection.h"
#include "io_events.h"
#include "read_socket.h"
#include "respond.h"
#include "server.h"
#include "write_socket.h"
#endif
void _hs_read_socket_and_handle_return_code(http_request_t *request) {
struct hs_read_opts_s opts;
opts.initial_request_buf_capacity = HTTP_REQUEST_BUF_SIZE;
opts.max_request_buf_capacity = HTTP_MAX_REQUEST_BUF_SIZE;
opts.eof_rc = 0;
enum hs_read_rc_e rc = hs_read_request_and_exec_user_cb(request, opts);
switch (rc) {
case HS_READ_RC_PARSE_ERR:
hs_request_respond_error(request, 400, "Bad Request",
hs_request_begin_write);
break;
case HS_READ_RC_SOCKET_ERR:
hs_request_terminate_connection(request);
break;
case HS_READ_RC_SUCCESS:
break;
}
}
void hs_request_begin_read(http_request_t *request);
void _hs_write_socket_and_handle_return_code(http_request_t *request) {
enum hs_write_rc_e rc = hs_write_socket(request);
request->timeout = rc == HS_WRITE_RC_SUCCESS ? HTTP_KEEP_ALIVE_TIMEOUT
: HTTP_REQUEST_TIMEOUT;
if (rc != HS_WRITE_RC_CONTINUE)
_hs_buffer_free(&request->buffer, &request->server->memused);
switch (rc) {
case HS_WRITE_RC_SUCCESS_CLOSE:
case HS_WRITE_RC_SOCKET_ERR:
// Error or response complete, connection: close
hs_request_terminate_connection(request);
break;
case HS_WRITE_RC_SUCCESS:
// Response complete, keep-alive connection
hs_request_begin_read(request);
break;
case HS_WRITE_RC_SUCCESS_CHUNK:
// Finished writing chunk, request next
request->state = HTTP_SESSION_NOP;
request->chunk_cb(request);
break;
case HS_WRITE_RC_CONTINUE:
break;
}
}
void _hs_accept_and_begin_request_cycle(http_server_t *server,
hs_io_cb_t on_client_connection_cb,
hs_io_cb_t on_timer_event_cb) {
http_request_t *request = NULL;
while ((request = hs_server_accept_connection(server, on_client_connection_cb,
on_timer_event_cb))) {
if (server->memused > HTTP_MAX_TOTAL_EST_MEM_USAGE) {
hs_request_respond_error(request, 503, "Service Unavailable",
hs_request_begin_write);
} else {
hs_request_begin_read(request);
}
}
}
#ifdef KQUEUE
void _hs_on_kqueue_client_connection_event(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_request_terminate_connection(request);
} else {
if (request->state == HTTP_SESSION_READ) {
_hs_read_socket_and_handle_return_code(request);
} else if (request->state == HTTP_SESSION_WRITE) {
_hs_write_socket_and_handle_return_code(request);
}
}
}
void hs_on_kqueue_server_event(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_and_begin_request_cycle(
server, _hs_on_kqueue_client_connection_event, NULL);
}
}
#else
void _hs_on_epoll_client_connection_event(struct epoll_event *ev) {
http_request_t *request = (http_request_t *)ev->data.ptr;
if (request->state == HTTP_SESSION_READ) {
_hs_read_socket_and_handle_return_code(request);
} else if (request->state == HTTP_SESSION_WRITE) {
_hs_write_socket_and_handle_return_code(request);
}
}
void _hs_on_epoll_request_timer_event(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_request_terminate_connection(request);
}
void hs_on_epoll_server_connection_event(struct epoll_event *ev) {
_hs_accept_and_begin_request_cycle((http_server_t *)ev->data.ptr,
_hs_on_epoll_client_connection_event,
_hs_on_epoll_request_timer_event);
}
void hs_on_epoll_server_timer_event(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);
}
#endif
void _hs_add_write_event(http_request_t *request) {
#ifdef KQUEUE
struct kevent ev_set[2];
EV_SET(&ev_set[0], request->socket, EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0,
request);
EV_SET(&ev_set[1], request->socket, EVFILT_READ, EV_DISABLE, 0, 0, request);
kevent(request->server->loop, ev_set, 2, NULL, 0, NULL);
#else
struct epoll_event ev;
ev.events = EPOLLOUT | EPOLLET;
ev.data.ptr = request;
epoll_ctl(request->server->loop, EPOLL_CTL_MOD, request->socket, &ev);
#endif
}
void hs_request_begin_write(http_request_t *request) {
request->state = HTTP_SESSION_WRITE;
_hs_add_write_event(request);
_hs_write_socket_and_handle_return_code(request);
}
void _hs_add_read_event(http_request_t *request) {
#ifdef KQUEUE
// No action needed for kqueue since it's read event stays active. Should
// it be disabled during write?
struct kevent ev_set;
EV_SET(&ev_set, request->socket, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0,
request);
kevent(request->server->loop, &ev_set, 1, NULL, 0, NULL);
#else
struct epoll_event ev;
ev.events = EPOLLIN | EPOLLET;
ev.data.ptr = request;
epoll_ctl(request->server->loop, EPOLL_CTL_MOD, request->socket, &ev);
#endif
}
void hs_request_begin_read(http_request_t *request) {
request->state = HTTP_SESSION_READ;
_hs_add_read_event(request);
_hs_read_socket_and_handle_return_code(request);
}
#endif
#endif
#endif
|