02d270508d80f21b31919dcf7ef4a2d0d3c88469
[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 #define UH_LIMIT_MSGHEAD        4096
38
39 struct config {
40         char docroot[PATH_MAX];
41         char *realm;
42         char *file;
43         char *error_handler;
44         char *cgi_prefix;
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 auth_realm {
56         struct list_head list;
57         char *path;
58         char *user;
59         char *pass;
60 };
61
62 enum http_method {
63         UH_HTTP_MSG_GET,
64         UH_HTTP_MSG_POST,
65         UH_HTTP_MSG_HEAD,
66 };
67
68 enum http_version {
69         UH_HTTP_VER_0_9,
70         UH_HTTP_VER_1_0,
71         UH_HTTP_VER_1_1,
72 };
73
74 struct http_request {
75         enum http_method method;
76         enum http_version version;
77         int redirect_status;
78         char *url;
79         struct auth_realm *realm;
80 };
81
82 struct http_response {
83         int statuscode;
84         char *statusmsg;
85         char *headers[UH_LIMIT_HEADERS];
86 };
87
88 enum client_state {
89         CLIENT_STATE_INIT,
90         CLIENT_STATE_HEADER,
91         CLIENT_STATE_DATA,
92         CLIENT_STATE_DONE,
93         CLIENT_STATE_CLOSE,
94 };
95
96 struct client {
97         struct list_head list;
98         int id;
99
100         struct ustream *us;
101         struct ustream_fd sfd;
102 #ifdef HAVE_TLS
103         struct ustream_ssl stream_ssl;
104 #endif
105         struct uloop_fd rpipe;
106         struct uloop_fd wpipe;
107         struct uloop_process proc;
108         struct uloop_timeout timeout;
109         bool (*cb)(struct client *);
110         void *priv;
111
112         enum client_state state;
113
114         struct http_request request;
115         struct http_response response;
116         struct sockaddr_in6 servaddr;
117         struct sockaddr_in6 peeraddr;
118
119         struct blob_buf hdr;
120
121         void (*dispatch_write_cb)(struct client *cl);
122         void (*dispatch_close_fds)(struct client *cl);
123         void (*dispatch_free)(struct client *cl);
124
125         union {
126                 struct {
127                         struct blob_attr **hdr;
128                         int fd;
129                 } file;
130         } data;
131 };
132
133 extern int n_clients;
134 extern struct config conf;
135
136 void uh_index_add(const char *filename);
137
138 void uh_accept_client(int fd);
139
140 void uh_unblock_listeners(void);
141 void uh_setup_listeners(void);
142 int uh_socket_bind(const char *host, const char *port, bool tls);
143
144 bool uh_use_chunked(struct client *cl);
145 void uh_chunk_write(struct client *cl, const void *data, int len);
146 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
147
148 void __printf(2, 3)
149 uh_chunk_printf(struct client *cl, const char *format, ...);
150
151 void uh_chunk_eof(struct client *cl);
152 void uh_request_done(struct client *cl);
153
154 void uh_http_header(struct client *cl, int code, const char *summary);
155 void __printf(4, 5)
156 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
157
158 void uh_handle_file_request(struct client *cl);
159
160 void uh_auth_add(const char *path, const char *user, const char *pass);
161
162 void uh_close_listen_fds(void);
163 void uh_close_fds(void);
164
165 #endif