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