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