-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUrlUtils.cpp
More file actions
246 lines (211 loc) · 6.52 KB
/
UrlUtils.cpp
File metadata and controls
246 lines (211 loc) · 6.52 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "Base.h"
#include "UrlUtils.h"
NS_BEGIN
//#################################################################################################
CStr8 UrlEscape8(PCSTR szUrl)
{
CStr8 strEscapedUrl(szUrl);
UrlEscape8(strEscapedUrl);
return strEscapedUrl;
}
//#################################################################################################
void UrlEscape8(CStr8 &strUrl)
{
CStr8 strReplace;
for(size_t nPos = 0; nPos < strUrl.GetLength(); ++nPos)
{
unsigned char ch = strUrl.GetAt(nPos);
if(ch <= ')' ||
ch == ',' ||
(ch >= ':' && ch <= '?') ||
(ch >= '[' && ch <= '^') ||
ch == '`' ||
ch >= '{')
{
strReplace.Printf("%%%02X", ch);
strUrl.Delete(nPos);
strUrl.Insert(nPos, strReplace);
nPos += strReplace.GetLength() - 1;
}
}
}
//#################################################################################################
CStr8 UrlUnescape8(PCSTR szUrl)
{
CStr8 strUnescapedUrl(szUrl);
UrlUnescape8(strUnescapedUrl);
return strUnescapedUrl;
}
//#################################################################################################
void UrlUnescape8(CStr8 &strUrl)
{
size_t nPos;
size_t nStart = 0;
while((nPos = strUrl.Find('%', nStart)) != CStr8::not_found)
{
char ch1 = strUrl.GetAt(nPos + 1);
char ch2 = strUrl.GetAt(nPos + 2);
if(IsHexChar8(ch1) && IsHexChar8(ch2))
{
char chReplace = StringToInteger8<char>(strUrl.GetMid(nPos + 1, 2), 16);
strUrl.Delete(nPos, 3);
strUrl.Insert(nPos, chReplace);
}
nStart = nPos + 1;
}
}
//#################################################################################################
CStrW UrlEscapeW(PCWSTR szUrl)
{
CStrW strEscapedUrl(szUrl);
UrlEscapeW(strEscapedUrl);
return strEscapedUrl;
}
//#################################################################################################
void UrlEscapeW(CStrW &strUrl)
{
CStrW strReplace;
for(size_t nPos = 0; nPos < strUrl.GetLength(); ++nPos)
{
wchar_t ch = strUrl.GetAt(nPos);
if(ch <= L')' ||
ch == L',' ||
(ch >= L':' && ch <= L'?') ||
(ch >= L'[' && ch <= L'^') ||
ch == L'`' ||
(ch >= L'{' && ch <= 255))
{
strReplace.Printf(L"%%%02X", ch);
strUrl.Delete(nPos);
strUrl.Insert(nPos, strReplace);
nPos += strReplace.GetLength() - 1;
}
else if(ch > 255)
{
strReplace.Printf(L"%%u%04X", ch);
strUrl.Delete(nPos);
strUrl.Insert(nPos, strReplace);
nPos += strReplace.GetLength() - 1;
}
}
}
//#################################################################################################
CStrW UrlUnescapeW(PCWSTR szUrl)
{
CStrW strUnescapedUrl(szUrl);
UrlUnescapeW(strUnescapedUrl);
return strUnescapedUrl;
}
//#################################################################################################
void UrlUnescapeW(CStrW &strUrl)
{
size_t nPos;
size_t nStart = 0;
while((nPos = strUrl.Find(L'%', nStart)) != CStrW::not_found)
{
wchar_t ch1 = strUrl.GetAt(nPos + 1);
wchar_t ch2 = strUrl.GetAt(nPos + 2);
wchar_t ch3 = strUrl.GetAt(nPos + 3);
wchar_t ch4 = strUrl.GetAt(nPos + 4);
wchar_t ch5 = strUrl.GetAt(nPos + 5);
if(IsHexCharW(ch1) && IsHexCharW(ch2))
{
wchar_t chReplace = StringToIntegerW<wchar_t>(strUrl.GetMid(nPos + 1, 2), 16);
strUrl.Delete(nPos, 3);
strUrl.Insert(nPos, chReplace);
}
else if(ch1 == L'u' && IsHexCharW(ch2) && IsHexCharW(ch3) && IsHexCharW(ch4) && IsHexCharW(ch5))
{
wchar_t chReplace = StringToIntegerW<wchar_t>(strUrl.GetMid(nPos + 2, 4), 16);
strUrl.Delete(nPos, 6);
strUrl.Insert(nPos, chReplace);
}
nStart = nPos + 1;
}
}
//#################################################################################################
CStr8 UrlEncode8(PCSTR szUrl)
{
CStr8 strEncodedUrl(szUrl);
UrlEncode8(strEncodedUrl);
return strEncodedUrl;
}
//#################################################################################################
void UrlEncode8(CStr8 &strUrl)
{
CStr8 strReplace;
for(size_t nPos = 0; nPos < strUrl.GetLength(); ++nPos)
{
unsigned char ch = strUrl.GetAt(nPos);
if(ch <= ' ' ||
(ch >= '"' && ch <= '&') ||
ch == '+' ||
ch == ',' ||
ch == '/' ||
(ch >= ':' && ch <= '@') ||
(ch >= '[' && ch <= '^') ||
ch == '`' ||
(ch >= '{' && ch <= '}') ||
ch >= 127)
{
strReplace.Printf("%%%02X", ch);
strUrl.Delete(nPos);
strUrl.Insert(nPos, strReplace);
nPos += strReplace.GetLength() - 1;
}
}
}
//#################################################################################################
CStr8 UrlDecode8(PCSTR szUrl)
{
CStr8 strDecodedUrl(szUrl);
UrlDecode8(strDecodedUrl);
return strDecodedUrl;
}
//#################################################################################################
void UrlDecode8(CStr8 &strUrl)
{
size_t nPos;
size_t nStart = 0;
while((nPos = strUrl.Find('%', nStart)) != CStr8::not_found)
{
char ch1 = strUrl.GetAt(nPos + 1);
char ch2 = strUrl.GetAt(nPos + 2);
if(IsHexChar8(ch1) && IsHexChar8(ch2))
{
char chReplace = StringToInteger8<char>(strUrl.GetMid(nPos + 1, 2), 16);
strUrl.Delete(nPos, 3);
strUrl.Insert(nPos, chReplace);
}
nStart = nPos + 1;
}
}
//#################################################################################################
CStrW UrlEncodeW(PCWSTR szUrl)
{ // URL encoding is based on UTF8 character codes, so convert the URL to UTF8 and back
CStr8 strEncodedUrl(szUrl);
UrlEncode8(strEncodedUrl);
return strEncodedUrl.AsWide();
}
//#################################################################################################
void UrlEncodeW(CStrW &strUrl)
{ // URL encoding is based on UTF8 character codes, so convert the URL to UTF8 and back
CStr8 strEncodedUrl(strUrl);
UrlEncode8(strEncodedUrl);
strUrl = strEncodedUrl;
}
//#################################################################################################
CStrW UrlDecodeW(PCWSTR szUrl)
{ // URL encoding is based on UTF8 character codes, so convert the URL to UTF8 and back
CStr8 strDecodedUrl(szUrl);
UrlDecode8(strDecodedUrl);
return strDecodedUrl.AsWide();
}
//#################################################################################################
void UrlDecodeW(CStrW &strUrl)
{ // URL encoding is based on UTF8 character codes, so convert the URL to UTF8 and back
CStr8 strDecodedUrl(strUrl);
UrlDecode8(strDecodedUrl);
strUrl = strDecodedUrl;
}
NS_END