code cleanup
[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 struct config {
39         const char *docroot;
40         const char *realm;
41         const char *file;
42         const char *error_handler;
43         const char *cgi_prefix;
44         const char *cgi_path;
45         int no_symlinks;
46         int no_dirlists;
47         int network_timeout;
48         int rfc1918_filter;
49         int tcp_keepalive;
50         int max_requests;
51         int http_keepalive;
52         int script_timeout;
53 };
54
55 struct path_info {
56         const char *root;
57         const char *phys;
58         const char *name;
59         const char *info;
60         const char *query;
61         int redirected;
62         struct stat stat;
63 };
64
65 struct auth_realm {
66         struct list_head list;
67         char *path;
68         char *user;
69         char *pass;
70 };
71
72 enum http_method {
73         UH_HTTP_MSG_GET,
74         UH_HTTP_MSG_POST,
75         UH_HTTP_MSG_HEAD,
76 };
77
78 enum http_version {
79         UH_HTTP_VER_0_9,
80         UH_HTTP_VER_1_0,
81         UH_HTTP_VER_1_1,
82 };
83
84 struct http_request {
85         enum http_method method;
86         enum http_version version;
87         int redirect_status;
88         char *url;
89         const struct auth_realm *realm;
90 };
91
92 struct http_response {
93         int statuscode;
94         char *statusmsg;
95         char *headers[UH_LIMIT_HEADERS];
96 };
97
98 enum client_state {
99         CLIENT_STATE_INIT,
100         CLIENT_STATE_HEADER,
101         CLIENT_STATE_DATA,
102         CLIENT_STATE_DONE,
103         CLIENT_STATE_CLOSE,
104 };
105
106 struct client {
107         struct list_head list;
108         int id;
109
110         struct ustream *us;
111         struct ustream_fd sfd;
112 #ifdef HAVE_TLS
113         struct ustream_ssl stream_ssl;
114 #endif
115         struct uloop_timeout timeout;
116
117         enum client_state state;
118
119         struct http_request request;
120         struct http_response response;
121         struct sockaddr_in6 servaddr;
122         struct sockaddr_in6 peeraddr;
123
124         struct blob_buf hdr;
125
126         struct {
127                 void (*write_cb)(struct client *cl);
128                 void (*close_fds)(struct client *cl);
129                 void (*free)(struct client *cl);
130                 union {
131                         struct {
132                                 struct blob_attr **hdr;
133                                 int fd;
134                         } file;
135                 };
136         } dispatch;
137 };
138
139 extern char uh_buf[4096];
140 extern int n_clients;
141 extern struct config conf;
142 extern const char * const http_versions[];
143 extern const char * const http_methods[];
144
145 void uh_index_add(const char *filename);
146
147 void uh_accept_client(int fd);
148
149 void uh_unblock_listeners(void);
150 void uh_setup_listeners(void);
151 int uh_socket_bind(const char *host, const char *port, bool tls);
152
153 bool uh_use_chunked(struct client *cl);
154 void uh_chunk_write(struct client *cl, const void *data, int len);
155 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
156
157 void __printf(2, 3)
158 uh_chunk_printf(struct client *cl, const char *format, ...);
159
160 void uh_chunk_eof(struct client *cl);
161 void uh_request_done(struct client *cl);
162
163 void uh_http_header(struct client *cl, int code, const char *summary);
164 void __printf(4, 5)
165 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
166
167 void uh_handle_file_request(struct client *cl);
168
169 void uh_auth_add(const char *path, const char *user, const char *pass);
170
171 void uh_close_listen_fds(void);
172 void uh_close_fds(void);
173
174 #endif