scripts/config: fix menuconfig segfault in text inputs when format patterns are enter...
[openwrt.git] / package / network / services / uhttpd / src / uhttpd-utils.h
1 /*
2  * uhttpd - Tiny single-threaded httpd - Utility header
3  *
4  *   Copyright (C) 2010-2012 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 <pwd.h>
24 #include <sys/stat.h>
25
26 #include <libubox/uloop.h>
27
28
29 #ifdef HAVE_SHADOW
30 #include <shadow.h>
31 #endif
32
33 #define min(x, y) (((x) < (y)) ? (x) : (y))
34 #define max(x, y) (((x) > (y)) ? (x) : (y))
35
36 #define array_size(x) \
37         (sizeof(x) / sizeof(x[0]))
38
39 #define foreach_header(i, h) \
40         for( i = 0; (i + 1) < (sizeof(h) / sizeof(h[0])) && h[i]; i += 2 )
41
42 #define fd_cloexec(fd) \
43         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC)
44
45 #define fd_nonblock(fd) \
46         fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK)
47
48 #define ensure_out(x) \
49         do { if((x) < 0) goto out; } while(0)
50
51 #define ensure_ret(x) \
52         do { if((x) < 0) return -1; } while(0)
53
54
55 struct path_info {
56         char *root;
57         char *phys;
58         char *name;
59         char *info;
60         char *query;
61         int redirected;
62         struct stat stat;
63 };
64
65
66 const char * sa_straddr(void *sa);
67 const char * sa_strport(void *sa);
68 int sa_port(void *sa);
69 int sa_rfc1918(void *sa);
70
71 char *strfind(char *haystack, int hslen, const char *needle, int ndlen);
72
73 bool uh_socket_wait(int fd, int sec, bool write);
74
75 int uh_raw_send(int fd, const char *buf, int len, int seconds);
76 int uh_raw_recv(int fd, char *buf, int len, int seconds);
77 int uh_tcp_send(struct client *cl, const char *buf, int len);
78 int uh_tcp_send_lowlevel(struct client *cl, const char *buf, int len);
79 int uh_tcp_recv(struct client *cl, char *buf, int len);
80 int uh_tcp_recv_lowlevel(struct client *cl, char *buf, int len);
81
82 int uh_http_sendhf(struct client *cl, int code, const char *summary,
83                                    const char *fmt, ...);
84
85 #define uh_http_response(cl, code, message) \
86         uh_http_sendhf(cl, code, message, message)
87
88 int uh_http_sendc(struct client *cl, const char *data, int len);
89
90 int uh_http_sendf(
91         struct client *cl, struct http_request *req,
92         const char *fmt, ...
93 );
94
95 int uh_http_send(
96         struct client *cl, struct http_request *req,
97         const char *buf, int len
98 );
99
100
101 int uh_urldecode(char *buf, int blen, const char *src, int slen);
102 int uh_urlencode(char *buf, int blen, const char *src, int slen);
103 int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen);
104
105
106 struct auth_realm * uh_auth_add(char *path, char *user, char *pass);
107
108 int uh_auth_check(
109         struct client *cl, struct http_request *req, struct path_info *pi
110 );
111
112
113 struct path_info * uh_path_lookup(struct client *cl, const char *url);
114
115 struct listener * uh_listener_add(int sock, struct config *conf);
116 struct listener * uh_listener_lookup(int sock);
117
118 struct client * uh_client_add(int sock, struct listener *serv,
119                               struct sockaddr_in6 *peer);
120
121 struct client * uh_client_lookup(int sock);
122
123 #define uh_client_error(cl, code, status, ...) do { \
124         uh_http_sendhf(cl, code, status, __VA_ARGS__);  \
125         uh_client_shutdown(cl);                         \
126 } while(0)
127
128 void uh_client_shutdown(struct client *cl);
129 void uh_client_remove(struct client *cl);
130
131 void uh_ufd_add(struct uloop_fd *u, uloop_fd_handler h, unsigned int ev);
132 void uh_ufd_remove(struct uloop_fd *u);
133
134
135 #ifdef HAVE_CGI
136 struct interpreter * uh_interpreter_add(const char *extn, const char *path);
137 struct interpreter * uh_interpreter_lookup(const char *path);
138 #endif
139
140 #endif