cace950fc41b0d4bc26b907ff2f4c0f9f38f0004
[project/uhttpd.git] / uhttpd.h
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #ifndef __UHTTPD_H
21 #define __UHTTPD_H
22
23 #include <netinet/in.h>
24 #include <limits.h>
25 #include <dirent.h>
26
27 #include <libubox/list.h>
28 #include <libubox/uloop.h>
29 #include <libubox/ustream.h>
30 #include <libubox/blob.h>
31 #include <libubox/utils.h>
32 #ifdef HAVE_UBUS
33 #include <libubus.h>
34 #include <json/json.h>
35 #endif
36 #ifdef HAVE_TLS
37 #include <libubox/ustream-ssl.h>
38 #endif
39
40 #include "utils.h"
41
42 #define UH_LIMIT_CLIENTS 64
43
44 #define __enum_header(_name, _val) HDR_##_name,
45 #define __blobmsg_header(_name, _val) [HDR_##_name] = { .name = #_val, .type = BLOBMSG_TYPE_STRING },
46
47 struct client;
48
49 struct config {
50 const char *docroot;
51 const char *realm;
52 const char *file;
53 const char *error_handler;
54 const char *cgi_prefix;
55 const char *cgi_path;
56 const char *lua_handler;
57 const char *lua_prefix;
58 const char *ubus_prefix;
59 const char *ubus_socket;
60 int no_symlinks;
61 int no_dirlists;
62 int network_timeout;
63 int rfc1918_filter;
64 int tcp_keepalive;
65 int max_script_requests;
66 int max_connections;
67 int http_keepalive;
68 int script_timeout;
69 int ubus_noauth;
70 };
71
72 struct auth_realm {
73 struct list_head list;
74 const char *path;
75 const char *user;
76 const char *pass;
77 };
78
79 enum http_method {
80 UH_HTTP_MSG_GET,
81 UH_HTTP_MSG_POST,
82 UH_HTTP_MSG_HEAD,
83 };
84
85 enum http_version {
86 UH_HTTP_VER_0_9,
87 UH_HTTP_VER_1_0,
88 UH_HTTP_VER_1_1,
89 };
90
91 enum http_user_agent {
92 UH_UA_UNKNOWN,
93 UH_UA_GECKO,
94 UH_UA_CHROME,
95 UH_UA_SAFARI,
96 UH_UA_MSIE,
97 UH_UA_KONQUEROR,
98 UH_UA_OPERA,
99 UH_UA_MSIE_OLD,
100 UH_UA_MSIE_NEW,
101 };
102
103 struct http_request {
104 enum http_method method;
105 enum http_version version;
106 enum http_user_agent ua;
107 int redirect_status;
108 int content_length;
109 bool expect_cont;
110 bool connection_close;
111 uint8_t transfer_chunked;
112 const struct auth_realm *realm;
113 };
114
115 enum client_state {
116 CLIENT_STATE_INIT,
117 CLIENT_STATE_HEADER,
118 CLIENT_STATE_DATA,
119 CLIENT_STATE_DONE,
120 CLIENT_STATE_CLOSE,
121 CLIENT_STATE_CLEANUP,
122 };
123
124 struct interpreter {
125 struct list_head list;
126 const char *path;
127 const char *ext;
128 };
129
130 struct path_info {
131 const char *root;
132 const char *phys;
133 const char *name;
134 const char *info;
135 const char *query;
136 const char *auth;
137 bool redirected;
138 struct stat stat;
139 const struct interpreter *ip;
140 };
141
142 struct env_var {
143 const char *name;
144 const char *value;
145 };
146
147 struct relay {
148 struct ustream_fd sfd;
149 struct uloop_process proc;
150 struct uloop_timeout timeout;
151 struct client *cl;
152
153 bool process_done;
154 bool error;
155 bool skip_data;
156
157 int ret;
158 int header_ofs;
159
160 void (*header_cb)(struct relay *r, const char *name, const char *value);
161 void (*header_end)(struct relay *r);
162 void (*close)(struct relay *r, int ret);
163 };
164
165 struct dispatch_proc {
166 struct uloop_timeout timeout;
167 struct blob_buf hdr;
168 struct uloop_fd wrfd;
169 struct relay r;
170 int status_code;
171 char *status_msg;
172 };
173
174 struct dispatch_handler {
175 struct list_head list;
176 bool script;
177
178 bool (*check_url)(const char *url);
179 bool (*check_path)(struct path_info *pi, const char *url);
180 void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
181 };
182
183 #ifdef HAVE_UBUS
184 struct dispatch_ubus {
185 struct ubus_request req;
186
187 struct uloop_timeout timeout;
188 struct json_tokener *jstok;
189 struct json_object *jsobj;
190 struct json_object *jsobj_cur;
191 int post_len;
192
193 uint32_t obj;
194 const char *func;
195
196 struct blob_buf buf;
197 bool req_pending;
198 bool array;
199 int array_idx;
200 };
201 #endif
202
203 struct dispatch {
204 int (*data_send)(struct client *cl, const char *data, int len);
205 void (*data_done)(struct client *cl);
206 void (*write_cb)(struct client *cl);
207 void (*close_fds)(struct client *cl);
208 void (*free)(struct client *cl);
209
210 void *req_data;
211 void (*req_free)(struct client *cl);
212
213 bool data_blocked;
214
215 union {
216 struct {
217 struct blob_attr **hdr;
218 int fd;
219 } file;
220 struct dispatch_proc proc;
221 #ifdef HAVE_UBUS
222 struct dispatch_ubus ubus;
223 #endif
224 };
225 };
226
227 struct client {
228 struct list_head list;
229 int refcount;
230 int id;
231
232 struct ustream *us;
233 struct ustream_fd sfd;
234 #ifdef HAVE_TLS
235 struct ustream_ssl ssl;
236 #endif
237 struct uloop_timeout timeout;
238 int requests;
239
240 enum client_state state;
241 bool tls;
242
243 struct http_request request;
244 struct uh_addr srv_addr, peer_addr;
245
246 struct blob_buf hdr;
247 struct dispatch dispatch;
248 };
249
250 extern char uh_buf[4096];
251 extern int n_clients;
252 extern struct config conf;
253 extern const char * const http_versions[];
254 extern const char * const http_methods[];
255 extern struct dispatch_handler cgi_dispatch;
256
257 void uh_index_add(const char *filename);
258
259 bool uh_accept_client(int fd, bool tls);
260
261 void uh_unblock_listeners(void);
262 void uh_setup_listeners(void);
263 int uh_socket_bind(const char *host, const char *port, bool tls);
264
265 bool uh_use_chunked(struct client *cl);
266 void uh_chunk_write(struct client *cl, const void *data, int len);
267 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
268
269 void __printf(2, 3)
270 uh_chunk_printf(struct client *cl, const char *format, ...);
271
272 void uh_chunk_eof(struct client *cl);
273 void uh_request_done(struct client *cl);
274
275 void uh_http_header(struct client *cl, int code, const char *summary);
276 void __printf(4, 5)
277 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
278
279 void uh_handle_request(struct client *cl);
280 void client_poll_post_data(struct client *cl);
281 void uh_client_read_cb(struct client *cl);
282 void uh_client_notify_state(struct client *cl);
283
284 void uh_auth_add(const char *path, const char *user, const char *pass);
285 bool uh_auth_check(struct client *cl, struct path_info *pi);
286
287 void uh_close_listen_fds(void);
288 void uh_close_fds(void);
289
290 void uh_interpreter_add(const char *ext, const char *path);
291 void uh_dispatch_add(struct dispatch_handler *d);
292
293 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
294 void uh_relay_close(struct relay *r, int ret);
295 void uh_relay_free(struct relay *r);
296 void uh_relay_kill(struct client *cl, struct relay *r);
297
298 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
299 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
300 void (*cb)(struct client *cl, struct path_info *pi, char *url));
301
302 int uh_plugin_init(const char *name);
303 void uh_plugin_post_init(void);
304
305 static inline void uh_client_ref(struct client *cl)
306 {
307 cl->refcount++;
308 }
309
310 static inline void uh_client_unref(struct client *cl)
311 {
312 if (--cl->refcount)
313 return;
314
315 if (cl->state == CLIENT_STATE_CLEANUP)
316 ustream_state_change(cl->us);
317 }
318
319 #endif