/* * uhttpd - Tiny single-threaded httpd * * Copyright (C) 2010-2013 Jo-Philipp Wich * Copyright (C) 2013 Felix Fietkau * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _UHTTPD_UTILS_ #include #include #include #include #include #include #include #include #include #include #include struct uh_addr { uint8_t family; uint16_t port; union { struct in_addr in; struct in6_addr in6; }; }; #define min(x, y) (((x) < (y)) ? (x) : (y)) #define max(x, y) (((x) > (y)) ? (x) : (y)) #define array_size(x) \ (sizeof(x) / sizeof(x[0])) #define fd_cloexec(fd) \ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC) #ifdef __APPLE__ static inline void clearenv(void) { extern char **environ; *environ = NULL; } time_t timegm (struct tm *tm); #endif #ifdef __GNUC__ #define __printf(a, b) __attribute__((format(printf, a, b))) #else #define __printf(a, b) #endif enum { /* chartypes flags */ CT_ALPHA = (1 << 0), CT_CTL = (1 << 1), CT_DIGIT = (1 << 2), CT_HEXDIG = (1 << 3), CT_VCHAR = (1 << 4), CT_WSP = (1 << 5), CT_DELIM = (1 << 6), CT_VDELIM = CT_DELIM | CT_VCHAR, CT_VALPHA = CT_ALPHA | CT_VCHAR, CT_XALPHA = CT_ALPHA | CT_HEXDIG | CT_VCHAR, CT_XDIGIT = CT_DIGIT | CT_HEXDIG | CT_VCHAR, }; extern uint8_t chartypes[256]; /* RFC 9110 character class helpers */ #define uh_is_tchar(c) ((chartypes[(uint8_t)(c)] & (CT_VCHAR | CT_DELIM)) == CT_VCHAR) #define uh_is_wsp(c) (chartypes[(uint8_t)(c)] & CT_WSP) #define uh_is_ctl(c) (chartypes[(uint8_t)(c)] & CT_CTL) #define uh_is_digit(c) (chartypes[(uint8_t)(c)] & CT_DIGIT) #define uh_is_hexdig(c) (chartypes[(uint8_t)(c)] & CT_HEXDIG) int uh_urldecode(char *buf, int blen, const char *src, int slen); int uh_urlencode(char *buf, int blen, const char *src, int slen); int uh_b64decode(char *buf, int blen, const void *src, int slen); bool uh_path_match(const char *prefix, const char *url); char *uh_split_header(char *str, size_t len); bool uh_addr_rfc1918(struct uh_addr *addr); char *uh_htmlescape(const char *src); #endif