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/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
;
56 const char *lua_handler
;
57 const char *lua_prefix
;
58 const char *ubus_prefix
;
59 const char *ubus_socket
;
65 int max_script_requests
;
73 struct list_head list
;
91 enum http_user_agent
{
103 struct http_request
{
104 enum http_method method
;
105 enum http_version version
;
106 enum http_user_agent ua
;
110 bool connection_close
;
111 uint8_t transfer_chunked
;
112 const struct auth_realm
*realm
;
124 struct list_head list
;
138 const struct interpreter
*ip
;
147 struct ustream_fd sfd
;
148 struct uloop_process proc
;
149 struct uloop_timeout timeout
;
157 void (*header_cb
)(struct relay
*r
, const char *name
, const char *value
);
158 void (*header_end
)(struct relay
*r
);
159 void (*close
)(struct relay
*r
, int ret
);
162 struct dispatch_proc
{
163 struct uloop_timeout timeout
;
165 struct uloop_fd wrfd
;
171 struct dispatch_handler
{
172 struct list_head list
;
175 bool (*check_url
)(const char *url
);
176 bool (*check_path
)(struct path_info
*pi
, const char *url
);
177 void (*handle_request
)(struct client
*cl
, char *url
, struct path_info
*pi
);
181 struct dispatch_ubus
{
182 struct ubus_request req
;
184 struct uloop_timeout timeout
;
185 struct json_tokener
*jstok
;
186 struct json_object
*jsobj
;
187 struct json_object
*jsobj_cur
;
201 int (*data_send
)(struct client
*cl
, const char *data
, int len
);
202 void (*data_done
)(struct client
*cl
);
203 void (*write_cb
)(struct client
*cl
);
204 void (*close_fds
)(struct client
*cl
);
205 void (*free
)(struct client
*cl
);
208 void (*req_free
)(struct client
*cl
);
214 struct blob_attr
**hdr
;
217 struct dispatch_proc proc
;
219 struct dispatch_ubus ubus
;
225 struct list_head list
;
229 struct ustream_fd sfd
;
231 struct ustream_ssl ssl
;
233 struct uloop_timeout timeout
;
236 enum client_state state
;
239 struct http_request request
;
240 struct uh_addr srv_addr
, peer_addr
;
243 struct dispatch dispatch
;
246 extern char uh_buf
[4096];
247 extern int n_clients
;
248 extern struct config conf
;
249 extern const char * const http_versions
[];
250 extern const char * const http_methods
[];
251 extern struct dispatch_handler cgi_dispatch
;
253 void uh_index_add(const char *filename
);
255 bool uh_accept_client(int fd
, bool tls
);
257 void uh_unblock_listeners(void);
258 void uh_setup_listeners(void);
259 int uh_socket_bind(const char *host
, const char *port
, bool tls
);
261 bool uh_use_chunked(struct client
*cl
);
262 void uh_chunk_write(struct client
*cl
, const void *data
, int len
);
263 void uh_chunk_vprintf(struct client
*cl
, const char *format
, va_list arg
);
266 uh_chunk_printf(struct client
*cl
, const char *format
, ...);
268 void uh_chunk_eof(struct client
*cl
);
269 void uh_request_done(struct client
*cl
);
271 void uh_http_header(struct client
*cl
, int code
, const char *summary
);
273 uh_client_error(struct client
*cl
, int code
, const char *summary
, const char *fmt
, ...);
275 void uh_handle_request(struct client
*cl
);
276 void client_poll_post_data(struct client
*cl
);
277 void uh_client_read_cb(struct client
*cl
);
278 void uh_client_notify_state(struct client
*cl
);
280 void uh_auth_add(const char *path
, const char *user
, const char *pass
);
281 bool uh_auth_check(struct client
*cl
, struct path_info
*pi
);
283 void uh_close_listen_fds(void);
284 void uh_close_fds(void);
286 void uh_interpreter_add(const char *ext
, const char *path
);
287 void uh_dispatch_add(struct dispatch_handler
*d
);
289 void uh_relay_open(struct client
*cl
, struct relay
*r
, int fd
, int pid
);
290 void uh_relay_close(struct relay
*r
, int ret
);
291 void uh_relay_free(struct relay
*r
);
292 void uh_relay_kill(struct client
*cl
, struct relay
*r
);
294 struct env_var
*uh_get_process_vars(struct client
*cl
, struct path_info
*pi
);
295 bool uh_create_process(struct client
*cl
, struct path_info
*pi
, char *url
,
296 void (*cb
)(struct client
*cl
, struct path_info
*pi
, char *url
));
298 int uh_plugin_init(const char *name
);
299 void uh_plugin_post_init(void);