2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 bool uh_use_chunked(struct client *cl)
25 if (cl->request.version != UH_HTTP_VER_1_1)
28 if (cl->request.method == UH_HTTP_MSG_HEAD)
34 void uh_chunk_write(struct client *cl, const void *data, int len)
36 bool chunked = uh_use_chunked(cl);
38 uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
40 ustream_printf(cl->us, "%X\r\n", len);
41 ustream_write(cl->us, data, len, true);
43 ustream_printf(cl->us, "\r\n", len);
46 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg)
52 uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
53 if (!uh_use_chunked(cl)) {
54 ustream_vprintf(cl->us, format, arg);
59 len = vsnprintf(buf, sizeof(buf), format, arg2);
62 ustream_printf(cl->us, "%X\r\n", len);
63 if (len < sizeof(buf))
64 ustream_write(cl->us, buf, len, true);
66 ustream_vprintf(cl->us, format, arg);
67 ustream_printf(cl->us, "\r\n", len);
70 void uh_chunk_printf(struct client *cl, const char *format, ...)
74 va_start(arg, format);
75 uh_chunk_vprintf(cl, format, arg);
79 void uh_chunk_eof(struct client *cl)
81 if (!uh_use_chunked(cl))
84 ustream_printf(cl->us, "0\r\n\r\n");
87 /* blen is the size of buf; slen is the length of src. The input-string need
88 ** not be, and the output string will not be, null-terminated. Returns the
89 ** length of the decoded string, -1 on buffer overflow, -2 on malformed string. */
90 int uh_urldecode(char *buf, int blen, const char *src, int slen)
96 (((x) <= '9') ? ((x) - '0') : \
97 (((x) <= 'F') ? ((x) - 'A' + 10) : \
100 for (i = 0; (i < slen) && (len < blen); i++)
107 if (i + 2 >= slen || !isxdigit(src[i + 1]) || !isxdigit(src[i + 2]))
110 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
115 return (i == slen) ? len : -1;
118 /* blen is the size of buf; slen is the length of src. The input-string need
119 ** not be, and the output string will not be, null-terminated. Returns the
120 ** length of the encoded string, or -1 on error (buffer overflow) */
121 int uh_urlencode(char *buf, int blen, const char *src, int slen)
125 static const char hex[] = "0123456789abcdef";
127 for (i = 0; (i < slen) && (len < blen); i++)
129 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
130 (src[i] == '.') || (src[i] == '~') )
134 else if ((len+3) <= blen)
137 buf[len++] = hex[(src[i] >> 4) & 15];
138 buf[len++] = hex[ src[i] & 15];
147 return (i == slen) ? len : -1;
150 int uh_b64decode(char *buf, int blen, const void *src, int slen)
152 const unsigned char *str = src;
153 unsigned int cout = 0;
154 unsigned int cin = 0;
158 for (i = 0; (i <= slen) && (str[i] != 0); i++)
162 if ((cin >= '0') && (cin <= '9'))
163 cin = cin - '0' + 52;
164 else if ((cin >= 'A') && (cin <= 'Z'))
166 else if ((cin >= 'a') && (cin <= 'z'))
167 cin = cin - 'a' + 26;
177 cout = (cout << 6) | cin;
182 if ((len + 3) >= blen)
185 buf[len++] = (char)(cout >> 16);
186 buf[len++] = (char)(cout >> 8);
187 buf[len++] = (char)(cout);
194 bool uh_path_match(const char *prefix, const char *url)
196 int len = strlen(prefix);
198 if (strncmp(url, prefix, len) != 0)
201 return url[len] == '/' || url[len] == 0;
204 char *uh_split_header(char *str)
208 val = strchr(str, ':');
215 while (isspace(*val))
221 bool uh_addr_rfc1918(struct uh_addr *addr)
225 if (addr->family != AF_INET)
228 a = htonl(addr->in.s_addr);
229 return ((a >= 0x0A000000) && (a <= 0x0AFFFFFF)) ||
230 ((a >= 0xAC100000) && (a <= 0xAC1FFFFF)) ||
231 ((a >= 0xC0A80000) && (a <= 0xC0A8FFFF));