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