fe86b01d402f247ab54d09d7bcb9689bd892593a
[openwrt/openwrt.git] / package / uhttpd / src / uhttpd.h
1 /*
2 * uhttpd - Tiny single-threaded httpd - Main header
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #ifndef _UHTTPD_
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/select.h>
30 #include <sys/wait.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
34 #include <linux/limits.h>
35 #include <netdb.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <dlfcn.h>
39
40 #include <libubox/list.h>
41 #include <libubox/uloop.h>
42
43
44 #ifdef HAVE_LUA
45 #include <lua.h>
46 #endif
47
48 #ifdef HAVE_TLS
49 #include <openssl/ssl.h>
50 #endif
51
52 /* uClibc... */
53 #ifndef SOL_TCP
54 #define SOL_TCP 6
55 #endif
56
57 #ifdef DEBUG
58 #define D(...) fprintf(stderr, __VA_ARGS__)
59 #else
60 #define D(...)
61 #endif
62
63
64 #define UH_LIMIT_MSGHEAD 4096
65 #define UH_LIMIT_HEADERS 64
66
67 #define UH_LIMIT_CLIENTS 64
68
69 #define UH_HTTP_MSG_GET 0
70 #define UH_HTTP_MSG_HEAD 1
71 #define UH_HTTP_MSG_POST 2
72
73 #define UH_SOCK_CLIENT 0
74 #define UH_SOCK_SERVER 1
75
76 struct listener;
77 struct client;
78 struct interpreter;
79 struct http_request;
80 struct uh_ubus_state;
81
82 struct config {
83 char docroot[PATH_MAX];
84 char *realm;
85 char *file;
86 char *index_file;
87 char *error_handler;
88 int no_symlinks;
89 int no_dirlists;
90 int network_timeout;
91 int rfc1918_filter;
92 int tcp_keepalive;
93 int max_requests;
94 #ifdef HAVE_CGI
95 char *cgi_prefix;
96 #endif
97 #ifdef HAVE_LUA
98 char *lua_prefix;
99 char *lua_handler;
100 lua_State *lua_state;
101 lua_State * (*lua_init) (const struct config *conf);
102 void (*lua_close) (lua_State *L);
103 bool (*lua_request) (struct client *cl, lua_State *L);
104 #endif
105 #ifdef HAVE_UBUS
106 char *ubus_prefix;
107 char *ubus_socket;
108 void *ubus_state;
109 struct uh_ubus_state * (*ubus_init) (const struct config *conf);
110 void (*ubus_close) (struct uh_ubus_state *state);
111 bool (*ubus_request) (struct client *cl, struct uh_ubus_state *state);
112 #endif
113 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
114 int script_timeout;
115 #endif
116 #ifdef HAVE_TLS
117 char *cert;
118 char *key;
119 SSL_CTX *tls;
120 SSL_CTX * (*tls_init) (void);
121 int (*tls_cert) (SSL_CTX *c, const char *file);
122 int (*tls_key) (SSL_CTX *c, const char *file);
123 void (*tls_free) (struct listener *l);
124 int (*tls_accept) (struct client *c);
125 void (*tls_close) (struct client *c);
126 int (*tls_recv) (struct client *c, char *buf, int len);
127 int (*tls_send) (struct client *c, const char *buf, int len);
128 #endif
129 };
130
131 struct http_request {
132 int method;
133 float version;
134 int redirect_status;
135 char *url;
136 char *headers[UH_LIMIT_HEADERS];
137 struct auth_realm *realm;
138 };
139
140 struct http_response {
141 int statuscode;
142 char *statusmsg;
143 char *headers[UH_LIMIT_HEADERS];
144 };
145
146 struct listener {
147 struct uloop_fd fd;
148 int socket;
149 int n_clients;
150 struct sockaddr_in6 addr;
151 struct config *conf;
152 #ifdef HAVE_TLS
153 SSL_CTX *tls;
154 #endif
155 struct listener *next;
156 };
157
158 struct client {
159 #ifdef HAVE_TLS
160 SSL *tls;
161 #endif
162 struct uloop_fd fd;
163 struct uloop_fd rpipe;
164 struct uloop_fd wpipe;
165 struct uloop_process proc;
166 struct uloop_timeout timeout;
167 bool (*cb)(struct client *);
168 void *priv;
169 bool dispatched;
170 bool dead;
171 struct {
172 char buf[UH_LIMIT_MSGHEAD];
173 char *ptr;
174 int len;
175 } httpbuf;
176 struct listener *server;
177 struct http_request request;
178 struct http_response response;
179 struct sockaddr_in6 servaddr;
180 struct sockaddr_in6 peeraddr;
181 struct client *next;
182 };
183
184 struct client_light {
185 #ifdef HAVE_TLS
186 SSL *tls;
187 #endif
188 struct uloop_fd fd;
189 };
190
191 struct auth_realm {
192 char path[PATH_MAX];
193 char user[32];
194 char pass[128];
195 struct auth_realm *next;
196 };
197
198 #ifdef HAVE_CGI
199 struct interpreter {
200 char path[PATH_MAX];
201 char extn[32];
202 struct interpreter *next;
203 };
204 #endif
205
206 #endif