eac737b9918a6505791bf44eb850f052e1b7e61e
[project/luci.git] / contrib / package / uhttpd / src / uhttpd-utils.h
1 /*
2  * uhttpd - Tiny non-forking httpd - Utility 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_UTILS_
20
21 #include <stdarg.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24
25 #define min(x, y) (((x) < (y)) ? (x) : (y))
26 #define max(x, y) (((x) > (y)) ? (x) : (y))
27
28 #define array_size(x) \
29         (sizeof(x) / sizeof(x[0]))
30
31 #define foreach_header(i, h) \
32         for( i = 0; (i + 1) < (sizeof(h) / sizeof(h[0])) && h[i]; i += 2 )
33
34 struct uh_path_info {
35         char *root;
36         char *wdir;
37         char *phys;
38         char *name;
39         char *info;
40         char *query;
41         struct stat stat;
42 };
43
44
45 const char * sa_straddr(void *sa);
46 const char * sa_strport(void *sa);
47 int sa_port(void *sa);
48
49 char *strfind(char *haystack, int hslen, const char *needle, int ndlen);
50
51 int uh_tcp_send(struct client *cl, const char *buf, int len);
52 int uh_tcp_peek(struct client *cl, char *buf, int len);
53 int uh_tcp_recv(struct client *cl, char *buf, int len);
54
55 int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...);
56
57 #define uh_http_response(cl, code, message) \
58         uh_http_sendhf(cl, code, message, message)
59
60 int uh_http_sendc(struct client *cl, const char *data, int len);
61
62 int uh_http_sendf(
63         struct client *cl, struct http_request *req,
64         const char *fmt, ...
65 );
66
67 int uh_http_send(
68         struct client *cl, struct http_request *req,
69         const char *buf, int len
70 );
71
72
73 int uh_urldecode(char *buf, int blen, const char *src, int slen);
74 int uh_urlencode(char *buf, int blen, const char *src, int slen);
75 int uh_path_normalize(char *buf, int blen, const char *src, int slen);
76
77 struct uh_path_info * uh_path_lookup(struct client *cl, const char *url);
78
79 struct listener * uh_listener_add(int sock, struct config *conf);
80 struct listener * uh_listener_lookup(int sock);
81
82 struct client * uh_client_add(int sock, struct listener *serv);
83 struct client * uh_client_lookup(int sock);
84 void uh_client_remove(int sock);
85
86 #endif