2 * uhttpd - Tiny single-threaded httpd - Main header
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
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) HDR_##_name,
45 #define __blobmsg_header(_name) [HDR_##_name] = { .name = #_name, .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;
71 struct list_head list;
90 enum http_method method;
91 enum http_version version;
95 uint8_t transfer_chunked;
96 const struct auth_realm *realm;
108 struct list_head list;
122 const struct interpreter *ip;
131 struct ustream_fd sfd;
132 struct uloop_process proc;
139 void (*header_cb)(struct relay *r, const char *name, const char *value);
140 void (*header_end)(struct relay *r);
141 void (*close)(struct relay *r, int ret);
144 struct dispatch_proc {
146 struct uloop_fd wrfd;
152 struct dispatch_handler {
153 struct list_head list;
155 bool (*check_url)(const char *url);
156 bool (*check_path)(struct path_info *pi, const char *url);
157 void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
161 struct dispatch_ubus {
162 struct ubus_request req;
163 struct json_tokener *jstok;
164 struct json_object *jsobj;
174 int (*data_send)(struct client *cl, const char *data, int len);
175 void (*data_done)(struct client *cl);
176 void (*write_cb)(struct client *cl);
177 void (*close_fds)(struct client *cl);
178 void (*free)(struct client *cl);
183 struct blob_attr **hdr;
186 struct dispatch_proc proc;
188 struct dispatch_ubus ubus;
194 struct list_head list;
198 struct ustream_fd sfd;
200 struct ustream_ssl ssl;
202 struct uloop_timeout timeout;
204 enum client_state state;
207 struct http_request request;
208 struct uh_addr srv_addr, peer_addr;
211 struct dispatch dispatch;
214 extern char uh_buf[4096];
215 extern int n_clients;
216 extern struct config conf;
217 extern const char * const http_versions[];
218 extern const char * const http_methods[];
219 extern struct dispatch_handler cgi_dispatch;
221 void uh_index_add(const char *filename);
223 bool uh_accept_client(int fd, bool tls);
225 void uh_unblock_listeners(void);
226 void uh_setup_listeners(void);
227 int uh_socket_bind(const char *host, const char *port, bool tls);
229 bool uh_use_chunked(struct client *cl);
230 void uh_chunk_write(struct client *cl, const void *data, int len);
231 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
234 uh_chunk_printf(struct client *cl, const char *format, ...);
236 void uh_chunk_eof(struct client *cl);
237 void uh_request_done(struct client *cl);
239 void uh_http_header(struct client *cl, int code, const char *summary);
241 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
243 void uh_handle_request(struct client *cl);
244 void client_poll_post_data(struct client *cl);
245 void uh_client_read_cb(struct client *cl);
246 void uh_client_notify_state(struct client *cl);
248 void uh_auth_add(const char *path, const char *user, const char *pass);
249 bool uh_auth_check(struct client *cl, struct path_info *pi);
251 void uh_close_listen_fds(void);
252 void uh_close_fds(void);
254 void uh_interpreter_add(const char *ext, const char *path);
255 void uh_dispatch_add(struct dispatch_handler *d);
257 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
258 void uh_relay_close(struct relay *r, int ret);
259 void uh_relay_free(struct relay *r);
261 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
262 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
263 void (*cb)(struct client *cl, struct path_info *pi, char *url));
265 int uh_plugin_init(const char *name);
266 void uh_plugin_post_init(void);