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