file: re-run json handler script after file fallback redirect
[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 bool no_cache;
228
229 union {
230 struct {
231 struct blob_attr **hdr;
232 int fd;
233 } file;
234 struct dispatch_proc proc;
235 #ifdef HAVE_UBUS
236 struct dispatch_ubus ubus;
237 #endif
238 };
239 };
240
241 struct client {
242 struct list_head list;
243 int refcount;
244 int id;
245
246 struct ustream *us;
247 struct ustream_fd sfd;
248 #ifdef HAVE_TLS
249 struct ustream_ssl ssl;
250 #endif
251 struct uloop_timeout timeout;
252 int requests;
253
254 enum client_state state;
255 bool tls;
256
257 int http_code;
258 struct http_request request;
259 struct uh_addr srv_addr, peer_addr;
260
261 struct blob_buf hdr;
262 struct blob_buf hdr_response;
263 struct dispatch dispatch;
264 };
265
266 extern char uh_buf[4096];
267 extern int n_clients;
268 extern struct config conf;
269 extern const char * const http_versions[];
270 extern const char * const http_methods[];
271 extern struct dispatch_handler cgi_dispatch;
272
273 void uh_index_add(const char *filename);
274
275 bool uh_accept_client(int fd, bool tls);
276
277 void uh_unblock_listeners(void);
278 void uh_setup_listeners(void);
279 int uh_socket_bind(const char *host, const char *port, bool tls);
280
281 int uh_first_tls_port(int family);
282
283 bool uh_use_chunked(struct client *cl);
284 void uh_chunk_write(struct client *cl, const void *data, int len);
285 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
286
287 void __printf(2, 3)
288 uh_chunk_printf(struct client *cl, const char *format, ...);
289
290 void uh_chunk_eof(struct client *cl);
291 void uh_request_done(struct client *cl);
292
293 void uh_http_header(struct client *cl, int code, const char *summary);
294 void __printf(4, 5)
295 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
296
297 void uh_handle_request(struct client *cl);
298 void client_poll_post_data(struct client *cl);
299 void uh_client_read_cb(struct client *cl);
300 void uh_client_notify_state(struct client *cl);
301
302 void uh_auth_add(const char *path, const char *user, const char *pass);
303 bool uh_auth_check(struct client *cl, struct path_info *pi);
304
305 void uh_close_listen_fds(void);
306 void uh_close_fds(void);
307
308 void uh_interpreter_add(const char *ext, const char *path);
309 void uh_dispatch_add(struct dispatch_handler *d);
310
311 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
312 void uh_relay_close(struct relay *r, int ret);
313 void uh_relay_free(struct relay *r);
314 void uh_relay_kill(struct client *cl, struct relay *r);
315
316 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
317 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
318 void (*cb)(struct client *cl, struct path_info *pi, char *url));
319
320 int uh_plugin_init(const char *name);
321 void uh_plugin_post_init(void);
322
323 int uh_handler_add(const char *file);
324 int uh_handler_run(struct client *cl, char **url, bool fallback);
325
326 struct path_info *uh_path_lookup(struct client *cl, const char *url);
327
328 static inline void uh_client_ref(struct client *cl)
329 {
330 cl->refcount++;
331 }
332
333 static inline void uh_client_unref(struct client *cl)
334 {
335 if (--cl->refcount)
336 return;
337
338 if (cl->state == CLIENT_STATE_CLEANUP)
339 ustream_state_change(cl->us);
340 }
341
342 #endif