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