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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
Original author: Sebastian Guenther
Unit to parse complete URI in its parts or to reassemble an URI
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$IFDEF FPC}
{$MODE objfpc}
{$H+}
{$ENDIF}
unit URIParser;
interface
type
TURI = record
Protocol: String;
Username: String;
Password: String;
Host: String;
Port: Word;
Path: String;
Document: String;
Params: String;
Bookmark: String;
HasAuthority: Boolean;
end;
function EncodeURI(const URI: TURI): String;
function ParseURI(const URI: String; Decode : Boolean = True): TURI; overload;
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word; Decode : Boolean = True): TURI; overload;
function ResolveRelativeURI(const BaseUri, RelUri: WideString;
out ResultUri: WideString): Boolean; overload;
function ResolveRelativeURI(const BaseUri, RelUri: UTF8String;
out ResultUri: UTF8String): Boolean; overload;
function URIToFilename(const URI: string; out Filename: string): Boolean;
function FilenameToURI(const Filename: string; Encode : Boolean = True): string;
function IsAbsoluteURI(const UriReference: string): Boolean;
implementation
uses SysUtils;
const
GenDelims = [':', '/', '?', '#', '[', ']', '@'];
SubDelims = ['!', '$', '&', '''', '(', ')', '*', '+', ',', ';', '='];
ALPHA = ['A'..'Z', 'a'..'z'];
DIGIT = ['0'..'9'];
Unreserved = ALPHA + DIGIT + ['-', '.', '_', '~'];
ValidPathChars = Unreserved + SubDelims + ['@', ':', '/'];
function Escape(const s: String; const Allowed: TSysCharSet): String;
var
i, L: Integer;
P: PChar;
begin
L := Length(s);
for i := 1 to Length(s) do
if not (s[i] in Allowed) then Inc(L,2);
if L = Length(s) then
begin
Result := s;
Exit;
end;
SetLength(Result, L);
P := @Result[1];
for i := 1 to Length(s) do
begin
if not (s[i] in Allowed) then
begin
P^ := '%'; Inc(P);
StrFmt(P, '%.2x', [ord(s[i])]); Inc(P);
end
else
P^ := s[i];
Inc(P);
end;
end;
function EncodeURI(const URI: TURI): String;
// ! if no scheme then first colon in path should be escaped
begin
SetLength(Result, 0);
if Length(URI.Protocol) > 0 then
Result := LowerCase(URI.Protocol) + ':';
if URI.HasAuthority then
begin
Result := Result + '//';
if Length(URI.Username) > 0 then
begin
Result := Result + URI.Username;
if Length(URI.Password) > 0 then
Result := Result + ':' + URI.Password;
Result := Result + '@';
end;
Result := Result + URI.Host;
end;
if URI.Port <> 0 then
Result := Result + ':' + IntToStr(URI.Port);
Result := Result + Escape(URI.Path, ValidPathChars);
if Length(URI.Document) > 0 then
begin
if (Length(URI.Path) > 0) and ((Length(Result) = 0) or (Result[Length(Result)] <> '/')) then
Result := Result + '/';
Result := Result + Escape(URI.Document, ValidPathChars);
end;
if Length(URI.Params) > 0 then
Result := Result + '?' + Escape(URI.Params, ValidPathChars);
if Length(URI.Bookmark) > 0 then
Result := Result + '#' + Escape(URI.Bookmark, ValidPathChars);
end;
function ParseURI(const URI: String; Decode : Boolean = True): TURI;
begin
Result := ParseURI(URI, '', 0, Decode);
end;
function HexValue(c: Char): Integer;
begin
case c of
'0'..'9': Result := ord(c) - ord('0');
'A'..'F': Result := ord(c) - (ord('A') - 10);
'a'..'f': Result := ord(c) - (ord('a') - 10);
else
Result := 0;
end;
end;
function Unescape(const s: String): String;
var
i, RealLength: Integer;
P: PChar;
begin
SetLength(Result, Length(s));
i := 1;
P := PChar(Result); { use PChar to prevent numerous calls to UniqueString }
RealLength := 0;
while i <= Length(s) do
begin
if s[i] = '%' then
begin
P[RealLength] := Chr(HexValue(s[i + 1]) shl 4 or HexValue(s[i + 2]));
Inc(i, 3);
end else
begin
P[RealLength] := s[i];
Inc(i);
end;
Inc(RealLength);
end;
SetLength(Result, RealLength);
end;
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode : Boolean = True): TURI;
var
s, Authority: String;
i,j: Integer;
PortValid: Boolean;
begin
Result.Protocol := LowerCase(DefaultProtocol);
Result.Port := DefaultPort;
s := URI;
// Extract scheme
for i := 1 to Length(s) do
if s[i] = ':' then
begin
Result.Protocol := Copy(s, 1, i - 1);
s := Copy(s, i + 1, MaxInt);
break;
end
else
if not (((i=1) and (s[i] in ALPHA)) or (s[i] in ALPHA + DIGIT + ['+', '-', '.'])) then
break;
// Extract the bookmark
i := LastDelimiter('#', s);
if i > 0 then
begin
Result.Bookmark := Copy(s, i + 1, MaxInt);
if Decode then
Result.Bookmark:=Unescape(Result.Bookmark);
s := Copy(s, 1, i - 1);
end;
// Extract the params
i := LastDelimiter('?', s);
if i > 0 then
begin
Result.Params := Copy(s, i + 1, MaxInt);
if Decode then
Result.Params:=Unescape(Result.Params);
s := Copy(s, 1, i - 1);
end;
// extract authority
if (Length(s) > 1) and (s[1] = '/') and (s[2] = '/') then
begin
i := 3;
while (i <= Length(s)) and (s[i] <> '/') do
Inc(i);
Authority := Copy(s, 3, i-3);
s := Copy(s, i, MaxInt);
Result.HasAuthority := True; // even if Authority is empty
end
else
begin
Result.HasAuthority := False;
Authority := '';
end;
// now s is 'hier-part' per RFC3986
// Extract the document name (nasty...)
for i := Length(s) downto 1 do
if s[i] = '/' then
begin
Result.Document :=Copy(s, i + 1, Length(s));
if Decode then
Result.Document:=Unescape(Result.Document);
if (Result.Document <> '.') and (Result.Document <> '..') then
s := Copy(s, 1, i)
else
Result.Document := '';
break;
end else if s[i] = ':' then
break
else if i = 1 then
begin
Result.Document :=s;
if Decode then
Result.Document:=Unescape(Result.Document);
if (Result.Document <> '.') and (Result.Document <> '..') then
s := ''
else
Result.Document := '';
// break - not needed, last iteration
end;
// Everything left is a path
Result.Path := s;
if Decode then
Result.Path:=Unescape(Result.Path);
// Extract the port number
i := LastDelimiter(':@', Authority);
if (i > 0) and (Authority[i] = ':') then
begin
PortValid := true;
for j:=i+1 to Length(Authority) do
if not (Authority[j] in ['0'..'9']) then
begin
PortValid := false;
break;
end;
if PortValid then
begin
Result.Port := StrToInt(Copy(Authority, i + 1, MaxInt));
Authority := Copy(Authority, 1, i - 1);
end;
end;
// Extract the hostname
i := Pos('@', Authority);
if i > 0 then
begin
Result.Host := Copy(Authority, i+1, MaxInt);
Delete(Authority, i, MaxInt);
// Extract username and password
if Length(Authority) > 0 then
begin
i := Pos(':', Authority);
if i = 0 then
Result.Username := Authority
else
begin
Result.Username := Copy(Authority, 1, i - 1);
Result.Password := Copy(Authority, i + 1, MaxInt);
end;
end;
end
else
Result.Host := Authority;
end;
procedure RemoveDotSegments(var s: string);
var
Cur, Prev: Integer;
begin
Prev := Pos('/', s);
while (Prev > 0) and (Prev < Length(s)) do
begin
Cur := Prev+1;
while (Cur <= Length(s)) and (s[Cur] <> '/') do
Inc(Cur);
if (Cur - Prev = 2) and (s[Prev+1] = '.') then
Delete(s, Prev+1, 2)
else if (Cur - Prev = 3) and (s[Prev+1] = '.') and (s[Prev+2] = '.') then
begin
while (Prev > 1) and (s[Prev-1] <> '/') do
Dec(Prev);
if Prev > 1 then
Dec(Prev);
Delete(s, Prev+1, Cur-Prev);
end
else
Prev := Cur;
end;
end;
function ResolveRelativeURI(const BaseUri, RelUri: UTF8String;
out ResultUri: UTF8String): Boolean;
var
Base, Rel: TUri;
begin
Base := ParseUri(BaseUri);
Rel := ParseUri(RelUri);
Result := (Base.Protocol <> '') or (Rel.Protocol <> '');
if not Result then
Exit;
with Rel do
begin
if (Path = '') and (Document = '') then
begin
if (Protocol = '') and (Host = '') then
begin
if Params <> '' then
Base.Params := Params;
Base.Bookmark := Bookmark;
ResultUri := EncodeUri(Base);
Exit;
end;
end;
if (Protocol <> '') then // RelURI is absolute - return it...
begin
ResultUri := RelUri;
Exit;
end;
// Inherit protocol
Protocol := Base.Protocol;
if (Host = '') then // TODO: or "not HasAuthority"?
begin
// Inherit Authority (host, port, username, password)
Host := Base.Host;
Port := Base.Port;
Username := Base.Username;
Password := Base.Password;
HasAuthority := Base.HasAuthority;
if (Path = '') or (Path[1] <> '/') then // path is empty or relative
Path := Base.Path + Path;
RemoveDotSegments(Path);
end;
end; // with
// EncodeUri percent-encodes the result, and that's good
ResultUri := EncodeUri(Rel);
end;
function ResolveRelativeURI(const BaseUri, RelUri: WideString;
out ResultUri: WideString): Boolean;
var
rslt: UTF8String;
begin
Result := ResolveRelativeURI(UTF8Encode(BaseUri), UTF8Encode(RelUri), rslt);
if Result then
ResultURI := UTF8Decode(rslt);
end;
function URIToFilename(const URI: string; out Filename: string): Boolean;
var
U: TURI;
I: Integer;
begin
Result := False;
U := ParseURI(URI);
if SameText(U.Protocol, 'file') then
begin
if (Length(U.Path) > 2) and (U.Path[1] = '/') and (U.Path[3] = ':') then
Filename := Copy(U.Path, 2, MaxInt)
else
Filename := U.Path;
Filename := Filename + U.Document;
Result := True;
end
else
if U.Protocol = '' then // fire and pray?
begin
Filename := U.Path + U.Document;
Result := True;
end;
if PathDelim <> '/' then
begin
I := Pos('/', Filename);
while I > 0 do
begin
Filename[I] := PathDelim;
I := Pos('/', Filename);
end;
end;
end;
function FilenameToURI(const Filename: string; Encode : Boolean = True): string;
var
I: Integer;
IsAbsFilename: Boolean;
FilenamePart: string;
begin
IsAbsFilename := ((Filename <> '') and (Filename[1] = PathDelim)) or
((Length(Filename) > 2) and (Filename[1] in ['A'..'Z', 'a'..'z']) and (Filename[2] = ':'));
Result := 'file:';
if IsAbsFilename then
begin
if Filename[1] <> PathDelim then
Result := Result + '///'
else
Result := Result + '//';
end;
FilenamePart := Filename;
{ unreachable code warning is ok here }
if PathDelim <> '/' then
begin
I := Pos(PathDelim, FilenamePart);
while I <> 0 do
begin
FilenamePart[I] := '/';
I := Pos(PathDelim, FilenamePart);
end;
end;
if Encode then
FilenamePart := Escape(FilenamePart, ValidPathChars);
Result := Result + FilenamePart;
end;
function IsAbsoluteURI(const UriReference: string): Boolean;
var
I: Integer;
begin
Result := True;
for I := 1 to Length(UriReference) do
begin
if UriReference[I] = ':' then
Exit
else
if not (((I=1) and (UriReference[I] in ALPHA)) or
(UriReference[i] in ALPHA + DIGIT + ['+', '-', '.'])) then
Break;
end;
Result := False;
end;
end.
|