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