2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
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.
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.
23 #include <netinet/in.h>
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>
34 #include <json-c/json.h>
37 #include <libubox/ustream-ssl.h>
42 #define UH_LIMIT_CLIENTS 64
44 #define __enum_header(_name, _val) HDR_##_name,
45 #define __blobmsg_header(_name, _val) [HDR_##_name] = { .name = #_val, .type = BLOBMSG_TYPE_STRING },
53 const char *error_handler;
54 const char *cgi_prefix;
55 const char *cgi_docroot_path;
57 const char *lua_handler;
58 const char *lua_prefix;
59 const char *ubus_prefix;
60 const char *ubus_socket;
67 int max_script_requests;
76 struct list_head list;
95 enum http_user_agent {
107 struct http_request {
108 enum http_method method;
109 enum http_version version;
110 enum http_user_agent ua;
114 bool connection_close;
115 bool disable_chunked;
116 uint8_t transfer_chunked;
117 const struct auth_realm *realm;
126 CLIENT_STATE_CLEANUP,
130 struct list_head list;
144 const struct interpreter *ip;
153 struct ustream_fd sfd;
154 struct uloop_process proc;
155 struct uloop_timeout timeout;
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);
170 struct dispatch_proc {
171 struct uloop_timeout timeout;
173 struct uloop_fd wrfd;
179 struct dispatch_handler {
180 struct list_head list;
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);
189 struct dispatch_ubus {
190 struct ubus_request req;
192 struct uloop_timeout timeout;
193 struct json_tokener *jstok;
194 struct json_object *jsobj;
195 struct json_object *jsobj_cur;
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);
216 void (*req_free)(struct client *cl);
222 struct blob_attr **hdr;
225 struct dispatch_proc proc;
227 struct dispatch_ubus ubus;
233 struct list_head list;
238 struct ustream_fd sfd;
240 struct ustream_ssl ssl;
242 struct uloop_timeout timeout;
245 enum client_state state;
249 struct http_request request;
250 struct uh_addr srv_addr, peer_addr;
253 struct dispatch dispatch;
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;
263 void uh_index_add(const char *filename);
265 bool uh_accept_client(int fd, bool tls);
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);
271 int uh_first_tls_port(int family);
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);
278 uh_chunk_printf(struct client *cl, const char *format, ...);
280 void uh_chunk_eof(struct client *cl);
281 void uh_request_done(struct client *cl);
283 void uh_http_header(struct client *cl, int code, const char *summary);
285 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
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);
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);
295 void uh_close_listen_fds(void);
296 void uh_close_fds(void);
298 void uh_interpreter_add(const char *ext, const char *path);
299 void uh_dispatch_add(struct dispatch_handler *d);
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);
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));
310 int uh_plugin_init(const char *name);
311 void uh_plugin_post_init(void);
313 static inline void uh_client_ref(struct client *cl)
318 static inline void uh_client_unref(struct client *cl)
323 if (cl->state == CLIENT_STATE_CLEANUP)
324 ustream_state_change(cl->us);