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