-
Notifications
You must be signed in to change notification settings - Fork 145
/
httpserver.h
2818 lines (2494 loc) · 84.8 KB
/
httpserver.h
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
// 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) {