d230b31343cfbcaa15dc8e60803c275078988283
[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-c/json.h>
35 #endif
36 #ifdef HAVE_UCODE
37 #include <ucode/vm.h>
38 #endif
39 #ifdef HAVE_TLS
40 #include <libubox/ustream-ssl.h>
41 #endif
42
43 #include "utils.h"
44
45 #define UH_LIMIT_CLIENTS 64
46
47 #define __enum_header(_name, _val) HDR_##_name,
48 #define __blobmsg_header(_name, _val) [HDR_##_name] = { .name = #_val, .type = BLOBMSG_TYPE_STRING },
49
50 struct client;
51
52 struct alias {
53 struct list_head list;
54 char *alias;
55 char *path;
56 };
57
58 struct lua_prefix {
59 struct list_head list;
60 const char *handler;
61 const char *prefix;
62 void *ctx;
63 };
64
65 #ifdef HAVE_UCODE
66 struct ucode_prefix {
67 struct list_head list;
68 const char *handler;
69 const char *prefix;
70 uc_vm_t ctx;
71 };
72 #endif
73
74 struct config {
75 const char *docroot;
76 const char *realm;
77 const char *file;
78 const char *error_handler;
79 const char *cgi_prefix;
80 const char *cgi_docroot_path;
81 const char *cgi_path;
82 const char *ubus_prefix;
83 const char *ubus_socket;
84 int no_symlinks;
85 int no_dirlists;
86 int network_timeout;
87 int rfc1918_filter;
88 int tls_redirect;
89 int tcp_keepalive;
90 int max_script_requests;
91 int max_connections;
92 int http_keepalive;
93 int script_timeout;
94 int ubus_noauth;
95 int ubus_cors;
96 int cgi_prefix_len;
97 int events_retry;
98 struct list_head cgi_alias;
99 struct list_head lua_prefix;
100 #ifdef HAVE_UCODE
101 struct list_head ucode_prefix;
102 #endif
103 };
104
105 struct auth_realm {
106 struct list_head list;
107 const char *path;
108 const char *user;
109 const char *pass;
110 };
111
112 enum http_method {
113 UH_HTTP_MSG_GET,
114 UH_HTTP_MSG_POST,
115 UH_HTTP_MSG_HEAD,
116 UH_HTTP_MSG_OPTIONS,
117 UH_HTTP_MSG_PUT,
118 UH_HTTP_MSG_PATCH,
119 UH_HTTP_MSG_DELETE,
120 };
121
122 enum http_version {
123 UH_HTTP_VER_0_9,
124 UH_HTTP_VER_1_0,
125 UH_HTTP_VER_1_1,
126 };
127
128 enum http_user_agent {
129 UH_UA_UNKNOWN,
130 UH_UA_GECKO,
131 UH_UA_CHROME,
132 UH_UA_SAFARI,
133 UH_UA_MSIE,
134 UH_UA_KONQUEROR,
135 UH_UA_OPERA,
136 UH_UA_MSIE_OLD,
137 UH_UA_MSIE_NEW,
138 };
139
140 struct http_request {
141 enum http_method method;
142 enum http_version version;
143 enum http_user_agent ua;
144 int redirect_status;
145 int content_length;
146 bool expect_cont;
147 bool connection_close;
148 bool disable_chunked;
149 uint8_t transfer_chunked;
150 const struct auth_realm *realm;
151 };
152
153 enum client_state {
154 CLIENT_STATE_INIT,
155 CLIENT_STATE_HEADER,
156 CLIENT_STATE_DATA,
157 CLIENT_STATE_DONE,
158 CLIENT_STATE_CLOSE,
159 CLIENT_STATE_CLEANUP,
160 };
161
162 struct interpreter {
163 struct list_head list;
164 const char *path;
165 const char *ext;
166 };
167
168 struct path_info {
169 const char *root;
170 const char *phys;
171 const char *name;
172 const char *info;
173 const char *query;
174 bool redirected;
175 struct stat stat;
176 const struct interpreter *ip;
177 };
178
179 struct env_var {
180 const char *name;
181 const char *value;
182 };
183
184 struct relay {
185 struct ustream_fd sfd;
186 struct uloop_process proc;
187 struct uloop_timeout timeout;
188 struct client *cl;
189
190 bool process_done;
191 bool error;
192 bool skip_data;
193
194 int ret;
195 int header_ofs;
196
197 void (*header_cb)(struct relay *r, const char *name, const char *value);
198 void (*header_end)(struct relay *r);
199 void (*close)(struct relay *r, int ret);
200 };
201
202 struct dispatch_proc {
203 struct uloop_timeout timeout;
204 struct blob_buf hdr;
205 struct uloop_fd wrfd;
206 struct relay r;
207 int status_code;
208 char *status_msg;
209 };
210
211 struct dispatch_handler {
212 struct list_head list;
213 bool script;
214
215 bool (*check_url)(const char *url);
216 bool (*check_path)(struct path_info *pi, const char *url);
217 void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
218 };
219
220 #ifdef HAVE_UBUS
221 struct dispatch_ubus {
222 struct ubus_request req;
223
224 struct uloop_timeout timeout;
225 struct json_tokener *jstok;
226 struct json_object *jsobj;
227 struct json_object *jsobj_cur;
228 char *url_path;
229 int post_len;
230
231 uint32_t obj;
232 const char *func;
233
234 struct blob_buf buf;
235 bool req_pending;
236 bool array;
237 int array_idx;
238 bool legacy; /* Got legacy request => use legacy reply */
239
240 struct ubus_subscriber sub;
241 };
242 #endif
243
244 struct dispatch {
245 int (*data_send)(struct client *cl, const char *data, int len);
246 void (*data_done)(struct client *cl);
247 void (*write_cb)(struct client *cl);
248 void (*close_fds)(struct client *cl);
249 void (*free)(struct client *cl);
250
251 void *req_data;
252 void (*req_free)(struct client *cl);
253
254 bool data_blocked;
255 bool no_cache;
256
257 union {
258 struct {
259 struct blob_attr **hdr;
260 int fd;
261 } file;
262 struct dispatch_proc proc;
263 #ifdef HAVE_UBUS
264 struct dispatch_ubus ubus;
265 #endif
266 };
267 };
268
269 struct client {
270 struct list_head list;
271 int refcount;
272 int id;
273
274 struct ustream *us;
275 struct ustream_fd sfd;
276 #ifdef HAVE_TLS
277 struct ustream_ssl ssl;
278 #endif
279 struct uloop_timeout timeout;
280 int requests;
281
282 enum client_state state;
283 bool tls;
284
285 int http_code;
286 struct http_request request;
287 struct uh_addr srv_addr, peer_addr;
288
289 struct blob_buf hdr;
290 struct blob_buf hdr_response;
291 struct dispatch dispatch;
292 };
293
294 extern char uh_buf[4096];
295 extern int n_clients;
296 extern struct config conf;
297 extern const char * const http_versions[];
298 extern const char * const http_methods[];
299 extern struct dispatch_handler cgi_dispatch;
300
301 void uh_index_add(const char *filename);
302
303 bool uh_accept_client(int fd, bool tls);
304
305 void uh_unblock_listeners(void);
306 void uh_setup_listeners(void);
307 int uh_socket_bind(const char *host, const char *port, bool tls);
308
309 int uh_first_tls_port(int family);
310
311 bool uh_use_chunked(struct client *cl);
312 void uh_chunk_write(struct client *cl, const void *data, int len);
313 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
314
315 void __printf(2, 3)
316 uh_chunk_printf(struct client *cl, const char *format, ...);
317
318 void uh_chunk_eof(struct client *cl);
319 void uh_request_done(struct client *cl);
320
321 void uh_http_header(struct client *cl, int code, const char *summary);
322 void __printf(4, 5)
323 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
324
325 void uh_handle_request(struct client *cl);
326 void client_poll_post_data(struct client *cl);
327 void uh_client_read_cb(struct client *cl);
328 void uh_client_notify_state(struct client *cl);
329
330 void uh_auth_add(const char *path, const char *user, const char *pass);
331 bool uh_auth_check(struct client *cl, const char *path, const char *auth,
332 char **uptr, char **pptr);
333
334 void uh_close_listen_fds(void);
335 void uh_close_fds(void);
336
337 void uh_interpreter_add(const char *ext, const char *path);
338 void uh_dispatch_add(struct dispatch_handler *d);
339
340 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
341 void uh_relay_close(struct relay *r, int ret);
342 void uh_relay_free(struct relay *r);
343 void uh_relay_kill(struct client *cl, struct relay *r);
344
345 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
346 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
347 void (*cb)(struct client *cl, struct path_info *pi, char *url));
348
349 int uh_plugin_init(const char *name);
350 void uh_plugin_post_init(void);
351
352 int uh_handler_add(const char *file);
353 int uh_handler_run(struct client *cl, char **url, bool fallback);
354
355 struct path_info *uh_path_lookup(struct client *cl, const char *url);
356
357 static inline void uh_client_ref(struct client *cl)
358 {
359 cl->refcount++;
360 }
361
362 static inline void uh_client_unref(struct client *cl)
363 {
364 if (--cl->refcount)
365 return;
366
367 if (cl->state == CLIENT_STATE_CLEANUP)
368 ustream_state_change(cl->us);
369 }
370
371 #endif