limit default max_requests to 3
[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
32 #include "utils.h"
33
34 #define UH_LIMIT_CLIENTS        64
35 #define UH_LIMIT_HEADERS        64
36 #define UH_LIMIT_MSGHEAD        4096
37
38 struct config {
39         char docroot[PATH_MAX];
40         char *realm;
41         char *file;
42         char *error_handler;
43         int no_symlinks;
44         int no_dirlists;
45         int network_timeout;
46         int rfc1918_filter;
47         int tcp_keepalive;
48         int max_requests;
49         int http_keepalive;
50 };
51
52 enum http_method {
53         UH_HTTP_MSG_GET,
54         UH_HTTP_MSG_POST,
55         UH_HTTP_MSG_HEAD,
56 };
57
58 enum http_version {
59         UH_HTTP_VER_0_9,
60         UH_HTTP_VER_1_0,
61         UH_HTTP_VER_1_1,
62 };
63
64 struct http_request {
65         enum http_method method;
66         enum http_version version;
67         int redirect_status;
68         char *url;
69         struct auth_realm *realm;
70 };
71
72 struct http_response {
73         int statuscode;
74         char *statusmsg;
75         char *headers[UH_LIMIT_HEADERS];
76 };
77
78 enum client_state {
79         CLIENT_STATE_INIT,
80         CLIENT_STATE_HEADER,
81         CLIENT_STATE_DATA,
82         CLIENT_STATE_DONE,
83         CLIENT_STATE_CLOSE,
84 };
85
86 struct client {
87         struct list_head list;
88         int id;
89
90         struct ustream *us;
91         struct ustream_fd sfd;
92 #ifdef HAVE_TLS
93         struct ustream_ssl stream_ssl;
94 #endif
95         struct uloop_fd rpipe;
96         struct uloop_fd wpipe;
97         struct uloop_process proc;
98         struct uloop_timeout timeout;
99         bool (*cb)(struct client *);
100         void *priv;
101
102         enum client_state state;
103
104         struct http_request request;
105         struct http_response response;
106         struct sockaddr_in6 servaddr;
107         struct sockaddr_in6 peeraddr;
108
109         struct blob_buf hdr;
110
111         void (*dispatch_write_cb)(struct client *cl);
112         void (*dispatch_free)(struct client *cl);
113
114         union {
115                 struct {
116                         struct blob_attr **hdr;
117                         int fd;
118                 } file;
119         } data;
120 };
121
122 extern int n_clients;
123 extern struct config conf;
124
125 void uh_index_add(const char *filename);
126
127 void uh_accept_client(int fd);
128
129 void uh_unblock_listeners(void);
130 void uh_setup_listeners(void);
131 int uh_socket_bind(const char *host, const char *port, bool tls);
132
133 bool uh_use_chunked(struct client *cl);
134 void uh_chunk_write(struct client *cl, const void *data, int len);
135 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg);
136
137 void __printf(2, 3)
138 uh_chunk_printf(struct client *cl, const char *format, ...);
139
140 void uh_chunk_eof(struct client *cl);
141 void uh_request_done(struct client *cl);
142
143 void uh_http_header(struct client *cl, int code, const char *summary);
144 void __printf(4, 5)
145 uh_client_error(struct client *cl, int code, const char *summary, const char *fmt, ...);
146
147 void uh_handle_file_request(struct client *cl);
148
149 #endif