1e487f8ab526eeb05431ece86e079e34dad23f42
[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         int no_symlinks;
45         int no_dirlists;
46         int network_timeout;
47         int rfc1918_filter;
48         int tcp_keepalive;
49         int max_requests;
50         int http_keepalive;
51         int script_timeout;
52 };
53
54 struct auth_realm {
55         struct list_head list;
56         char *path;
57         char *user;
58         char *pass;
59 };
60
61 enum http_method {
62         UH_HTTP_MSG_GET,
63         UH_HTTP_MSG_POST,
64         UH_HTTP_MSG_HEAD,
65 };
66
67 enum http_version {
68         UH_HTTP_VER_0_9,
69         UH_HTTP_VER_1_0,
70         UH_HTTP_VER_1_1,
71 };
72
73 struct http_request {
74         enum http_method method;
75         enum http_version version;
76         int redirect_status;
77         char *url;
78         struct auth_realm *realm;
79 };
80
81 struct http_response {
82         int statuscode;
83         char *statusmsg;
84         char *headers[UH_LIMIT_HEADERS];
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 client {
96         struct list_head list;
97         int id;
98
99         struct ustream *us;
100         struct ustream_fd sfd;
101 #ifdef HAVE_TLS
102         struct ustream_ssl stream_ssl;
103 #endif
104         struct uloop_fd rpipe;
105         struct uloop_fd wpipe;
106         struct uloop_process proc;
107         struct uloop_timeout timeout;
108         bool (*cb)(struct client *);
109         void *priv;
110
111         enum client_state state;
112
113         struct http_request request;
114         struct http_response response;
115         struct sockaddr_in6 servaddr;
116         struct sockaddr_in6 peeraddr;
117
118         struct blob_buf hdr;
119
120         void (*dispatch_write_cb)(struct client *cl);
121         void (*dispatch_free)(struct client *cl);
122
123         union {
124                 struct {
125                         struct blob_attr **hdr;
126                         int fd;
127                 } file;
128         } data;
129 };
130
131 extern int n_clients;
132 extern struct config conf;
133
134 void uh_index_add(const char *filename);
135
136 void uh_accept_client(int fd);
137
138 void uh_unblock_listeners(void);
139 void uh_setup_listeners(void);
140 int uh_socket_bind(const char *host, const char *port, bool tls);
141
142 bool uh_use_chunked(struct client *cl);
143 void uh_chunk_write(struct client *cl, const void *data, int len);
144 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
145
146 void __printf(2, 3)
147 uh_chunk_printf(struct client *cl, const char *format, ...);
148
149 void uh_chunk_eof(struct client *cl);
150 void uh_request_done(struct client *cl);
151
152 void uh_http_header(struct client *cl, int code, const char *summary);
153 void __printf(4, 5)
154 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
155
156 void uh_handle_file_request(struct client *cl);
157
158 void uh_auth_add(const char *path, const char *user, const char *pass);
159
160 #endif