add basic tls support, todo: error handling
[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_TLS
33 #include <libubox/ustream-ssl.h>
34 #endif
35
36 #include "utils.h"
37
38 #define UH_LIMIT_CLIENTS 64
39
40 #define __enum_header(_name) HDR_##_name,
41 #define __blobmsg_header(_name) [HDR_##_name] = { .name = #_name, .type = BLOBMSG_TYPE_STRING },
42
43 struct client;
44
45 struct config {
46 const char *docroot;
47 const char *realm;
48 const char *file;
49 const char *error_handler;
50 const char *cgi_prefix;
51 const char *cgi_path;
52 int no_symlinks;
53 int no_dirlists;
54 int network_timeout;
55 int rfc1918_filter;
56 int tcp_keepalive;
57 int max_requests;
58 int http_keepalive;
59 int script_timeout;
60 };
61
62 struct auth_realm {
63 struct list_head list;
64 const char *path;
65 const char *user;
66 const char *pass;
67 };
68
69 enum http_method {
70 UH_HTTP_MSG_GET,
71 UH_HTTP_MSG_POST,
72 UH_HTTP_MSG_HEAD,
73 };
74
75 enum http_version {
76 UH_HTTP_VER_0_9,
77 UH_HTTP_VER_1_0,
78 UH_HTTP_VER_1_1,
79 };
80
81 struct http_request {
82 enum http_method method;
83 enum http_version version;
84 int redirect_status;
85 int content_length;
86 bool expect_cont;
87 uint8_t transfer_chunked;
88 const struct auth_realm *realm;
89 };
90
91 enum client_state {
92 CLIENT_STATE_INIT,
93 CLIENT_STATE_HEADER,
94 CLIENT_STATE_DATA,
95 CLIENT_STATE_DONE,
96 CLIENT_STATE_CLOSE,
97 };
98
99 struct interpreter {
100 struct list_head list;
101 const char *path;
102 const char *ext;
103 };
104
105 struct path_info {
106 const char *root;
107 const char *phys;
108 const char *name;
109 const char *info;
110 const char *query;
111 const char *auth;
112 bool redirected;
113 struct stat stat;
114 const struct interpreter *ip;
115 };
116
117 struct env_var {
118 const char *name;
119 const char *value;
120 };
121
122 struct relay {
123 struct ustream_fd sfd;
124 struct uloop_process proc;
125 struct client *cl;
126
127 bool process_done;
128 int ret;
129 int header_ofs;
130
131 void (*header_cb)(struct relay *r, const char *name, const char *value);
132 void (*header_end)(struct relay *r);
133 void (*close)(struct relay *r, int ret);
134 };
135
136 struct dispatch_proc {
137 struct blob_buf hdr;
138 struct uloop_fd wrfd;
139 struct relay r;
140 int status_code;
141 char *status_msg;
142 };
143
144 struct dispatch_handler {
145 struct list_head list;
146
147 bool (*check_url)(const char *url);
148 bool (*check_path)(struct path_info *pi, const char *url);
149 void (*handle_request)(struct client *cl, const char *url, struct path_info *pi);
150 };
151
152 struct dispatch {
153 int (*data_send)(struct client *cl, const char *data, int len);
154 void (*data_done)(struct client *cl);
155 void (*write_cb)(struct client *cl);
156 void (*close_fds)(struct client *cl);
157 void (*free)(struct client *cl);
158 bool data_blocked;
159
160 union {
161 struct {
162 struct blob_attr **hdr;
163 int fd;
164 } file;
165 struct dispatch_proc proc;
166 };
167 };
168
169 struct client {
170 struct list_head list;
171 int id;
172
173 struct ustream *us;
174 struct ustream_fd sfd;
175 #ifdef HAVE_TLS
176 struct ustream_ssl ssl;
177 #endif
178 struct uloop_timeout timeout;
179
180 enum client_state state;
181 bool tls;
182
183 struct http_request request;
184 struct uh_addr srv_addr, peer_addr;
185
186 struct blob_buf hdr;
187 struct dispatch dispatch;
188 };
189
190 extern char uh_buf[4096];
191 extern int n_clients;
192 extern struct config conf;
193 extern const char * const http_versions[];
194 extern const char * const http_methods[];
195 extern struct dispatch_handler cgi_dispatch;
196
197 void uh_index_add(const char *filename);
198
199 bool uh_accept_client(int fd, bool tls);
200
201 void uh_unblock_listeners(void);
202 void uh_setup_listeners(void);
203 int uh_socket_bind(const char *host, const char *port, bool tls);
204
205 bool uh_use_chunked(struct client *cl);
206 void uh_chunk_write(struct client *cl, const void *data, int len);
207 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
208
209 void __printf(2, 3)
210 uh_chunk_printf(struct client *cl, const char *format, ...);
211
212 void uh_chunk_eof(struct client *cl);
213 void uh_request_done(struct client *cl);
214
215 void uh_http_header(struct client *cl, int code, const char *summary);
216 void __printf(4, 5)
217 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
218
219 void uh_handle_request(struct client *cl);
220 void client_poll_post_data(struct client *cl);
221 void uh_client_read_cb(struct client *cl);
222 void uh_client_notify_state(struct client *cl);
223
224 void uh_auth_add(const char *path, const char *user, const char *pass);
225 bool uh_auth_check(struct client *cl, struct path_info *pi);
226
227 void uh_close_listen_fds(void);
228 void uh_close_fds(void);
229
230 void uh_interpreter_add(const char *ext, const char *path);
231 void uh_dispatch_add(struct dispatch_handler *d);
232
233 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
234 void uh_relay_close(struct relay *r, int ret);
235 void uh_relay_free(struct relay *r);
236
237 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
238 bool uh_create_process(struct client *cl, struct path_info *pi,
239 void (*cb)(struct client *cl, struct path_info *pi));
240
241 #endif