12cfc287676d870c51d2c5587b6f160d3fe36cf5
[project/uhttpd.git] / uhttpd.h
1 /*
2  * uhttpd - Tiny single-threaded httpd - Main header
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
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) HDR_##_name,
45 #define __blobmsg_header(_name) [HDR_##_name] = { .name = #_name, .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 struct http_request {
90         enum http_method method;
91         enum http_version version;
92         int redirect_status;
93         int content_length;
94         bool expect_cont;
95         uint8_t transfer_chunked;
96         const struct auth_realm *realm;
97 };
98
99 enum client_state {
100         CLIENT_STATE_INIT,
101         CLIENT_STATE_HEADER,
102         CLIENT_STATE_DATA,
103         CLIENT_STATE_DONE,
104         CLIENT_STATE_CLOSE,
105 };
106
107 struct interpreter {
108         struct list_head list;
109         const char *path;
110         const char *ext;
111 };
112
113 struct path_info {
114         const char *root;
115         const char *phys;
116         const char *name;
117         const char *info;
118         const char *query;
119         const char *auth;
120         bool redirected;
121         struct stat stat;
122         const struct interpreter *ip;
123 };
124
125 struct env_var {
126         const char *name;
127         const char *value;
128 };
129
130 struct relay {
131         struct ustream_fd sfd;
132         struct uloop_process proc;
133         struct client *cl;
134
135         bool process_done;
136         int ret;
137         int header_ofs;
138
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);
142 };
143
144 struct dispatch_proc {
145         struct blob_buf hdr;
146         struct uloop_fd wrfd;
147         struct relay r;
148         int status_code;
149         char *status_msg;
150 };
151
152 struct dispatch_handler {
153         struct list_head list;
154
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);
158 };
159
160 #ifdef HAVE_UBUS
161 struct dispatch_ubus {
162         struct ubus_request req;
163
164         struct uloop_timeout timeout;
165         struct json_tokener *jstok;
166         struct json_object *jsobj;
167         struct json_object *jsobj_cur;
168         int post_len;
169
170         const char *sid;
171         uint32_t obj;
172         const char *func;
173
174         struct blob_buf buf;
175         bool req_pending;
176         bool array;
177         int array_idx;
178 };
179 #endif
180
181 struct dispatch {
182         int (*data_send)(struct client *cl, const char *data, int len);
183         void (*data_done)(struct client *cl);
184         void (*write_cb)(struct client *cl);
185         void (*close_fds)(struct client *cl);
186         void (*free)(struct client *cl);
187         bool data_blocked;
188
189         union {
190                 struct {
191                         struct blob_attr **hdr;
192                         int fd;
193                 } file;
194                 struct dispatch_proc proc;
195 #ifdef HAVE_UBUS
196                 struct dispatch_ubus ubus;
197 #endif
198         };
199 };
200
201 struct client {
202         struct list_head list;
203         int id;
204
205         struct ustream *us;
206         struct ustream_fd sfd;
207 #ifdef HAVE_TLS
208         struct ustream_ssl ssl;
209 #endif
210         struct uloop_timeout timeout;
211
212         enum client_state state;
213         bool tls;
214
215         struct http_request request;
216         struct uh_addr srv_addr, peer_addr;
217
218         struct blob_buf hdr;
219         struct dispatch dispatch;
220 };
221
222 extern char uh_buf[4096];
223 extern int n_clients;
224 extern struct config conf;
225 extern const char * const http_versions[];
226 extern const char * const http_methods[];
227 extern struct dispatch_handler cgi_dispatch;
228
229 void uh_index_add(const char *filename);
230
231 bool uh_accept_client(int fd, bool tls);
232
233 void uh_unblock_listeners(void);
234 void uh_setup_listeners(void);
235 int uh_socket_bind(const char *host, const char *port, bool tls);
236
237 bool uh_use_chunked(struct client *cl);
238 void uh_chunk_write(struct client *cl, const void *data, int len);
239 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
240
241 void __printf(2, 3)
242 uh_chunk_printf(struct client *cl, const char *format, ...);
243
244 void uh_chunk_eof(struct client *cl);
245 void uh_request_done(struct client *cl);
246
247 void uh_http_header(struct client *cl, int code, const char *summary);
248 void __printf(4, 5)
249 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
250
251 void uh_handle_request(struct client *cl);
252 void client_poll_post_data(struct client *cl);
253 void uh_client_read_cb(struct client *cl);
254 void uh_client_notify_state(struct client *cl);
255
256 void uh_auth_add(const char *path, const char *user, const char *pass);
257 bool uh_auth_check(struct client *cl, struct path_info *pi);
258
259 void uh_close_listen_fds(void);
260 void uh_close_fds(void);
261
262 void uh_interpreter_add(const char *ext, const char *path);
263 void uh_dispatch_add(struct dispatch_handler *d);
264
265 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
266 void uh_relay_close(struct relay *r, int ret);
267 void uh_relay_free(struct relay *r);
268
269 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
270 bool uh_create_process(struct client *cl, struct path_info *pi, char *url,
271                        void (*cb)(struct client *cl, struct path_info *pi, char *url));
272
273 int uh_plugin_init(const char *name);
274 void uh_plugin_post_init(void);
275
276 #endif