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