forked from jeremycw/httpserver.h
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
127 lines (111 loc) · 2.26 KB
/
Copy pathcommon.h
File metadata and controls
127 lines (111 loc) · 2.26 KB
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
#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