add post data relaying
[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 int content_length;
83 bool expect_cont;
84 bool transfer_chunked;
85 const char *url;
86 const struct auth_realm *realm;
87 };
88
89 enum client_state {
90 CLIENT_STATE_INIT,
91 CLIENT_STATE_HEADER,
92 CLIENT_STATE_DATA,
93 CLIENT_STATE_DONE,
94 CLIENT_STATE_CLOSE,
95 };
96
97 struct interpreter {
98 struct list_head list;
99 const char *path;
100 const char *ext;
101 };
102
103 struct path_info {
104 const char *root;
105 const char *phys;
106 const char *name;
107 const char *info;
108 const char *query;
109 const char *auth;
110 bool redirected;
111 struct stat stat;
112 const struct interpreter *ip;
113 };
114
115 struct env_var {
116 const char *name;
117 const char *value;
118 };
119
120 struct relay {
121 struct ustream_fd sfd;
122 struct uloop_process proc;
123 struct client *cl;
124
125 bool process_done;
126 int ret;
127 int header_ofs;
128
129 void (*header_cb)(struct relay *r, const char *name, const char *value);
130 void (*header_end)(struct relay *r);
131 void (*close)(struct relay *r, int ret);
132 };
133
134 struct dispatch_handler {
135 struct list_head list;
136
137 bool (*check_url)(const char *url);
138 bool (*check_path)(struct path_info *pi, const char *url);
139 void (*handle_request)(struct client *cl, const char *url, struct path_info *pi);
140 };
141
142 struct dispatch {
143 void (*data_send)(struct client *cl, const char *data, int len);
144 void (*data_done)(struct client *cl);
145 void (*write_cb)(struct client *cl);
146 void (*close_fds)(struct client *cl);
147 void (*free)(struct client *cl);
148 union {
149 struct {
150 struct blob_attr **hdr;
151 int fd;
152 } file;
153 struct {
154 struct blob_buf hdr;
155 struct relay r;
156 int status_code;
157 char *status_msg;
158 } proc;
159 };
160 };
161
162 struct client {
163 struct list_head list;
164 int id;
165
166 struct ustream *us;
167 struct ustream_fd sfd;
168 #ifdef HAVE_TLS
169 struct ustream_ssl stream_ssl;
170 #endif
171 struct uloop_timeout timeout;
172
173 enum client_state state;
174
175 struct http_request request;
176 struct uh_addr srv_addr, peer_addr;
177
178 struct blob_buf hdr;
179 struct dispatch dispatch;
180 };
181
182 extern char uh_buf[4096];
183 extern int n_clients;
184 extern struct config conf;
185 extern const char * const http_versions[];
186 extern const char * const http_methods[];
187 extern struct dispatch_handler cgi_dispatch;
188
189 void uh_index_add(const char *filename);
190
191 void uh_accept_client(int fd);
192
193 void uh_unblock_listeners(void);
194 void uh_setup_listeners(void);
195 int uh_socket_bind(const char *host, const char *port, bool tls);
196
197 bool uh_use_chunked(struct client *cl);
198 void uh_chunk_write(struct client *cl, const void *data, int len);
199 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
200
201 void __printf(2, 3)
202 uh_chunk_printf(struct client *cl, const char *format, ...);
203
204 void uh_chunk_eof(struct client *cl);
205 void uh_request_done(struct client *cl);
206
207 void uh_http_header(struct client *cl, int code, const char *summary);
208 void __printf(4, 5)
209 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
210
211 void uh_handle_request(struct client *cl);
212
213 void uh_auth_add(const char *path, const char *user, const char *pass);
214 bool uh_auth_check(struct client *cl, struct path_info *pi);
215
216 void uh_close_listen_fds(void);
217 void uh_close_fds(void);
218
219 void uh_interpreter_add(const char *ext, const char *path);
220 void uh_dispatch_add(struct dispatch_handler *d);
221
222 void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid);
223 void uh_relay_close(struct relay *r, int ret);
224 void uh_relay_free(struct relay *r);
225
226 struct env_var *uh_get_process_vars(struct client *cl, struct path_info *pi);
227 bool uh_create_process(struct client *cl, struct path_info *pi,
228 void (*cb)(struct client *cl, struct path_info *pi, int fd));
229
230 #endif