ec0b3aa0612d1b3c86f35685b3b2a14047b8ee69
[project/uhttpd.git] / utils.c
1 /*
2  * uhttpd - Tiny single-threaded httpd
3  *
4  *   Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5  *   Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
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)
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         uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
39         if (chunked)
40                 ustream_printf(cl->us, "%X\r\n", len);
41         ustream_write(cl->us, data, len, true);
42         if (chunked)
43                 ustream_printf(cl->us, "\r\n", len);
44 }
45
46 void uh_chunk_vprintf(struct client *cl, const char *format, va_list arg)
47 {
48         char buf[256];
49         va_list arg2;
50         int len;
51
52         uloop_timeout_set(&cl->timeout, conf.network_timeout * 1000);
53         if (!uh_use_chunked(cl)) {
54                 ustream_vprintf(cl->us, format, arg);
55                 return;
56         }
57
58         va_copy(arg2, arg);
59         len = vsnprintf(buf, sizeof(buf), format, arg2);
60         va_end(arg2);
61
62         ustream_printf(cl->us, "%X\r\n", len);
63         if (len < sizeof(buf))
64                 ustream_write(cl->us, buf, len, true);
65         else
66                 ustream_vprintf(cl->us, format, arg);
67         ustream_printf(cl->us, "\r\n", len);
68 }
69
70 void uh_chunk_printf(struct client *cl, const char *format, ...)
71 {
72         va_list arg;
73
74         va_start(arg, format);
75         uh_chunk_vprintf(cl, format, arg);
76         va_end(arg);
77 }
78
79 void uh_chunk_eof(struct client *cl)
80 {
81         if (!uh_use_chunked(cl))
82                 return;
83
84         ustream_printf(cl->us, "0\r\n\r\n");
85 }
86
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)
91 {
92         int i;
93         int len = 0;
94
95 #define hex(x) \
96         (((x) <= '9') ? ((x) - '0') : \
97                 (((x) <= 'F') ? ((x) - 'A' + 10) : \
98                         ((x) - 'a' + 10)))
99
100         for (i = 0; (i < slen) && (len < blen); i++)
101         {
102                 if (src[i] == '%')
103                 {
104                         if (((i+2) < slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]))
105                         {
106                                 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
107                                 i += 2;
108                         }
109                         else
110                         {
111                                 /* Encoding error: it's hard to think of a
112                                 ** scenario in which returning an incorrect
113                                 ** 'decoding' of the malformed string is
114                                 ** preferable to signaling an error condition. */
115                                 #if 0 /* WORSE_IS_BETTER */
116                                     buf[len++] = '%';
117                                 #else
118                                     return -2;
119                                 #endif
120                         }
121                 }
122                 else
123                 {
124                         buf[len++] = src[i];
125                 }
126         }
127
128         return (i == slen) ? len : -1;
129 }
130
131 /* blen is the size of buf; slen is the length of src.  The input-string need
132 ** not be, and the output string will not be, null-terminated.  Returns the
133 ** length of the encoded string, or -1 on error (buffer overflow) */
134 int uh_urlencode(char *buf, int blen, const char *src, int slen)
135 {
136         int i;
137         int len = 0;
138         const char hex[] = "0123456789abcdef";
139
140         for (i = 0; (i < slen) && (len < blen); i++)
141         {
142                 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
143                     (src[i] == '.') || (src[i] == '~') )
144                 {
145                         buf[len++] = src[i];
146                 }
147                 else if ((len+3) <= blen)
148                 {
149                         buf[len++] = '%';
150                         buf[len++] = hex[(src[i] >> 4) & 15];
151                         buf[len++] = hex[ src[i]       & 15];
152                 }
153                 else
154                 {
155                         len = -1;
156                         break;
157                 }
158         }
159
160         return (i == slen) ? len : -1;
161 }
162
163 int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen)
164 {
165         int i = 0;
166         int len = 0;
167
168         unsigned int cin  = 0;
169         unsigned int cout = 0;
170
171
172         for (i = 0; (i <= slen) && (src[i] != 0); i++)
173         {
174                 cin = src[i];
175
176                 if ((cin >= '0') && (cin <= '9'))
177                         cin = cin - '0' + 52;
178                 else if ((cin >= 'A') && (cin <= 'Z'))
179                         cin = cin - 'A';
180                 else if ((cin >= 'a') && (cin <= 'z'))
181                         cin = cin - 'a' + 26;
182                 else if (cin == '+')
183                         cin = 62;
184                 else if (cin == '/')
185                         cin = 63;
186                 else if (cin == '=')
187                         cin = 0;
188                 else
189                         continue;
190
191                 cout = (cout << 6) | cin;
192
193                 if ((i % 4) == 3)
194                 {
195                         if ((len + 3) < blen)
196                         {
197                                 buf[len++] = (char)(cout >> 16);
198                                 buf[len++] = (char)(cout >> 8);
199                                 buf[len++] = (char)(cout);
200                         }
201                         else
202                         {
203                                 break;
204                         }
205                 }
206         }
207
208         buf[len++] = 0;
209         return len;
210 }