de-constify the url parameter for the handler, it becomes invalid after the request...
[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 const char *lua_handler;
53 const char *lua_prefix;
54 int no_symlinks;
55 int no_dirlists;
56 int network_timeout;
57 int rfc1918_filter;
58 int tcp_keepalive;
59 int max_requests;
60 int http_keepalive;
61 int script_timeout;
62 };
63
64 struct auth_realm {
65 struct list_head list;
66 const char *path;
67 const char *user;
68 const char *pass;
69 };
70
71 enum http_method {
72 UH_HTTP_MSG_GET,
73 UH_HTTP_MSG_POST,
74 UH_HTTP_MSG_HEAD,
75 };
76
77 enum http_version {
78 UH_HTTP_VER_0_9,
79 UH_HTTP_VER_1_0,
80 UH_HTTP_VER_1_1,
81 };
82
83 struct http_request {
84 enum http_method method;
85 enum http_version version;
86 int redirect_status;
87 int content_length;
88 bool expect_cont;
89 uint8_t transfer_chunked;
90 const struct auth_realm *realm;
91 };
92
93 enum client_state {
94 CLIENT_STATE_INIT,
95 CLIENT_STATE_HEADER,
96 CLIENT_STATE_DATA,
97 CLIENT_STATE_DONE,
98 CLIENT_STATE_CLOSE,
99 };
100
101 struct interpreter {
102 struct list_head list;
103 const char *path;
104 const char *ext;
105 };
106
107 struct path_info {
108 const char *root;
109 const char *phys;
110 const char *name;
111 const char *info;
112 const char *query;
113 const char *auth;
114 bool redirected;
115 struct stat stat;
116 const struct interpreter *ip;
117 };
118
119 struct env_var {
120 const char *name;
121 const char *value;
122 };
123
124 struct relay {
125 struct ustream_fd sfd;
126 struct uloop_process proc;
127 struct client *cl;
128
129 bool process_done;
130 int ret;
131 int header_ofs;
132
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);
136 };
137
138 struct dispatch_proc {
139 struct blob_buf hdr;
140 struct uloop_fd wrfd;
141 struct relay r;
142 int status_code;
143 char *status_msg;
144 };
145
146 struct dispatch_handler {
147 struct list_head list;
148
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);
152 };
153
154 struct dispatch {
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);
160 bool data_blocked;
161
162 union {
163 struct {
164 struct blob_attr **hdr;
165 int fd;
166 } file;
167 struct dispatch_proc proc;
168 };
169 };
170
171 struct client {
172 struct list_head list;
173 int id;
174
175 struct ustream *us;
176 struct ustream_fd sfd;
177 #ifdef HAVE_TLS
178 struct ustream_ssl ssl;
179 #endif
180 struct uloop_timeout timeout;
181
182 enum client_state state;
183 bool tls;
184
185 struct http_request request;
186 struct uh_addr srv_addr, peer_addr;
187
188 struct blob_buf hdr;
189 struct dispatch dispatch;
190 };
191
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;
198
199 void uh_index_add(const char *filename);
200
201 bool uh_accept_client(int fd, bool tls);
202
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);
206
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);
210
211 void __printf(2, 3)
212 uh_chunk_printf(struct client *cl, const char *format, ...);
213
214 void uh_chunk_eof(struct client *cl);
215 void uh_request_done(struct client *cl);
216
217 void uh_http_header(struct client *cl, int code, const char *summary);
218 void __printf(4, 5)
219 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
220
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);
225
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);
228
229 void uh_close_listen_fds(void);
230 void uh_close_fds(void);
231
232 void uh_interpreter_add(const char *ext, const char *path);
233 void uh_dispatch_add(struct dispatch_handler *d);
234
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);
238
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));
242
243 int uh_plugin_init(const char *name);
244
245 #endif