add lua plugin support
[project/uhttpd.git] / 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 <sys/stat.h>
22
23 #include <stdarg.h>
24 #include <fcntl.h>
25 #include <pwd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <alloca.h>
31 #include <time.h>
32 #include <unistd.h>
33
34 struct uh_addr {
35         uint8_t family;
36         uint16_t port;
37         union {
38                 struct in_addr in;
39                 struct in6_addr in6;
40         };
41 };
42
43 #define min(x, y) (((x) < (y)) ? (x) : (y))
44 #define max(x, y) (((x) > (y)) ? (x) : (y))
45
46 #define array_size(x) \
47         (sizeof(x) / sizeof(x[0]))
48
49 #define fd_cloexec(fd) \
50         fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC)
51
52 #ifdef __APPLE__
53 static inline void clearenv(void)
54 {
55         extern char **environ;
56         *environ = NULL;
57 }
58
59 time_t timegm (struct tm *tm);
60
61 #endif
62
63 #ifdef __GNUC__
64 #define __printf(a, b) __attribute__((format(printf, a, b)))
65 #else
66 #define __printf(a, b)
67 #endif
68
69 int uh_urldecode(char *buf, int blen, const char *src, int slen);
70 int uh_urlencode(char *buf, int blen, const char *src, int slen);
71 int uh_b64decode(char *buf, int blen, const void *src, int slen);
72 bool uh_path_match(const char *prefix, const char *url);
73 char *uh_split_header(char *str);
74 bool uh_addr_rfc1918(struct uh_addr *addr);
75
76 #endif