proc: expose HTTP Origin header in process environment
[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         bool redirected;
150         struct stat stat;
151         const struct interpreter *ip;
152 };
153
154 struct env_var {
155         const char *name;
156         const char *value;
157 };
158
159 struct relay {
160         struct ustream_fd sfd;
161         struct uloop_process proc;
162         struct uloop_timeout timeout;
163         struct client *cl;
164
165         bool process_done;
166         bool error;
167         bool skip_data;
168
169         int ret;
170         int header_ofs;
171
172         void (*header_cb)(struct relay *r, const char *name, const char *value);
173         void (*header_end)(struct relay *r);
174         void (*close)(struct relay *r, int ret);
175 };
176
177 struct dispatch_proc {
178         struct uloop_timeout timeout;
179         struct blob_buf hdr;
180         struct uloop_fd wrfd;
181         struct relay r;
182         int status_code;
183         char *status_msg;
184 };
185
186 struct dispatch_handler {
187         struct list_head list;
188         bool script;
189
190         bool (*check_url)(const char *url);
191         bool (*check_path)(struct path_info *pi, const char *url);
192         void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
193 };
194
195 #ifdef HAVE_UBUS
196 struct dispatch_ubus {
197         struct ubus_request req;
198
199         struct uloop_timeout timeout;
200         struct json_tokener *jstok;
201         struct json_object *jsobj;
202         struct json_object *jsobj_cur;
203         int post_len;
204
205         uint32_t obj;
206         const char *func;
207
208         struct blob_buf buf;
209         bool req_pending;
210         bool array;
211         int array_idx;
212 };
213 #endif
214
215 struct dispatch {
216         int (*data_send)(struct client *cl, const char *data, int len);
217         void (*data_done)(struct client *cl);
218         void (*write_cb)(struct client *cl);
219         void (*close_fds)(struct client *cl);
220         void (*free)(struct client *cl);
221
222         void *req_data;
223         void (*req_free)(struct client *cl);
224
225         bool data_blocked;
226         bool no_cache;
227
228         union {
229                 struct {
230                         struct blob_attr **hdr;
231                         int fd;
232                 } file;
233                 struct dispatch_proc proc;
234 #ifdef HAVE_UBUS
235                 struct dispatch_ubus ubus;
236 #endif
237         };
238 };
239
240 struct client {
241         struct list_head list;
242         int refcount;
243         int id;
244
245         struct ustream *us;
246         struct ustream_fd sfd;
247 #ifdef HAVE_TLS
248         struct ustream_ssl ssl;
249 #endif
250         struct uloop_timeout timeout;
251         int requests;
252
253         enum client_state state;
254         bool tls;
255
256         int http_code;
257         struct http_request request;
258         struct uh_addr srv_addr, peer_addr;
259
260         struct blob_buf hdr;
261         struct blob_buf hdr_response;
262         struct dispatch dispatch;
263 };
264
265 extern char uh_buf[4096];
266 extern int n_clients;
267 extern struct config conf;
268 extern const char * const http_versions[];
269 extern const char * const http_methods[];
270 extern struct dispatch_handler cgi_dispatch;
271
272 void uh_index_add(const char *filename);
273
274 bool uh_accept_client(int fd, bool tls);
275
276 void uh_unblock_listeners(void);
277 void uh_setup_listeners(void);
278 int uh_socket_bind(const char *host, const char *port, bool tls);
279
280 int uh_first_tls_port(int family);
281
282 bool uh_use_chunked(struct client *cl);
283 void uh_chunk_write(struct client *cl, const void *data, int len);
284 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
285
286 void __printf(2, 3)
287 uh_chunk_printf(struct client *cl, const char *format, ...);
288
289 void uh_chunk_eof(struct client *cl);
290 void uh_request_done(struct client *cl);
291
292 void uh_http_header(struct client *cl, int code, const char *summary);
293 void __printf(4, 5)
294 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
295
296 void uh_handle_request(struct client *cl);
297 void client_poll_post_data(struct client *cl);
298 void uh_client_read_cb(struct client *cl);
299 void uh_client_notify_state(struct client *cl);
300
301 void uh_auth_add(const char *path, const char *user, const char *pass);
302 bool uh_auth_check(struct client *cl, const char *path, const char *auth,
303                    char **uptr, char **pptr);
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