This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.Web.URI.pas
377 lines (325 loc) · 9.94 KB
/
EvilWorks.Web.URI.pas
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//
// EvilLibrary by Vedran Vuk 2010-2012
//
// Name: EvilWorks.Web.URI
// Description: URI and encoding/decoding utilities.
// Contains code from Peter Johnson (http://www.delphidabbler.com/)
//
// File last change date: September 28th. 2012
// File version: 0.0.1
// Comment: Slightly modified, trimmed and integrated with EvilLibrary. And OCD formated.
// Some extra helper classes added.
//
// URIEncode functions:
// Original Name: UURIEncode.pas
// Original Date: 2010-05-28 04:34
// Original Author: Peter Johnson (http://www.delphidabbler.com/)
// Original Licence: MPL 1.1/GPL 2.0/LGPL 2.1
//
unit EvilWorks.Web.URI;
interface
uses
EvilWorks.System.StrUtils;
const
cPercentEncodedSpace = '%20';
cURIGenReservedChars = [':', '/', '?', '#', '[', ']', '@'];
cURISubReservedChars = ['!', '$', '&', '''', '(', ')', '*', '+', ',', ';', '='];
cURLUnreservedChars = ['A' .. 'Z', 'a' .. 'z', '0' .. '9', '-', '_', '.', '~'];
cURIReservedChars = cURIGenReservedChars + cURISubReservedChars + [cPercent];
type
{ TURIParser }
{ Parses an URI. https://user:[email protected]:80/path/to/file.ext?par1=val1&par2=val2 }
{ URI MUST start with a scheme for parser to work. }
TURIParser = record
private
FScheme: string;
FUser : string;
FPass : string;
FHost : string;
FPort : string;
FPath : string;
FQuery : string;
procedure Clear;
public
constructor Create(const aURI: string);
function Parse(const aURI: string): boolean;
property Scheme: string read FScheme;
property User: string read FUser;
property Pass: string read FPass;
property Host: string read FHost;
property Port: string read FPort;
property Path: string read FPath;
property Query: string read FQuery;
function HostPort: string;
function URI: string;
function URIFromPath: string;
function URIEncoded: string;
function URIEncodedFromPath: string;
end;
function URIEncode(const aStr: ansistring): string; overload;
function URIEncode(const aStr: UTF8String): string; overload;
function URIEncode(const aStr: unicodestring): string; overload;
function URIDecode(const aStr: string): string;
function URIEncodeQueryString(const aStr: UTF8String): string; overload;
function URIEncodeQueryString(const aStr: unicodestring): string; overload;
function URIEncodeQueryString(const aStr: ansistring): string; overload;
function URIDecodeQueryString(const aStr: string): string;
implementation
uses
EvilWorks.Web.Base64,
EvilWorks.Web.Punycode;
{ URI encodes aStr. }
function URIEncode(const aStr: ansistring): string; overload;
begin
Result := URIEncode(UTF8Encode(aStr));
end;
{ URI encodes aStr. }
function URIEncode(const aStr: UTF8String): string; overload;
var
ch: AnsiChar;
begin
Result := CEmpty;
for ch in aStr do
begin
if (ch in cURLUnreservedChars) then
Result := Result + WideChar(ch)
else
Result := Result + cPercent + TextIntToHex(Ord(ch), 2);
end;
end;
{ URI encodes aStr. }
function URIEncode(const aStr: unicodestring): string; overload;
begin
Result := URIEncode(UTF8Encode(aStr));
end;
{ Decodes a URI encoded string. }
function URIDecode(const aStr: string): string;
{ Counts number of '%' characters in a UTF8 string. }
function CountPercent(const S: UTF8String): Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(S) do
if S[i] = cPercent then
Inc(Result);
end;
var
srcUTF8: UTF8String; // input string as UTF-8
srcIdx : Integer; // index into source UTF-8 string
resUTF8: UTF8String; // output string as UTF-8
resIdx : Integer; // index into result UTF-8 string
hex : string; // hex component of % encoding
chVal : Integer; // character ordinal value from a % encoding
begin
// Convert input string to UTF-8
srcUTF8 := UTF8Encode(aStr);
// Size the decoded UTF-8 string: each 3 byte sequence starting with '%' is
// replaced by a single byte. All other bytes are copied unchanged.
SetLength(resUTF8, Length(srcUTF8) - 2 * CountPercent(srcUTF8));
srcIdx := 1;
resIdx := 1;
while srcIdx <= Length(srcUTF8) do
begin
if srcUTF8[srcIdx] = cPercent then
begin
// % encoding: decode following two hex chars into required code point
if Length(srcUTF8) < srcIdx + 2 then
Exit('');
hex := '$' + string(srcUTF8[srcIdx + 1] + srcUTF8[srcIdx + 2]);
chVal := TextToInt(hex, - 1);
if (chVal = - 1) then
Exit('');
resUTF8[resIdx] := AnsiChar(chVal);
Inc(resIdx);
Inc(srcIdx, 3);
end
else
begin
// plain char or UTF-8 continuation character: copy unchanged
resUTF8[resIdx] := srcUTF8[srcIdx];
Inc(resIdx);
Inc(srcIdx);
end;
end;
// Convert back to native string type for result
Result := UTF8ToString(resUTF8);
end;
{ URI encodes query aStr component. Spaces in original string are encoded as "+".}
function URIEncodeQueryString(const aStr: ansistring): string; overload;
begin
Result := URIEncodeQueryString(UTF8Encode(aStr));
end;
{ URI encodes query aStr component. Spaces in original string are encoded as "+".}
function URIEncodeQueryString(const aStr: UTF8String): string; overload;
begin
// First we URI encode the string. This so any existing '+' symbols get
// encoded because we use them to replace spaces and we can't confuse '+'
// already in URI with those that we add. After this step spaces get encoded
// as %20. So next we replace all occurences of %20 with '+'.
Result := TextReplace(URIEncode(aStr), cPercentEncodedSpace, cPlus);
end;
{ URI encodes query aStr component. Spaces in original string are encoded as "+".}
function URIEncodeQueryString(const aStr: unicodestring): string; overload;
begin
Result := URIEncodeQueryString(UTF8Encode(aStr));
end;
{ Decodes a URI encoded query aStr where spaces have been encoded as '+'. }
function URIDecodeQueryString(const aStr: string): string;
begin
// First replace plus signs with spaces. We use percent-encoded spaces here
// because string is still URI encoded and space is not one of unreserved
// chars and therefor should be percent-encoded. Finally we decode the
// percent-encoded string.
Result := URIDecode(TextReplace(aStr, cPlus, cPercentEncodedSpace, True));
end;
{ ========== }
{ TURIParser }
{ ========== }
{ Parses an URI. Get parts via public properties. }
constructor TURIParser.Create(const aURI: string);
begin
Parse(aURI);
end;
{ Clears internal vars. }
procedure TURIParser.Clear;
begin
FScheme := CEmpty;
FUser := CEmpty;
FPass := CEmpty;
FHost := CEmpty;
FPort := CEmpty;
FPath := CEmpty;
FQuery := CEmpty;
end;
{ Parses a URI. Get parts via public properties. }
function TURIParser.Parse(const aURI: string): boolean;
var
str: string;
begin
if (aURI = CEmpty) then
Exit(False)
else
Result := True;
str := aURI;
// Get scheme
FScheme := TextExtractLeft(str, CURISchemeDelimiter, True);
if (FScheme = CEmpty) then
FScheme := TextExtractLeft(str, CColon, True);
if (FScheme = CEmpty) then
begin
Clear;
Exit(False);
end;
// Extract optional user:pass@
FUser := TextExtractLeft(str, CMonkey, True);
// Extract optional pass.
if (FUser <> CEmpty) then
FPass := TextExtractRight(FUser, CColon, True);
// Extract host.
FHost := TextExtractLeft(str, CFrontSlash, True, False);
if (FHost = CEmpty) then
begin
FHost := str;
if (FHost = CEmpty) then
begin
Clear;
Exit(False);
end
else
str := CEmpty;
end;
// Extract optional port.
FPort := TextExtractRight(FHost, CColon, True);
// Extract optional path.
FPath := TextExtractLeft(str, CQuestionMark, True);
if (FPath = CEmpty) then
begin
FPath := str;
if (FPath = CEmpty) then
FPath := CFrontSlash;
Exit;
end;
// What's left is the query.
FQuery := str;
end;
{ Returns "Host:Port" }
function TURIParser.HostPort: string;
begin
if (Host = '') then
Exit('')
else
Result := FHost;
if (FPort <> '') then
Result := Result + ':' + FPort;
end;
{ Returns the parsed URI joined and encoded for HTTP method line. }
{ 'GET' + ' ' + TURIParser.URIEncoded }
function TURIParser.URI: string;
begin
Result := CEmpty;
if (FScheme = CEmpty) or (FHost = CEmpty) then
Exit;
Result := FScheme + CURISchemeDelimiter;
if (FUser <> CEmpty) then
Result := Result + FUser;
if (FPass <> CEmpty) then
Result := Result + CColon + FPass + CMonkey
else if (FUser <> CEmpty) then
Result := Result + CMonkey;
Result := Result + FHost;
Result := Result + URIFromPath;
end;
function TURIParser.URIFromPath: string;
var
pathTokens: TTokens;
i : integer;
begin
Result := CEmpty;
if (FPath = CEmpty) then
Exit;
// Encode path parts, but don't encode path delimiters.
pathTokens := TextTokenize(FPath, CFrontSlash);
for i := 0 to pathTokens.Count - 1 do
Result := Result + CFrontSlash + pathTokens[i];
if (TextEnds(FPath, CFrontSlash, True)) then
Result := Result + CFrontSlash;
// Encode query.
if (FQuery <> CEmpty) then
Result := Result + CQuestionMark + FQuery;
end;
function TURIParser.URIEncoded: string;
begin
Result := CEmpty;
if (FScheme = CEmpty) or (FHost = CEmpty) then
Exit;
Result := FScheme + CURISchemeDelimiter;
if (FUser <> CEmpty) then
Result := Result + FUser;
if (FPass <> CEmpty) then
Result := Result + CColon + FPass + CMonkey
else if (FUser <> CEmpty) then
Result := Result + CMonkey;
Result := Result + FHost;
Result := Result + URIEncodedFromPath;
end;
{ Same as URIEncoded(), but starts from path: /path/to/file.json?par1%3Dval1%26par2%3Dval2 }
function TURIParser.URIEncodedFromPath: string;
var
pathTokens: TTokens;
i : integer;
begin
Result := CEmpty;
if (FPath = CEmpty) then
Exit;
// Encode path parts, but don't encode path delimiters.
pathTokens := TextTokenize(FPath, CFrontSlash);
for i := 0 to pathTokens.Count - 1 do
Result := Result + CFrontSlash + URIEncode(pathTokens[i]);
if (TextEnds(FPath, CFrontSlash, True)) then
Result := Result + CFrontSlash;
// Encode query.
if (FQuery <> CEmpty) then
Result := Result + CQuestionMark + URIEncodeQueryString(FQuery);
end;
end.