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