-
Notifications
You must be signed in to change notification settings - Fork 7
/
urlparser.h
119 lines (99 loc) · 2.66 KB
/
urlparser.h
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* queryString: the string with is to be parsed.
* WARNING! This function overwrites the content of this string. Pass this function a copy
* if you need the value preserved.
* results: place to put the pairs of param name/value.
* resultsMaxCt: maximum number of results, = sizeof(results)/sizeof(*results)
* decodeUrl: if this is true, then url escapes will be decoded as per RFC 2616
*/
void percentDecode(char *src);
char *strsep(char **from, const char *delim) {
char *s, *dp, *ret;
if ((s = *from) == NULL)
return NULL;
ret = s;
while (*s != '\0') {
/* loop until the end of s, checking against each delimiting character,
* if we find a delimiter set **s to '\0' and return our previous token
* to the user. */
dp = (char *)delim;
while (*dp != '\0') {
if (*s == *dp) {
*s = '\0';
*from = s + 1;
return ret;
}
dp++;
}
s++;
}
/* end of string case */
*from = NULL;
return ret;
}
char *
strchrnul (const char *s, int c_in)
{
char c = c_in;
while (*s && (*s != c))
s++;
return (char *) s;
}
int parseUrlParams(char *queryString, char *results[][2], int resultsMaxCt, boolean decodeUrl) {
int ct = 0;
while (queryString && *queryString && ct < resultsMaxCt) {
results[ct][0] = strsep(&queryString, "&");
results[ct][1] = strchrnul(results[ct][0], '=');
if (*results[ct][1]) *results[ct][1]++ = '\0';
if (decodeUrl) {
percentDecode(results[ct][0]);
percentDecode(results[ct][1]);
}
ct++;
}
return ct;
}
/**
* Perform URL percent decoding.
* Decoding is done in-place and will modify the parameter.
*/
void percentDecode(char *src) {
char *dst = src;
while (*src) {
if (*src == '+') {
src++;
*dst++ = ' ';
}
else if (*src == '%') {
// handle percent escape
*dst = '\0';
src++;
if (*src >= '0' && *src <= '9') {
*dst = *src++ - '0';
}
else if (*src >= 'A' && *src <= 'F') {
*dst = 10 + *src++ - 'A';
}
else if (*src >= 'a' && *src <= 'f') {
*dst = 10 + *src++ - 'a';
}
// this will cause %4 to be decoded to ascii @, but %4 is invalid
// and we can't be expected to decode it properly anyway
*dst <<= 4;
if (*src >= '0' && *src <= '9') {
*dst |= *src++ - '0';
}
else if (*src >= 'A' && *src <= 'F') {
*dst |= 10 + *src++ - 'A';
}
else if (*src >= 'a' && *src <= 'f') {
*dst |= 10 + *src++ - 'a';
}
dst++;
}
else {
*dst++ = *src++;
}
}
*dst = '\0';
}