| 1 |
{ |
| 2 |
This file is part of the Free Pascal run time library. |
| 3 |
Copyright (c) 2003 by the Free Pascal development team |
| 4 |
Original author: Sebastian Guenther |
| 5 |
|
| 6 |
Unit to parse complete URI in its parts or to reassemble an URI |
| 7 |
|
| 8 |
See the file COPYING.FPC, included in this distribution, |
| 9 |
for details about the copyright. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
|
| 15 |
**********************************************************************} |
| 16 |
|
| 17 |
{$I castleconf.inc} // hide useless Delphi hints/warnings |
| 18 |
|
| 19 |
{$IFDEF FPC} |
| 20 |
{$MODE objfpc} |
| 21 |
{$H+} |
| 22 |
{$ENDIF} |
| 23 |
|
| 24 |
unit URIParser; |
| 25 |
|
| 26 |
interface |
| 27 |
|
| 28 |
type |
| 29 |
TURI = record |
| 30 |
Protocol: String; |
| 31 |
Username: String; |
| 32 |
Password: String; |
| 33 |
Host: String; |
| 34 |
Port: Word; |
| 35 |
Path: String; |
| 36 |
Document: String; |
| 37 |
Params: String; |
| 38 |
Bookmark: String; |
| 39 |
HasAuthority: Boolean; |
| 40 |
end; |
| 41 |
|
| 42 |
function EncodeURI(const URI: TURI): String; |
| 43 |
function ParseURI(const URI: String; Decode : Boolean = True): TURI; overload; |
| 44 |
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word; Decode : Boolean = True): TURI; overload; |
| 45 |
|
| 46 |
function ResolveRelativeURI(const BaseUri, RelUri: UnicodeString;out ResultUri: UnicodeString): Boolean; overload; |
| 47 |
{$ifdef WINDOWS} |
| 48 |
function ResolveRelativeURI(const BaseUri, RelUri: WideString; out ResultUri: WideString): Boolean; overload; |
| 49 |
{$ENDIF} |
| 50 |
function ResolveRelativeURI(const BaseUri, RelUri: AnsiString; out ResultUri: AnsiString): Boolean; overload; |
| 51 |
|
| 52 |
function UriToFilename(const URI: String; out Filename: String): Boolean; |
| 53 |
function FilenameToUri(const Filename: String; Encode : Boolean = True): String; |
| 54 |
|
| 55 |
function IsAbsoluteURI(const UriReference: String): Boolean; |
| 56 |
|
| 57 |
implementation |
| 58 |
|
| 59 |
uses SysUtils, CastleUriUtils; |
| 60 |
|
| 61 |
const |
| 62 |
GenDelims = [':', '/', '?', '#', '[', ']', '@']; |
| 63 |
SubDelims = ['!', '$', '&', '''', '(', ')', '*', '+', ',', ';', '=']; |
| 64 |
ALPHA = ['A'..'Z', 'a'..'z']; |
| 65 |
DIGIT = ['0'..'9']; |
| 66 |
Unreserved = ALPHA + DIGIT + ['-', '.', '_', '~']; |
| 67 |
ValidPathChars = Unreserved + SubDelims + ['@', ':', '/']; |
| 68 |
|
| 69 |
function EncodeURI(const URI: TURI): String; |
| 70 |
// ! if no scheme then first colon in path should be escaped |
| 71 |
begin |
| 72 |
SetLength(Result, 0); |
| 73 |
if Length(URI.Protocol) > 0 then |
| 74 |
Result := LowerCase(URI.Protocol) + ':'; |
| 75 |
if URI.HasAuthority then |
| 76 |
begin |
| 77 |
Result := Result + '//'; |
| 78 |
if Length(URI.Username) > 0 then |
| 79 |
begin |
| 80 |
Result := Result + URI.Username; |
| 81 |
if Length(URI.Password) > 0 then |
| 82 |
Result := Result + ':' + URI.Password; |
| 83 |
Result := Result + '@'; |
| 84 |
end; |
| 85 |
Result := Result + URI.Host; |
| 86 |
end; |
| 87 |
if URI.Port <> 0 then |
| 88 |
Result := Result + ':' + IntToStr(URI.Port); |
| 89 |
Result := Result + InternalURIEscape(URI.Path); |
| 90 |
if Length(URI.Document) > 0 then |
| 91 |
begin |
| 92 |
if (Length(URI.Path) > 0) and ((Length(Result) = 0) or (Result[Length(Result)] <> '/')) then |
| 93 |
Result := Result + '/'; |
| 94 |
Result := Result + InternalURIEscape(URI.Document); |
| 95 |
end; |
| 96 |
if Length(URI.Params) > 0 then |
| 97 |
Result := Result + '?' + InternalURIEscape(URI.Params); |
| 98 |
if Length(URI.Bookmark) > 0 then |
| 99 |
Result := Result + '#' + InternalURIEscape(URI.Bookmark); |
| 100 |
end; |
| 101 |
|
| 102 |
function ParseURI(const URI: String; Decode : Boolean = True): TURI; |
| 103 |
begin |
| 104 |
Result := ParseURI(URI, '', 0, Decode); |
| 105 |
end; |
| 106 |
|
| 107 |
function HexValue(c: Char): Integer; |
| 108 |
begin |
| 109 |
case c of |
| 110 |
'0'..'9': Result := ord(c) - ord('0'); |
| 111 |
'A'..'F': Result := ord(c) - (ord('A') - 10); |
| 112 |
'a'..'f': Result := ord(c) - (ord('a') - 10); |
| 113 |
else |
| 114 |
Result := 0; |
| 115 |
end; |
| 116 |
end; |
| 117 |
|
| 118 |
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word;Decode : Boolean = True): TURI; |
| 119 |
|
| 120 |
var |
| 121 |
s, Authority: String; |
| 122 |
i,j: Integer; |
| 123 |
PortValid: Boolean; |
| 124 |
|
| 125 |
begin |
| 126 |
Result:=Default(TURI); |
| 127 |
Result.Protocol := LowerCase(DefaultProtocol); |
| 128 |
Result.Port := DefaultPort; |
| 129 |
|
| 130 |
s := URI; |
| 131 |
|
| 132 |
// Extract scheme |
| 133 |
|
| 134 |
for i := 1 to Length(s) do |
| 135 |
if s[i] = ':' then |
| 136 |
begin |
| 137 |
Result.Protocol := Copy(s, 1, i - 1); |
| 138 |
s := Copy(s, i + 1, MaxInt); |
| 139 |
break; |
| 140 |
end |
| 141 |
else |
| 142 |
if not (((i=1) and (s[i] in ALPHA)) or (s[i] in ALPHA + DIGIT + ['+', '-', '.'])) then |
| 143 |
break; |
| 144 |
|
| 145 |
// Extract the bookmark |
| 146 |
|
| 147 |
i := LastDelimiter('#', s); |
| 148 |
if i > 0 then |
| 149 |
begin |
| 150 |
Result.Bookmark := Copy(s, i + 1, MaxInt); |
| 151 |
if Decode then |
| 152 |
Result.Bookmark:=InternalURIUnescape(Result.Bookmark); |
| 153 |
s := Copy(s, 1, i - 1); |
| 154 |
end; |
| 155 |
|
| 156 |
// Extract the params |
| 157 |
|
| 158 |
i := LastDelimiter('?', s); |
| 159 |
if i > 0 then |
| 160 |
begin |
| 161 |
Result.Params := Copy(s, i + 1, MaxInt); |
| 162 |
if Decode then |
| 163 |
Result.Params:=InternalURIUnescape(Result.Params); |
| 164 |
s := Copy(s, 1, i - 1); |
| 165 |
end; |
| 166 |
|
| 167 |
// extract authority |
| 168 |
|
| 169 |
if (Length(s) > 1) and (s[1] = '/') and (s[2] = '/') then |
| 170 |
begin |
| 171 |
i := 3; |
| 172 |
while (i <= Length(s)) and (s[i] <> '/') do |
| 173 |
Inc(i); |
| 174 |
Authority := Copy(s, 3, i-3); |
| 175 |
s := Copy(s, i, MaxInt); |
| 176 |
Result.HasAuthority := True; // even if Authority is empty |
| 177 |
end |
| 178 |
else |
| 179 |
begin |
| 180 |
Result.HasAuthority := False; |
| 181 |
Authority := ''; |
| 182 |
end; |
| 183 |
|
| 184 |
// now s is 'hier-part' per RFC3986 |
| 185 |
// Extract the document name (nasty...) |
| 186 |
|
| 187 |
for i := Length(s) downto 1 do |
| 188 |
if s[i] = '/' then |
| 189 |
begin |
| 190 |
Result.Document :=Copy(s, i + 1, Length(s)); |
| 191 |
if Decode then |
| 192 |
Result.Document:=InternalURIUnescape(Result.Document); |
| 193 |
if (Result.Document <> '.') and (Result.Document <> '..') then |
| 194 |
s := Copy(s, 1, i) |
| 195 |
else |
| 196 |
Result.Document := ''; |
| 197 |
break; |
| 198 |
end else if s[i] = ':' then |
| 199 |
break |
| 200 |
else if i = 1 then |
| 201 |
begin |
| 202 |
Result.Document :=s; |
| 203 |
if Decode then |
| 204 |
Result.Document:=InternalURIUnescape(Result.Document); |
| 205 |
if (Result.Document <> '.') and (Result.Document <> '..') then |
| 206 |
s := '' |
| 207 |
else |
| 208 |
Result.Document := ''; |
| 209 |
// break - not needed, last iteration |
| 210 |
end; |
| 211 |
|
| 212 |
// Everything left is a path |
| 213 |
|
| 214 |
Result.Path := s; |
| 215 |
if Decode then |
| 216 |
Result.Path:=InternalURIUnescape(Result.Path); |
| 217 |
|
| 218 |
// Extract the port number |
| 219 |
|
| 220 |
i := LastDelimiter(':@', Authority); |
| 221 |
if (i > 0) and (i < Length(Authority)) and (Authority[i] = ':') then |
| 222 |
begin |
| 223 |
PortValid := true; |
| 224 |
for j:=i+1 to Length(Authority) do |
| 225 |
if not (Authority[j] in ['0'..'9']) then |
| 226 |
begin |
| 227 |
PortValid := false; |
| 228 |
break; |
| 229 |
end; |
| 230 |
if PortValid then |
| 231 |
begin |
| 232 |
Result.Port := StrToInt(Copy(Authority, i + 1, MaxInt)); |
| 233 |
Authority := Copy(Authority, 1, i - 1); |
| 234 |
end; |
| 235 |
end; |
| 236 |
|
| 237 |
// Extract the hostname |
| 238 |
|
| 239 |
i := Pos('@', Authority); |
| 240 |
if i > 0 then |
| 241 |
begin |
| 242 |
Result.Host := Copy(Authority, i+1, MaxInt); |
| 243 |
Delete(Authority, i, MaxInt); |
| 244 |
|
| 245 |
// Extract username and password |
| 246 |
if Length(Authority) > 0 then |
| 247 |
begin |
| 248 |
i := Pos(':', Authority); |
| 249 |
if i = 0 then |
| 250 |
Result.Username := Authority |
| 251 |
else |
| 252 |
begin |
| 253 |
Result.Username := Copy(Authority, 1, i - 1); |
| 254 |
Result.Password := Copy(Authority, i + 1, MaxInt); |
| 255 |
end; |
| 256 |
end; |
| 257 |
end |
| 258 |
else |
| 259 |
Result.Host := Authority; |
| 260 |
end; |
| 261 |
|
| 262 |
procedure RemoveDotSegments(var s: String); |
| 263 |
var |
| 264 |
Cur, Prev: Integer; |
| 265 |
begin |
| 266 |
Prev := Pos('/', s); |
| 267 |
while (Prev > 0) and (Prev < Length(s)) do |
| 268 |
begin |
| 269 |
Cur := Prev+1; |
| 270 |
while (Cur <= Length(s)) and (s[Cur] <> '/') do |
| 271 |
Inc(Cur); |
| 272 |
if (Cur - Prev = 2) and (s[Prev+1] = '.') then |
| 273 |
Delete(s, Prev+1, 2) |
| 274 |
else if (Cur - Prev = 3) and (s[Prev+1] = '.') and (s[Prev+2] = '.') then |
| 275 |
begin |
| 276 |
while (Prev > 1) and (s[Prev-1] <> '/') do |
| 277 |
Dec(Prev); |
| 278 |
if Prev > 1 then |
| 279 |
Dec(Prev); |
| 280 |
Delete(s, Prev+1, Cur-Prev); |
| 281 |
end |
| 282 |
else |
| 283 |
Prev := Cur; |
| 284 |
end; |
| 285 |
end; |
| 286 |
|
| 287 |
function ResolveRelativeURI(const BaseUri, RelUri: AnsiString; out ResultUri: AnsiString): Boolean; |
| 288 |
var |
| 289 |
Base, Rel: TUri; |
| 290 |
begin |
| 291 |
Base := ParseUri(BaseUri); |
| 292 |
Rel := ParseUri(RelUri); |
| 293 |
|
| 294 |
Result := (Base.Protocol <> '') or (Rel.Protocol <> ''); |
| 295 |
if not Result then |
| 296 |
Exit; |
| 297 |
with Rel do |
| 298 |
begin |
| 299 |
if (Path = '') and (Document = '') then |
| 300 |
begin |
| 301 |
if (Protocol = '') and (Host = '') then |
| 302 |
begin |
| 303 |
if Params <> '' then |
| 304 |
Base.Params := Params; |
| 305 |
Base.Bookmark := Bookmark; |
| 306 |
ResultUri := EncodeUri(Base); |
| 307 |
Exit; |
| 308 |
end; |
| 309 |
end; |
| 310 |
if (Protocol <> '') then // RelURI is absolute - return it... |
| 311 |
begin |
| 312 |
ResultUri := RelUri; |
| 313 |
Exit; |
| 314 |
end; |
| 315 |
// Inherit protocol |
| 316 |
Protocol := Base.Protocol; |
| 317 |
if (Host = '') then // TODO: or "not HasAuthority"? |
| 318 |
begin |
| 319 |
// Inherit Authority (host, port, username, password) |
| 320 |
Host := Base.Host; |
| 321 |
Port := Base.Port; |
| 322 |
Username := Base.Username; |
| 323 |
Password := Base.Password; |
| 324 |
HasAuthority := Base.HasAuthority; |
| 325 |
if (Path = '') or (Path[1] <> '/') then // path is empty or relative |
| 326 |
Path := Base.Path + Path; |
| 327 |
RemoveDotSegments(Path); |
| 328 |
end; |
| 329 |
end; // with |
| 330 |
|
| 331 |
// EncodeUri percent-encodes the result, and that's good |
| 332 |
ResultUri := EncodeUri(Rel); |
| 333 |
end; |
| 334 |
|
| 335 |
{$IFDEF WINDOWS} |
| 336 |
function ResolveRelativeURI(const BaseUri, RelUri: WideString; out ResultUri: WideString): Boolean; |
| 337 |
|
| 338 |
Var |
| 339 |
Res : AnsiString; |
| 340 |
|
| 341 |
begin |
| 342 |
Result := ResolveRelativeURI(UTF8Encode(BaseUri), UTF8Encode(RelUri), Res); |
| 343 |
if Result then |
| 344 |
ResultURI := UTF8Decode(res); |
| 345 |
end; |
| 346 |
{$ENDIF} |
| 347 |
|
| 348 |
function ResolveRelativeURI(const BaseUri, RelUri: UnicodeString; |
| 349 |
out ResultUri: UnicodeString): Boolean; |
| 350 |
var |
| 351 |
rslt: AnsiString; |
| 352 |
begin |
| 353 |
Result := ResolveRelativeURI(UTF8Encode(BaseUri), UTF8Encode(RelUri), rslt); |
| 354 |
if Result then |
| 355 |
ResultURI := UTF8Decode(rslt); |
| 356 |
end; |
| 357 |
|
| 358 |
function UriToFilename(const URI: String; out Filename: String): Boolean; |
| 359 |
var |
| 360 |
U: TURI; |
| 361 |
I: Integer; |
| 362 |
begin |
| 363 |
Result := False; |
| 364 |
U := ParseURI(URI); |
| 365 |
if SameText(U.Protocol, 'file') then |
| 366 |
begin |
| 367 |
if (Length(U.Path) > 2) and (U.Path[1] = '/') and (U.Path[3] = ':') then |
| 368 |
Filename := Copy(U.Path, 2, MaxInt) |
| 369 |
else |
| 370 |
Filename := U.Path; |
| 371 |
Filename := Filename + U.Document; |
| 372 |
Result := True; |
| 373 |
end |
| 374 |
else |
| 375 |
if U.Protocol = '' then // fire and pray? |
| 376 |
begin |
| 377 |
Filename := U.Path + U.Document; |
| 378 |
Result := True; |
| 379 |
end; |
| 380 |
if PathDelim <> '/' then |
| 381 |
begin |
| 382 |
I := Pos('/', Filename); |
| 383 |
while I > 0 do |
| 384 |
begin |
| 385 |
Filename[I] := PathDelim; |
| 386 |
I := Pos('/', Filename); |
| 387 |
end; |
| 388 |
end; |
| 389 |
end; |
| 390 |
|
| 391 |
function FilenameToUri(const Filename: String; Encode : Boolean = True): String; |
| 392 |
var |
| 393 |
I: Integer; |
| 394 |
IsAbsFilename: Boolean; |
| 395 |
FilenamePart: String; |
| 396 |
begin |
| 397 |
IsAbsFilename := ((Filename <> '') and (Filename[1] = PathDelim)) or |
| 398 |
((Length(Filename) > 2) and (Filename[1] in ['A'..'Z', 'a'..'z']) and (Filename[2] = ':')); |
| 399 |
|
| 400 |
Result := 'file:'; |
| 401 |
if IsAbsFilename then |
| 402 |
begin |
| 403 |
if Filename[1] <> PathDelim then |
| 404 |
Result := Result + '///' |
| 405 |
else |
| 406 |
Result := Result + '//'; |
| 407 |
end; |
| 408 |
|
| 409 |
FilenamePart := Filename; |
| 410 |
{ unreachable code warning is ok here } |
| 411 |
if PathDelim <> '/' then |
| 412 |
begin |
| 413 |
I := Pos(PathDelim, FilenamePart); |
| 414 |
while I <> 0 do |
| 415 |
begin |
| 416 |
FilenamePart[I] := '/'; |
| 417 |
I := Pos(PathDelim, FilenamePart); |
| 418 |
end; |
| 419 |
end; |
| 420 |
if Encode then |
| 421 |
FilenamePart := InternalURIEscape(FilenamePart); |
| 422 |
|
| 423 |
Result := Result + FilenamePart; |
| 424 |
end; |
| 425 |
|
| 426 |
|
| 427 |
function IsAbsoluteURI(const UriReference: String): Boolean; |
| 428 |
var |
| 429 |
I: Integer; |
| 430 |
begin |
| 431 |
Result := True; |
| 432 |
for I := 1 to Length(UriReference) do |
| 433 |
begin |
| 434 |
if UriReference[I] = ':' then |
| 435 |
Exit |
| 436 |
else |
| 437 |
if not (((I=1) and (UriReference[I] in ALPHA)) or |
| 438 |
(UriReference[i] in ALPHA + DIGIT + ['+', '-', '.'])) then |
| 439 |
Break; |
| 440 |
end; |
| 441 |
Result := False; |
| 442 |
end; |
| 443 |
|
| 444 |
|
| 445 |
end. |