forked from lpereira/lwan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwan.h
182 lines (153 loc) · 5.22 KB
/
lwan.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
/*
* lwan - simple web server
* Copyright (c) 2012 Leandro A. F. Pereira <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef __LWAN_H__
#define __LWAN_H__
#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>
#include "lwan-trie.h"
#define USE_LORENTZ_WATERWHEEL_SCHEDULER 0
#define N_ELEMENTS(array) (sizeof(array) / sizeof(array[0]))
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define MULTICHAR_CONSTANT(a,b,c,d) ((int32_t)((a) | (b) << 8 | (c) << 16 | (d) << 24))
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define MULTICHAR_CONSTANT(d,c,b,a) ((int32_t)((a) | (b) << 8 | (c) << 16 | (d) << 24))
#elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__
# error A PDP? Seriously?
#endif
#define STRING_SWITCH(s) switch (*((int32_t *)(s)))
#ifdef DISABLE_INLINE_FUNCTIONS
# define ALWAYS_INLINE
#else
# define ALWAYS_INLINE inline __attribute__((always_inline))
#endif
#ifdef DISABLE_BRANCH_PREDICTION
# define LIKELY_IS(x,y) (x)
#else
# define LIKELY_IS(x,y) __builtin_expect((x), (y))
#endif
#define LIKELY(x) LIKELY_IS(!!(x), 1)
#define UNLIKELY(x) LIKELY_IS((x), 0)
typedef struct lwan_request_t_ lwan_request_t;
typedef struct lwan_response_t_ lwan_response_t;
typedef struct lwan_url_map_t_ lwan_url_map_t;
typedef struct lwan_t_ lwan_t;
typedef struct lwan_thread_t_ lwan_thread_t;
typedef enum {
HTTP_OK = 200,
HTTP_BAD_REQUEST = 400,
HTTP_NOT_FOUND = 404,
HTTP_FORBIDDEN = 403,
HTTP_NOT_ALLOWED = 405,
HTTP_TOO_LARGE = 413,
HTTP_INTERNAL_ERROR = 500,
} lwan_http_status_t;
typedef enum {
HTTP_GET = 0,
HTTP_HEAD
} lwan_http_method_t;
typedef enum {
HTTP_1_0 = 0,
HTTP_1_1
} lwan_http_version_t;
enum {
EXT_JPG = MULTICHAR_CONSTANT('.','j','p','g'),
EXT_PNG = MULTICHAR_CONSTANT('.','p','n','g'),
EXT_HTM = MULTICHAR_CONSTANT('.','h','t','m'),
EXT_CSS = MULTICHAR_CONSTANT('.','c','s','s'),
EXT_TXT = MULTICHAR_CONSTANT('.','t','x','t'),
EXT_JS = MULTICHAR_CONSTANT('.','j','s',0),
} lwan_mime_ext_t;
enum {
HTTP_STR_GET = MULTICHAR_CONSTANT('G','E','T',' '),
HTTP_STR_HEAD = MULTICHAR_CONSTANT('H','E','A','D'),
} lwan_http_method_str_t;
enum {
HTTP_HDR_CONNECTION = MULTICHAR_CONSTANT('C','o','n','n'),
HTTP_HDR_HOST = MULTICHAR_CONSTANT('H','o','s','t'),
HTTP_HDR_COOKIE = MULTICHAR_CONSTANT('C','o','o','k'),
HTTP_HDR_RANGE = MULTICHAR_CONSTANT('R','a','n','g'),
HTTP_HDR_REFERER = MULTICHAR_CONSTANT('R','e','f','e'),
HTTP_HDR_IF_MODIFIED_SINCE = MULTICHAR_CONSTANT('I','f','-','m')
} lwan_http_header_str_t;
struct lwan_response_t_ {
char *content;
char *mime_type;
int content_length;
struct {
lwan_http_status_t (*callback)(lwan_t *lwan, lwan_request_t *request, void *data);
void *data;
} stream_content;
};
struct lwan_request_t_ {
lwan_http_method_t method;
lwan_http_version_t http_version;
lwan_response_t *response;
char *url;
int url_len;
int fd;
unsigned int time_to_die;
struct {
char connection;
} header;
struct {
bool is_keep_alive : 1,
alive: 1;
} flags;
};
struct lwan_url_map_t_ {
const char *prefix;
int prefix_len;
lwan_http_status_t (*callback)(lwan_request_t *request, void *data);
void *data;
};
struct lwan_thread_t_ {
lwan_t *lwan;
int epoll_fd;
pthread_t id;
};
struct lwan_t_ {
lwan_trie_t *url_map_trie;
lwan_request_t *requests;
int main_socket;
struct {
int port;
int keep_alive_timeout;
unsigned char enable_thread_affinity : 1,
enable_tcp_defer_accept : 1,
enable_linger : 1;
} config;
struct {
int count;
int max_fd;
lwan_thread_t *threads;
} thread;
};
void lwan_init(lwan_t *l);
void lwan_set_url_map(lwan_t *l, lwan_url_map_t *url_map);
void lwan_main_loop(lwan_t *l);
void lwan_request_set_response(lwan_request_t *request, lwan_response_t *response);
bool lwan_response(lwan_t *l, lwan_request_t *request, lwan_http_status_t status);
size_t lwan_prepare_response_header(lwan_t *l, lwan_request_t *request, lwan_http_status_t status, char header_buffer[]);
bool lwan_default_response(lwan_t *l, lwan_request_t *request, lwan_http_status_t status);
const char *lwan_http_status_as_string(lwan_http_status_t status) __attribute__((pure));
const char *lwan_determine_mime_type_for_file_name(char *file_name) __attribute__((pure));
void lwan_request_set_corked(lwan_request_t *request, bool setting);
void lwan_shutdown(lwan_t *l);
#endif /* __LWAN_H__ */