set the docroot to the current working directory if none is specified, fixes random...
[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 };
122
123 struct interpreter {
124         struct list_head list;
125         const char *path;
126         const char *ext;
127 };
128
129 struct path_info {
130         const char *root;
131         const char *phys;
132         const char *name;
133         const char *info;
134         const char *query;
135         const char *auth;
136         bool redirected;
137         struct stat stat;
138         const struct interpreter *ip;
139 };
140
141 struct env_var {
142         const char *name;
143         const char *value;
144 };
145
146 struct relay {
147         struct ustream_fd sfd;
148         struct uloop_process proc;
149         struct client *cl;
150
151         bool process_done;
152         int ret;
153         int header_ofs;
154
155         void (*header_cb)(struct relay *r, const char *name, const char *value);
156         void (*header_end)(struct relay *r);
157         void (*close)(struct relay *r, int ret);
158 };
159
160 struct dispatch_proc {
161         struct uloop_timeout timeout;
162         struct blob_buf hdr;
163         struct uloop_fd wrfd;
164         struct relay r;
165         int status_code;
166         char *status_msg;
167 };
168
169 struct dispatch_handler {
170         struct list_head list;
171         bool script;
172
173         bool (*check_url)(const char *url);
174         bool (*check_path)(struct path_info *pi, const char *url);
175         void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
176 };
177
178 #ifdef HAVE_UBUS
179 struct dispatch_ubus {
180         struct ubus_request req;
181
182         struct uloop_timeout timeout;
183         struct json_tokener *jstok;
184         struct json_object *jsobj;
185         struct json_object *jsobj_cur;
186         int post_len;
187
188         const char *sid;
189         uint32_t obj;
190         const char *func;
191
192         struct blob_buf buf;
193         bool req_pending;
194         bool array;
195         int array_idx;
196 };
197 #endif
198
199 struct dispatch {
200         int (*data_send)(struct client *cl, const char *data, int len);
201         void (*data_done)(struct client *cl);
202         void (*write_cb)(struct client *cl);
203         void (*close_fds)(struct client *cl);
204         void (*free)(struct client *cl);
205
206         void *req_data;
207         void (*req_free)(struct client *cl);
208
209         bool data_blocked;
210
211         union {
212                 struct {
213                         struct blob_attr **hdr;
214                         int fd;
215                 } file;
216                 struct dispatch_proc proc;
217 #ifdef HAVE_UBUS
218                 struct dispatch_ubus ubus;
219 #endif
220         };
221 };
222
223 struct client {
224         struct list_head list;
225         int id;
226
227         struct ustream *us;
228         struct ustream_fd sfd;
229 #ifdef HAVE_TLS
230         struct ustream_ssl ssl;
231 #endif
232         struct uloop_timeout timeout;
233         int requests;
234
235         enum client_state state;
236         bool tls;
237
238         struct http_request request;
239         struct uh_addr srv_addr, peer_addr;
240
241         struct blob_buf hdr;
242         struct dispatch dispatch;
243 };
244
245 extern char uh_buf[4096];
246 extern int n_clients;
247 extern struct config conf;
248 extern const char * const http_versions[];
249 extern const char * const http_methods[];
250 extern struct dispatch_handler cgi_dispatch;
251
252 void uh_index_add(const char *filename);
253
254 bool uh_accept_client(int fd, bool tls);
255
256 void uh_unblock_listeners(void);
257 void uh_setup_listeners(void);
258 int uh_socket_bind(const char *host, const char *port, bool tls);
259
260 bool uh_use_chunked(struct client *cl);
261 void uh_chunk_write(struct client *cl, const void *data, int len);
262 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
263
264 void __printf(2, 3)
265 uh_chunk_printf(struct client *cl, const char *format, ...);
266
267 void uh_chunk_eof(struct client *cl);
268 void uh_request_done(struct client *cl);
269
270 void uh_http_header(struct client *cl, int code, const char *summary);
271 void __printf(4, 5)
272 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
273
274 void uh_handle_request(struct client *cl);
275 void client_poll_post_data(struct client *cl);
276 void uh_client_read_cb(struct client *cl);
277 void uh_client_notify_state(struct client *cl);
278
279 void uh_auth_add(const char *path, const char *user, const char *pass);
280 bool uh_auth_check(struct client *cl, struct path_info *pi);
281
282 void uh_close_listen_fds(void);
283 void uh_close_fds(void);
284
285 void uh_interpreter_add(const char *ext, const char *path);
286 void uh_dispatch_add(struct dispatch_handler *d);
287
288 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
289 void uh_relay_close(struct relay *r, int ret);
290 void uh_relay_free(struct relay *r);
291 void uh_relay_kill(struct client *cl, struct relay *r);
292
293 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
294 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
295                        void (*cb)(struct client *cl, struct path_info *pi, char *url));
296
297 int uh_plugin_init(const char *name);
298 void uh_plugin_post_init(void);
299
300 #endif