-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshttpd_uri.c
64 lines (53 loc) · 1020 Bytes
/
shttpd_uri.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "shttpd.h"
static void
remove_double_dots(char *s)
{
char *p = s;
while (*s != '\0')
{
*p++ = *s++;
if (s[-1] == '/' || s[-1] == '\\')
{
while (*s == '.' || *s == '/' || *s == '\\')
{
s++;
}
}
}
*p = '\0';
}
static int uri_decode(char *src, int src_len, char *dst, int dst_len)
{
int i, j, a, b;
#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W')
for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++)
{
switch (src[i])
{
case '%':
if (isxdigit(((unsigned char *) src)[i + 1]) &&
isxdigit(((unsigned char *) src)[i + 2]))
{
a = tolower(((unsigned char *)src)[i + 1]);
b = tolower(((unsigned char *)src)[i + 2]);
dst[j] = (HEXTOI(a) << 4) | HEXTOI(b);
i += 2;
}
else
{
dst[j] = '%';
}
break;
default:
dst[j] = src[i];
break;
}
}
dst[j] = '\0'; /* Null-terminate the destination */
return (j);
}
void uri_parse(char *src, int len)
{
uri_decode(src, len -1, src, len);
remove_double_dots(src);
}