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