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>
33 #include <libubox/ustream-ssl.h>
38 #define UH_LIMIT_CLIENTS 64
40 #define __enum_header(_name) HDR_##_name,
41 #define __blobmsg_header(_name) [HDR_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_STRING },
49 const char *error_handler;
50 const char *cgi_prefix;
52 const char *lua_handler;
53 const char *lua_prefix;
65 struct list_head list;
84 enum http_method method;
85 enum http_version version;
89 uint8_t transfer_chunked;
90 const struct auth_realm *realm;
102 struct list_head list;
116 const struct interpreter *ip;
125 struct ustream_fd sfd;
126 struct uloop_process proc;
133 void (*header_cb)(struct relay *r, const char *name, const char *value);
134 void (*header_end)(struct relay *r);
135 void (*close)(struct relay *r, int ret);
138 struct dispatch_proc {
140 struct uloop_fd wrfd;
146 struct dispatch_handler {
147 struct list_head list;
149 bool (*check_url)(const char *url);
150 bool (*check_path)(struct path_info *pi, const char *url);
151 void (*handle_request)(struct client *cl, char *url, struct path_info *pi);
155 int (*data_send)(struct client *cl, const char *data, int len);
156 void (*data_done)(struct client *cl);
157 void (*write_cb)(struct client *cl);
158 void (*close_fds)(struct client *cl);
159 void (*free)(struct client *cl);
164 struct blob_attr **hdr;
167 struct dispatch_proc proc;
172 struct list_head list;
176 struct ustream_fd sfd;
178 struct ustream_ssl ssl;
180 struct uloop_timeout timeout;
182 enum client_state state;
185 struct http_request request;
186 struct uh_addr srv_addr, peer_addr;
189 struct dispatch dispatch;
192 extern char uh_buf[4096];
193 extern int n_clients;
194 extern struct config conf;
195 extern const char * const http_versions[];
196 extern const char * const http_methods[];
197 extern struct dispatch_handler cgi_dispatch;
199 void uh_index_add(const char *filename);
201 bool uh_accept_client(int fd, bool tls);
203 void uh_unblock_listeners(void);
204 void uh_setup_listeners(void);
205 int uh_socket_bind(const char *host, const char *port, bool tls);
207 bool uh_use_chunked(struct client *cl);
208 void uh_chunk_write(struct client *cl, const void *data, int len);
209 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
212 uh_chunk_printf(struct client *cl, const char *format, ...);
214 void uh_chunk_eof(struct client *cl);
215 void uh_request_done(struct client *cl);
217 void uh_http_header(struct client *cl, int code, const char *summary);
219 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
221 void uh_handle_request(struct client *cl);
222 void client_poll_post_data(struct client *cl);
223 void uh_client_read_cb(struct client *cl);
224 void uh_client_notify_state(struct client *cl);
226 void uh_auth_add(const char *path, const char *user, const char *pass);
227 bool uh_auth_check(struct client *cl, struct path_info *pi);
229 void uh_close_listen_fds(void);
230 void uh_close_fds(void);
232 void uh_interpreter_add(const char *ext, const char *path);
233 void uh_dispatch_add(struct dispatch_handler *d);
235 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
236 void uh_relay_close(struct relay *r, int ret);
237 void uh_relay_free(struct relay *r);
239 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
240 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
241 void (*cb)(struct client *cl, struct path_info *pi, char *url));
243 int uh_plugin_init(const char *name);