file: invoke error handler in 403 case as well
[project/uhttpd.git] / utils.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
6  *
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.
10  *
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.
18  */
19
20 #include <ctype.h>
21 #include "uhttpd.h"
22
23 bool uh_use_chunked(struct client *cl)
24 {
25         if (cl->request.version != UH_HTTP_VER_1_1)
26                 return false;
27
28         if (cl->request.method == UH_HTTP_MSG_HEAD || cl->request.method == UH_HTTP_MSG_OPTIONS)
29                 return false;
30
31         return true;
32 }
33
34 void uh_chunk_write(struct client *cl, const void *data, int len)
35 {
36         bool chunked = uh_use_chunked(cl);
37
38         if (cl->state == CLIENT_STATE_CLEANUP)
39                 return;
40
41         uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
42         if (chunked)
43                 ustream_printf(cl->us, "%X\r\n", len);
44         ustream_write(cl->us, data, len, true);
45         if (chunked)
46                 ustream_printf(cl->us, "\r\n", len);
47 }
48
49 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg)
50 {
51         char buf[256];
52         va_list arg2;
53         int len;
54
55         if (cl->state == CLIENT_STATE_CLEANUP)
56                 return;
57
58         uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
59         if (!uh_use_chunked(cl)) {
60                 ustream_vprintf(cl->us, format, arg);
61                 return;
62         }
63
64         va_copy(arg2, arg);
65         len = vsnprintf(buf, sizeof(buf), format, arg2);
66         va_end(arg2);
67
68         ustream_printf(cl->us, "%X\r\n", len);
69         if (len < sizeof(buf))
70                 ustream_write(cl->us, buf, len, true);
71         else
72                 ustream_vprintf(cl->us, format, arg);
73         ustream_printf(cl->us, "\r\n", len);
74 }
75
76 void uh_chunk_printf(struct client *cl, const char *format, ...)
77 {
78         va_list arg;
79
80         va_start(arg, format);
81         uh_chunk_vprintf(cl, format, arg);
82         va_end(arg);
83 }
84
85 void uh_chunk_eof(struct client *cl)
86 {
87         if (!uh_use_chunked(cl))
88                 return;
89
90         if (cl->state == CLIENT_STATE_CLEANUP)
91                 return;
92
93         ustream_printf(cl->us, "0\r\n\r\n");
94 }
95
96 /* blen is the size of buf; slen is the length of src.  The input-string need
97 ** not be, and the output string will not be, null-terminated.  Returns the
98 ** length of the decoded string, -1 on buffer overflow, -2 on malformed string. */
99 int uh_urldecode(char *buf, int blen, const char *src, int slen)
100 {
101         int i;
102         int len = 0;
103
104 #define hex(x) \
105         (((x) <= '9') ? ((x) - '0') : \
106                 (((x) <= 'F') ? ((x) - 'A' + 10) : \
107                         ((x) - 'a' + 10)))
108
109         for (i = 0; (i < slen) && (len < blen); i++)
110         {
111                 if (src[i] != '%') {
112                         buf[len++] = src[i];
113                         continue;
114                 }
115
116                 if (i + 2 >= slen || !isxdigit(src[i + 1]) || !isxdigit(src[i + 2]))
117                         return -2;
118
119                 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
120                 i += 2;
121         }
122         buf[len] = 0;
123
124         return (i == slen) ? len : -1;
125 }
126
127 /* blen is the size of buf; slen is the length of src.  The input-string need
128 ** not be, and the output string will not be, null-terminated.  Returns the
129 ** length of the encoded string, or -1 on error (buffer overflow) */
130 int uh_urlencode(char *buf, int blen, const char *src, int slen)
131 {
132         int i;
133         int len = 0;
134         static const char hex[] = "0123456789abcdef";
135
136         for (i = 0; (i < slen) && (len < blen); i++)
137         {
138                 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
139                     (src[i] == '.') || (src[i] == '~') )
140                 {
141                         buf[len++] = src[i];
142                 }
143                 else if ((len+3) <= blen)
144                 {
145                         buf[len++] = '%';
146                         buf[len++] = hex[(src[i] >> 4) & 15];
147                         buf[len++] = hex[ src[i]       & 15];
148                 }
149                 else
150                 {
151                         len = -1;
152                         break;
153                 }
154         }
155
156         return (i == slen) ? len : -1;
157 }
158
159 int uh_b64decode(char *buf, int blen, const void *src, int slen)
160 {
161         const unsigned char *str = src;
162         unsigned int cout = 0;
163         unsigned int cin  = 0;
164         int len = 0;
165         int i = 0;
166
167         for (i = 0; (i <= slen) && (str[i] != 0); i++)
168         {
169                 cin = str[i];
170
171                 if ((cin >= '0') && (cin <= '9'))
172                         cin = cin - '0' + 52;
173                 else if ((cin >= 'A') && (cin <= 'Z'))
174                         cin = cin - 'A';
175                 else if ((cin >= 'a') && (cin <= 'z'))
176                         cin = cin - 'a' + 26;
177                 else if (cin == '+')
178                         cin = 62;
179                 else if (cin == '/')
180                         cin = 63;
181                 else if (cin == '=')
182                         cin = 0;
183                 else
184                         continue;
185
186                 cout = (cout << 6) | cin;
187
188                 if ((i % 4) != 3)
189                         continue;
190
191                 if ((len + 3) >= blen)
192                         break;
193
194                 buf[len++] = (char)(cout >> 16);
195                 buf[len++] = (char)(cout >> 8);
196                 buf[len++] = (char)(cout);
197         }
198
199         buf[len++] = 0;
200         return len;
201 }
202
203 bool uh_path_match(const char *prefix, const char *url)
204 {
205         int len = strlen(prefix);
206
207         if (strncmp(url, prefix, len) != 0)
208                 return false;
209
210         return url[len] == '/' || url[len] == 0;
211 }
212
213 char *uh_split_header(char *str)
214 {
215         char *val;
216
217         val = strchr(str, ':');
218         if (!val)
219                 return NULL;
220
221         *val = 0;
222         val++;
223
224         while (isspace(*val))
225                 val++;
226
227         return val;
228 }
229
230 bool uh_addr_rfc1918(struct uh_addr *addr)
231 {
232         uint32_t a;
233
234         if (addr->family != AF_INET)
235                 return false;
236
237         a = htonl(addr->in.s_addr);
238         return ((a >= 0x0A000000) && (a <= 0x0AFFFFFF)) ||
239                ((a >= 0xAC100000) && (a <= 0xAC1FFFFF)) ||
240                ((a >= 0xC0A80000) && (a <= 0xC0A8FFFF));
241
242         return 0;
243 }