| 1 |
{ |
| 2 |
Copyright 2007-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ URI utilities. These extend standard FPC URIParser unit. } |
| 17 |
unit CastleUriUtils; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Classes, |
| 24 |
CastleStringUtils; |
| 25 |
|
| 26 |
{ Extracts #anchor from URI. On input, URI contains full URI. |
| 27 |
On output, Anchor is removed from URI and saved in Anchor. |
| 28 |
If no #anchor existed, Anchor is set to ''. } |
| 29 |
procedure UriExtractAnchor(var Uri: String; out Anchor: string); |
| 30 |
|
| 31 |
{ Like UriExtractAnchor, but URI remains unchanged. } |
| 32 |
procedure UriGetAnchor(const Uri: String; out Anchor: string); |
| 33 |
|
| 34 |
{ Calculate #anchor from an URI, and split it into a key-value map. |
| 35 |
|
| 36 |
This supports special CGE syntax within URL anchor to specify loading parameters for |
| 37 |
@url(https://castle-engine.io/spine Spine), |
| 38 |
@url(https://castle-engine.io/sprite_sheets sprite sheets), |
| 39 |
@url(https://castle-engine.io/using_images images). |
| 40 |
|
| 41 |
On output, the key-value pairs from anchor are saved in TStringStringMap. |
| 42 |
The SettingsFromAnchor is always cleared at the beginning. |
| 43 |
If no anchor existed, SettingsFromAnchor will be empty when this ends. } |
| 44 |
procedure UriGetSettingsFromAnchor(const Uri: String; |
| 45 |
const SettingsFromAnchor: TStringStringMap); |
| 46 |
|
| 47 |
{ Return URI with anchor (if was any) stripped. } |
| 48 |
function UriDeleteAnchor(const Uri: String): string; |
| 49 |
|
| 50 |
{ Replace all sequences like %xx with their actual 8-bit characters. |
| 51 |
|
| 52 |
The intention is that this is similar to PHP function with the same name. |
| 53 |
|
| 54 |
To account for badly encoded strings, invalid encoded URIs do not |
| 55 |
raise an error --- they are only reported to WritelnWarning. |
| 56 |
So you can simply ignore them, or write a warning about them for user. |
| 57 |
This is done because often you will use this with |
| 58 |
URIs provided by the user, read from some file etc., so you can't be sure |
| 59 |
whether they are correctly encoded, and raising error unconditionally |
| 60 |
is not OK. (Considering the number of bad HTML pages on WWW.) |
| 61 |
|
| 62 |
The cases of badly encoded strings are: |
| 63 |
|
| 64 |
@unorderedList( |
| 65 |
@item("%xx" sequence ends unexpectedly at the end of the string. |
| 66 |
That is, string ends with "%" or "%x". In this case we simply |
| 67 |
keep "%" or "%x" in resulting string.) |
| 68 |
|
| 69 |
@item("xx" in "%xx" sequence is not a valid hexadecimal number. |
| 70 |
In this case we also simply keep "%xx" in resulting string.) |
| 71 |
) |
| 72 |
} |
| 73 |
function RawUriDecode(const S: string): string; |
| 74 |
|
| 75 |
{ Get protocol from given URI. |
| 76 |
|
| 77 |
This is very similar to how UriParser.ParseUri function detects the protocol, |
| 78 |
although not 100% compatible: |
| 79 |
|
| 80 |
@unorderedList( |
| 81 |
@item(We allow whitespace (including newline) before protocol name. |
| 82 |
|
| 83 |
This is useful, because some VRML/X3D files have the ECMAScript code |
| 84 |
inlined and there is sometimes whitespace before "ecmascript:" protocol.) |
| 85 |
|
| 86 |
@item(We never detect a single-letter protocol name. |
| 87 |
|
| 88 |
This is useful, because we do not use any single-letter protocol name, |
| 89 |
and it allows to detect Windows absolute filenames like |
| 90 |
@code(c:\blah.txt) as filenames. Otherwise, Windows absolute filenames |
| 91 |
could not be accepted by any of our routines that work with URLs |
| 92 |
(like the @link(Download) function), |
| 93 |
since they would be detected as URLs with unknown protocol "c". |
| 94 |
|
| 95 |
Our UriProtocol will answer that protocol is empty for @code(c:\blah.txt). |
| 96 |
Which means no protocol, so our engine will treat it as a filename. |
| 97 |
(In contrast with UriParser.ParseUri that would detect protocol called "c".) |
| 98 |
See doc/uri_filename.txt in sources for more comments about differentiating |
| 99 |
Uri and filenames in our engine.) |
| 100 |
|
| 101 |
@item(We always return lowercase protocol. This is comfortable, |
| 102 |
since you almost always calculate protocol to compare it, |
| 103 |
and protocol names are not case-sensitive, |
| 104 |
and you should always produce URLs with lowercase protocol names |
| 105 |
(see http://tools.ietf.org/html/rfc3986#section-3.1).) |
| 106 |
) |
| 107 |
} |
| 108 |
function UriProtocol(const Uri: String): string; |
| 109 |
|
| 110 |
{ Check does URI contain given Protocol. |
| 111 |
This is equivalent to checking UriProtocol(S) = Protocol, ignoring case, |
| 112 |
although may be a little faster. Given Protocol string cannot contain |
| 113 |
":" character. } |
| 114 |
function UriProtocolIs(const S: string; const Protocol: string; out Colon: Integer): boolean; |
| 115 |
|
| 116 |
{ Remove the protocol part from URI. } |
| 117 |
function UriDeleteProtocol(const S: string): string; |
| 118 |
deprecated 'use ParseUri to extract Uri.Path + Uri.Document, instead of this routine that doesn''t do decoding'; |
| 119 |
|
| 120 |
{ Is the S a valid protocol scheme. |
| 121 |
|
| 122 |
Following https://datatracker.ietf.org/doc/html/rfc3986 , |
| 123 |
protocol scheme must |
| 124 |
|
| 125 |
@unorderedList( |
| 126 |
@item(begin with an (ASCII) letter) |
| 127 |
|
| 128 |
@item(and be followed by any combination of (ASCII) letters, digits, plus ("+"), period ("."), or hyphen ("-").) |
| 129 |
) |
| 130 |
} |
| 131 |
function UriValidProtocol(const P: String): Boolean; |
| 132 |
|
| 133 |
{ Return absolute URI, given base and relative URI. |
| 134 |
|
| 135 |
Base URI must be either an absolute (with protocol) URI, or only |
| 136 |
an absolute filename (in which case we'll convert it to file:// URI under |
| 137 |
the hood, if necessary). This is usually the URI of the containing file, |
| 138 |
for example an HTML file referencing the image, processed by AbsoluteURI. |
| 139 |
|
| 140 |
Relative URI may be a relative URI or an absolute URI. |
| 141 |
In the former case it is merged with Base. |
| 142 |
In the latter case it is simply returned. |
| 143 |
|
| 144 |
If you want to support relative URIs, you want to use this routine. |
| 145 |
It treats Relative always as an URI (so it should be percent-escaped, |
| 146 |
with slashes and such). Other routines in our engine, |
| 147 |
like AbsoluteUri and @link(Download), treat strings without protocol |
| 148 |
as a filename (so it's not percent-escaped, it uses PathDelim |
| 149 |
specific to OS --- slash or backslash etc.). |
| 150 |
This routine, on the other hand, treats Relative string always as an |
| 151 |
URI (when it doesn't include protocol, it just means it's relative to Base). } |
| 152 |
function CombineUri(const Base, Relative: string): string; |
| 153 |
|
| 154 |
{ Make sure that the URI is absolute (always has a protocol). |
| 155 |
This function treats an URI without a protocol as a simple filename |
| 156 |
(absolute or relative to the current directory). |
| 157 |
This includes treating empty string as equivalent to current directory. } |
| 158 |
function AbsoluteUri(const Uri: String): string; |
| 159 |
|
| 160 |
{ Does URI contain only an absolute filename. |
| 161 |
Useful to detect unwanted paths in data files, |
| 162 |
you usually do not want to have such paths in data files, |
| 163 |
as they make it impossible to transfer the data (move/copy files) |
| 164 |
to other system/location. } |
| 165 |
function AbsoluteFileUri(const Uri: String): boolean; |
| 166 |
|
| 167 |
{ Convert URI (or filename) to a filename. |
| 168 |
|
| 169 |
This is an improved UriToFilename from UriParser. |
| 170 |
When URI is already a filename, this does a better job than UriToFilename, |
| 171 |
as it handles also Windows absolute filenames (see UriProtocol). |
| 172 |
Returns empty string in case of problems, for example when this is not |
| 173 |
a file URI. |
| 174 |
|
| 175 |
Just like UriParser.UriToFilename, this percent-decodes the parameter. |
| 176 |
For example, @code(%4d) in URI will turn into letter @code(M) in result. |
| 177 |
|
| 178 |
It also handles our castle-data: protocol.} |
| 179 |
function UriToFilenameSafe(const Uri: String): string; |
| 180 |
|
| 181 |
{ Convert filename to URI. |
| 182 |
|
| 183 |
This is a fixed version of UriParser.FilenameToUri, that correctly |
| 184 |
percent-encodes the parameter, making it truly a reverse of |
| 185 |
UriToFilenameSafe. In FPC > 2.6.2 UriParser.FilenameToUri will also |
| 186 |
do this (after Michalis' patch, see |
| 187 |
http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=revision&revision=24321 ). |
| 188 |
|
| 189 |
It also makes sure the filename is absolute (it uses ExpandFileName, |
| 190 |
so if the FileName is relative --- it will be expanded, treating it |
| 191 |
as relative to the current directory). } |
| 192 |
function FilenameToUriSafe(FileName: string): string; |
| 193 |
|
| 194 |
{ Tries change URI to use castle-data: protocol. |
| 195 |
It's used in our editor to change absolute paths to relative to castle-data |
| 196 |
directory. } |
| 197 |
function MaybeUseDataProtocol(const Url: String): String; |
| 198 |
|
| 199 |
{ Get MIME type for content of the URI @italic(without downloading the file). |
| 200 |
For local and remote files (file, http, and similar protocols) |
| 201 |
it guesses MIME type based on file extension. |
| 202 |
|
| 203 |
Using this function is not adviced if you want to properly support |
| 204 |
MIME types returned by http server for network resources. |
| 205 |
For this, you have to download the file, |
| 206 |
and look at what MIME type the http server reports. |
| 207 |
The @link(Download) function returns such proper MimeType. |
| 208 |
This function only guesses without downloading. |
| 209 |
|
| 210 |
Returns empty string if MIME type is unknown. |
| 211 |
|
| 212 |
Overloaded version returns also Gzipped to detect whether file contents |
| 213 |
are gzipped. |
| 214 |
|
| 215 |
The recognition mechanism can be enhanced by adding your own |
| 216 |
mappings to the @link(UriMimeExtensions). |
| 217 |
|
| 218 |
@groupBegin } |
| 219 |
function UriMimeType(const Uri: String): string; overload; |
| 220 |
function UriMimeType(const Uri: String; out Gzipped: boolean): string; overload; |
| 221 |
{ @groupEnd } |
| 222 |
|
| 223 |
{ Map from an extension to a MIME type, used by @link(UriMimeType). |
| 224 |
The extension should be lowercase, and includes a leading dot, like @code(.png). } |
| 225 |
function UriMimeExtensions: TStringStringMap; |
| 226 |
|
| 227 |
{ Convert URI to a nice form for display (to show in messages and such). |
| 228 |
It makes sure to nicely trim URLs that would be too long/unreadable otherwise |
| 229 |
(like "data:" URI, or multi-line URLs with inlined ECMAScript/CastleScript/shader |
| 230 |
code). |
| 231 |
|
| 232 |
When Short = @false (default), then for most "file:" and "http:" URLs, |
| 233 |
it just returns them untouched. |
| 234 |
|
| 235 |
When Short = @true, it will try to extract the last path component from |
| 236 |
URLs like "file:" and "http:", if this last component is not empty. |
| 237 |
Similar to what ExtractFileName does for filenames. |
| 238 |
It will also decode the URI (convert %xx to normal charaters). |
| 239 |
Because of the percent-decoding, |
| 240 |
it is not advised to use this on filenames with Short=true. |
| 241 |
Usually, you want to call UriCaption that makes sure that argument is URL |
| 242 |
(using AbsoluteUri) and then returns UriDisplay with Short=true. |
| 243 |
|
| 244 |
It is safe to use this on both absolute and relative URLs. |
| 245 |
It does not resolve relative URLs in any way. |
| 246 |
It also means that it returns empty string for empty URI |
| 247 |
(contrary to most other routines that convert empty string |
| 248 |
to a current directory when resolving relative URLs). } |
| 249 |
function UriDisplay(const Uri: String; const Short: boolean = false): string; |
| 250 |
|
| 251 |
{ Convert URI to a nice form for a short caption. |
| 252 |
|
| 253 |
Returns empty string for empty URI (contrary to most other routines that |
| 254 |
treat empty string like a current directory). |
| 255 |
|
| 256 |
See UriDisplay documentation for details. |
| 257 |
This calls UriDisplay with Short = @true. } |
| 258 |
function UriCaption(const Uri: String): string; |
| 259 |
|
| 260 |
{ Change extension of the URL. } |
| 261 |
function ChangeUriExt(const Url, Extension: string): string; |
| 262 |
|
| 263 |
{ Delete extension of the URL. } |
| 264 |
function DeleteUriExt(const Url: String): string; |
| 265 |
|
| 266 |
{ Extract filename (last part after slash) from URL. } |
| 267 |
function ExtractUriName(const Url: String): string; |
| 268 |
|
| 269 |
{ Extract path (everything before last part), including final slash, from URL. } |
| 270 |
function ExtractUriPath(const Url: String): string; |
| 271 |
|
| 272 |
{ Ensure URL ends with slash. |
| 273 |
|
| 274 |
For an empty URL, returns empty string (so it does not turn "" into "/"). |
| 275 |
For an URL ending with bashslash (which usually means you passed Windows |
| 276 |
path name), it removes the backslash before adding slash. |
| 277 |
|
| 278 |
This should be used instead of InclPathDelim or IncludeTrailingPathDelimiter, |
| 279 |
when you use URLs instead of filenames. } |
| 280 |
function UriIncludeSlash(const Url: String): String; |
| 281 |
|
| 282 |
{ Ensure URL does not end with slash. |
| 283 |
In case you passed Windows path name, it also removes the backslash. |
| 284 |
|
| 285 |
This should be used instead of ExclPathDelim or ExcludeTrailingPathDelimiter, |
| 286 |
when you use URLs instead of filenames. } |
| 287 |
function UriExcludeSlash(const Url: String): String; |
| 288 |
|
| 289 |
{ Does a file exist, that is: whether it makes sense to load it with |
| 290 |
the @link(Download) function. |
| 291 |
|
| 292 |
Returns @true for URLs where we cannot determine whether the file exists |
| 293 |
(like http / https). |
| 294 |
|
| 295 |
This is simply a shortcut for @code(UriExists(Url) in [ueFile, ueUnknown]). } |
| 296 |
function UriFileExists(const Url: String): Boolean; |
| 297 |
|
| 298 |
type |
| 299 |
{ Result of the @link(UriExists) query. } |
| 300 |
TUriExists = ( |
| 301 |
{ Given path does not indicate either a file or directory. } |
| 302 |
ueNotExists, |
| 303 |
|
| 304 |
{ Given path is a regular file. |
| 305 |
In particular, this means it can be read with the @link(Download) function. |
| 306 |
|
| 307 |
Note that there is no @italic(guarantee) that opening it will work. |
| 308 |
On a multi-process system the file can be always deleted between the call |
| 309 |
to UriExists and Download. |
| 310 |
And the file permissions may not allow reading. |
| 311 |
We merely say that "right now this file exists". |
| 312 |
} |
| 313 |
ueFile, |
| 314 |
|
| 315 |
{ Given path is a directory. E.g. it can be used as path for the @link(FindFiles) function. } |
| 316 |
ueDirectory, |
| 317 |
|
| 318 |
{ Detecting existence of given path is tricky, it could be time-consuming. |
| 319 |
|
| 320 |
This applies e.g. to URLs using http / https protocols. |
| 321 |
The only way to detect their existence would be to actually open them. |
| 322 |
But this involves a network request, so it may take some time, |
| 323 |
and should be done asynchronously (see @link(TCastleDownload)). |
| 324 |
|
| 325 |
If you really want to check the file existence, you can always |
| 326 |
try to open it by @link(Download): |
| 327 |
|
| 328 |
@longCode(# |
| 329 |
try |
| 330 |
Stream := Download(Url); |
| 331 |
FreeAndNil(Stream); |
| 332 |
ItExists := true; |
| 333 |
except |
| 334 |
on E: Exception do |
| 335 |
begin |
| 336 |
WritelnLog('Opening URL %s failed with exception %s', [ |
| 337 |
UriCaption(Url), |
| 338 |
ExceptMessage(E) |
| 339 |
]); |
| 340 |
ItExists := false; |
| 341 |
end; |
| 342 |
end; |
| 343 |
#) |
| 344 |
|
| 345 |
Depending on the circumstances, the "ueUnknown" can be sometimes interpreted |
| 346 |
as "it exists" and sometimes as "it doesn't exist". |
| 347 |
Opening it with @link(Download) may either fail or succeed, we cannot detect. |
| 348 |
} |
| 349 |
ueUnknown |
| 350 |
); |
| 351 |
|
| 352 |
{ Does a file or directory exist under this URL. |
| 353 |
See TUriExists for possible return values. } |
| 354 |
function UriExists(Url: String): TUriExists; |
| 355 |
|
| 356 |
{ Current working directory of the application, expressed as URL, |
| 357 |
including always final slash at the end. } |
| 358 |
function UriCurrentPath: string; |
| 359 |
|
| 360 |
{ If the given URL uses "castle-data:..." protocol, resolve it, |
| 361 |
returning a URL that does not use "castle-data:..." protocol any more. |
| 362 |
For example may resolve "castle-data:/xxx" into |
| 363 |
"file:///home/michalis/my-application/data/xxx". |
| 364 |
See https://castle-engine.io/data |
| 365 |
for documentation how our "data directory" works. |
| 366 |
|
| 367 |
If the URL has a different protocol, it is returned unchanged. } |
| 368 |
function ResolveCastleDataUrl(const Url: String): String; |
| 369 |
|
| 370 |
{ If this URL indicates something inside the @url(https://castle-engine.io/data |
| 371 |
CGE data directory) then return URL relative to this data directory. |
| 372 |
E.g. for "castle-data:/foo/bar.txt" it returns "foo/bar.txt". |
| 373 |
|
| 374 |
It accepts any URL, relative (to the current working directory) |
| 375 |
or absolute (with any protocol). |
| 376 |
It works when the URL starts with castle-data:/ protocol, |
| 377 |
it works when the URL starts with other (like file:/) protocol |
| 378 |
that still points to a file inside data. |
| 379 |
|
| 380 |
If the URL does not point to a file in data, it is returned untouched. } |
| 381 |
function RelativeToCastleDataUrl(const Url: String; out WasInsideData: Boolean): String; |
| 382 |
|
| 383 |
{ Encode string by using percent encoding (https://en.wikipedia.org/wiki/Percent-encoding) |
| 384 |
@exclude } |
| 385 |
function InternalUriEscape(const S: String): String; |
| 386 |
|
| 387 |
{ Decode string encoded by percent encoding (https://en.wikipedia.org/wiki/Percent-encoding ) |
| 388 |
@exclude } |
| 389 |
function InternalUriUnescape(const S: String): String; |
| 390 |
|
| 391 |
var |
| 392 |
{ On systems where filesystems are usually case-sensitive |
| 393 |
(Unix; like Linux, FreeBSD, macOS), |
| 394 |
accept any @code('castle-data:/xxx') URLs, even when they have different case |
| 395 |
than the actual files. |
| 396 |
|
| 397 |
This is a quick way to run applications prepared / tested on case-insensitive |
| 398 |
systems (like Windows) on Unix. It causes a small additional work when we open |
| 399 |
data files, but in many cases it is acceptable. } |
| 400 |
CastleDataIgnoreCase: Boolean = false; |
| 401 |
|
| 402 |
implementation |
| 403 |
|
| 404 |
uses UriParser, |
| 405 |
CastleUtils, CastleInternalDataUri, CastleLog, CastleFilesUtils, |
| 406 |
CastleInternalDirectoryInformation, CastleFindFiles, CastleDownload |
| 407 |
{$ifdef CASTLE_NINTENDO_SWITCH}, CastleInternalNxBase {$endif} |
| 408 |
{$ifndef FPC}, Character{$endif}; |
| 409 |
|
| 410 |
{ Escape and Unescape -------------------------------------------------------- |
| 411 |
Copied from UriParser and fixed for Delphi, as they are internal there. |
| 412 |
} |
| 413 |
|
| 414 |
function InternalUriUnescape(const S: String): String; |
| 415 |
|
| 416 |
function HexValue(C: Char): Integer; |
| 417 |
begin |
| 418 |
case C of |
| 419 |
'0'..'9': Result := Ord(C) - Ord('0'); |
| 420 |
'A'..'F': Result := Ord(C) - (Ord('A') - 10); |
| 421 |
'a'..'f': Result := Ord(C) - (Ord('a') - 10); |
| 422 |
else |
| 423 |
Result := 0; |
| 424 |
end; |
| 425 |
end; |
| 426 |
|
| 427 |
var |
| 428 |
I: Integer; |
| 429 |
{$ifdef FPC} |
| 430 |
RealLength: Integer; |
| 431 |
P: PChar; |
| 432 |
{$else} |
| 433 |
J: Integer; |
| 434 |
Utf8Char: RawByteString; |
| 435 |
Utf16Char: String; |
| 436 |
{$endif} |
| 437 |
begin |
| 438 |
I := 1; |
| 439 |
{$ifdef FPC} |
| 440 |
SetLength(Result, Length(s)); |
| 441 |
P := PChar(Result); { use PChar to prevent numerous calls to UniqueString } |
| 442 |
RealLength := 0; |
| 443 |
while i <= Length(S) do |
| 444 |
begin |
| 445 |
if S[I] = '%' then |
| 446 |
begin |
| 447 |
P[RealLength] := Chr(HexValue(S[I + 1]) shl 4 or HexValue(S[I + 2])); |
| 448 |
Inc(I, 3); |
| 449 |
end else |
| 450 |
begin |
| 451 |
P[RealLength] := S[I]; |
| 452 |
Inc(I); |
| 453 |
end; |
| 454 |
Inc(RealLength); |
| 455 |
end; |
| 456 |
SetLength(Result, RealLength); |
| 457 |
{$else} |
| 458 |
if S = '' then |
| 459 |
Exit(''); |
| 460 |
|
| 461 |
while I <= Length(S) do |
| 462 |
begin |
| 463 |
if IsSurrogate(S, I) then |
| 464 |
begin |
| 465 |
{ I don't think that should ever happen. If so, we simply add |
| 466 |
such a character. } |
| 467 |
Result := Result + S[I]; |
| 468 |
if I <> Length(S) then |
| 469 |
begin |
| 470 |
Result := Result + S[I + 1]; |
| 471 |
end; |
| 472 |
Inc(I); |
| 473 |
end else |
| 474 |
if S[I] = '%' then |
| 475 |
begin |
| 476 |
{ We need support here a case when more than one "%" block make one UTF16 |
| 477 |
character } |
| 478 |
Utf8Char := ''; |
| 479 |
J := I; |
| 480 |
while J <= Length(S) do |
| 481 |
begin |
| 482 |
Utf8Char := Utf8Char + AnsiChar(HexValue(S[J + 1]) shl 4 or HexValue(S[J + 2])); |
| 483 |
Inc(J, 3); |
| 484 |
if (J > Length(S)) or (S[J] <> '%') then |
| 485 |
Break; |
| 486 |
end; |
| 487 |
I := J - 1; { -1 becouse incrementation on the loop end } |
| 488 |
Utf16Char := UTF8ToString(Utf8Char); |
| 489 |
Result := Result + Utf16Char; |
| 490 |
end else |
| 491 |
Result := Result + S[I]; |
| 492 |
|
| 493 |
Inc(I); |
| 494 |
end; |
| 495 |
{$endif} |
| 496 |
end; |
| 497 |
|
| 498 |
function InternalUriEscapeCore(const S: String; const Allowed: TSysCharSet): String; |
| 499 |
var |
| 500 |
i, L: Integer; |
| 501 |
{$ifdef FPC} |
| 502 |
P: PChar; |
| 503 |
{$else} |
| 504 |
J: Integer; |
| 505 |
Utf16Char: String; // String here becouse somtimes UTF-16 char can be two wide chars |
| 506 |
UTF8Char: UTF8String; |
| 507 |
{$endif} |
| 508 |
begin |
| 509 |
L := Length(s); |
| 510 |
{$ifdef FPC} |
| 511 |
for i := 1 to Length(s) do |
| 512 |
if not CharInSet(s[i], Allowed) then Inc(L,2); |
| 513 |
if L = Length(s) then |
| 514 |
begin |
| 515 |
Result := s; |
| 516 |
Exit; |
| 517 |
end; |
| 518 |
|
| 519 |
SetLength(Result, L); |
| 520 |
P := @Result[1]; |
| 521 |
for i := 1 to Length(s) do |
| 522 |
begin |
| 523 |
if not CharInSet(s[i], Allowed) then |
| 524 |
begin |
| 525 |
P^ := '%'; Inc(P); |
| 526 |
StrFmt(P, '%.2x', [ord(s[i])]); Inc(P); |
| 527 |
end else |
| 528 |
P^ := s[i]; |
| 529 |
Inc(P); |
| 530 |
end; |
| 531 |
{$else} |
| 532 |
if L = 0 then |
| 533 |
Exit(''); |
| 534 |
|
| 535 |
I := 1; |
| 536 |
while I <= L do |
| 537 |
begin |
| 538 |
if IsSurrogate(S, I) then |
| 539 |
begin |
| 540 |
{ Check the string is complete (Is there the second char?) } |
| 541 |
if I < L then |
| 542 |
begin |
| 543 |
Utf16Char := S[I] + S[I + 1]; |
| 544 |
UTF8Char := UTF8String(Utf16Char); |
| 545 |
for J := 1 to Length(UTF8Char) do |
| 546 |
begin |
| 547 |
Result := Result + '%' + Format('%.2x', [ord(UTF8Char[J])]); |
| 548 |
end; |
| 549 |
end; |
| 550 |
Inc(I); |
| 551 |
end else |
| 552 |
begin |
| 553 |
if not CharInSet(s[i], Allowed) then |
| 554 |
begin |
| 555 |
// Not surrogate but also not allowed |
| 556 |
UTF8Char := UTF8String(S[I]); |
| 557 |
for J := 1 to Length(UTF8Char) do |
| 558 |
begin |
| 559 |
Result := Result + '%' + Format('%.2x', [ord(UTF8Char[J])]); |
| 560 |
end; |
| 561 |
end else |
| 562 |
begin |
| 563 |
Result := Result + S[I]; |
| 564 |
end; |
| 565 |
end; |
| 566 |
|
| 567 |
Inc(I); |
| 568 |
end; |
| 569 |
{$endif FPC} |
| 570 |
end; |
| 571 |
|
| 572 |
function InternalUriEscape(const S: String): String; |
| 573 |
const |
| 574 |
SubDelims = ['!', '$', '&', '''', '(', ')', '*', '+', ',', ';', '=']; |
| 575 |
ALPHA = ['A'..'Z', 'a'..'z']; |
| 576 |
DIGIT = ['0'..'9']; |
| 577 |
Unreserved = ALPHA + DIGIT + ['-', '.', '_', '~']; |
| 578 |
ValidPathChars = Unreserved + SubDelims + ['@', ':', '/']; |
| 579 |
begin |
| 580 |
Result := InternalUriEscapeCore(S, ValidPathChars); |
| 581 |
end; |
| 582 |
|
| 583 |
{ other routines ------------------------------------------------------------- } |
| 584 |
|
| 585 |
procedure UriGetAnchor(const Uri: String; out Anchor: string); |
| 586 |
var |
| 587 |
U: String; |
| 588 |
begin |
| 589 |
U := Uri; |
| 590 |
UriExtractAnchor(U, Anchor); |
| 591 |
end; |
| 592 |
|
| 593 |
procedure UriExtractAnchor(var Uri: String; out Anchor: string); |
| 594 |
var |
| 595 |
HashPos: Integer; |
| 596 |
begin |
| 597 |
Anchor := ''; |
| 598 |
|
| 599 |
{ Avoid extracting anchor from data URI, to avoid touching things like |
| 600 |
data:model/x3d+vrml,#X3D V3.2 utf8 |
| 601 |
... |
| 602 |
which are used to embed classic VRML/X3D content. |
| 603 |
The hash in data URI is *not* an anchor. } |
| 604 |
|
| 605 |
if TDataUri.IsDataUri(Uri) then |
| 606 |
Exit; |
| 607 |
|
| 608 |
HashPos := BackPos('#', Uri); |
| 609 |
if HashPos <> 0 then |
| 610 |
begin |
| 611 |
Anchor := SEnding(Uri, HashPos + 1); |
| 612 |
SetLength(Uri, HashPos - 1); |
| 613 |
end; |
| 614 |
end; |
| 615 |
|
| 616 |
procedure UriGetSettingsFromAnchor(const Uri: String; |
| 617 |
const SettingsFromAnchor: TStringStringMap); |
| 618 |
var |
| 619 |
UrlForDisplay: String; |
| 620 |
|
| 621 |
procedure ProcessAnchorPart(const Part: String); |
| 622 |
var |
| 623 |
Semicolon: Integer; |
| 624 |
PartName, PartValue: String; |
| 625 |
begin |
| 626 |
Semicolon := Pos(':', Part); |
| 627 |
|
| 628 |
if Semicolon = 0 then |
| 629 |
begin |
| 630 |
WritelnWarning('Empty setting (%s) in anchor of "%s"', [Part, UrlForDisplay]); |
| 631 |
SettingsFromAnchor.Add(Part, ''); |
| 632 |
end else |
| 633 |
begin |
| 634 |
PartName := Copy(Part, 1, Semicolon - 1); |
| 635 |
PartValue := SEnding(Part, Semicolon + 1); |
| 636 |
SettingsFromAnchor.Add(PartName, PartValue); |
| 637 |
end; |
| 638 |
end; |
| 639 |
|
| 640 |
var |
| 641 |
Anchor, AnchorPart: String; |
| 642 |
SeekPos: Integer; |
| 643 |
begin |
| 644 |
UrlForDisplay := UriDisplay(Uri); |
| 645 |
|
| 646 |
{ We need recognize escaped hash because GTK2 open dialog returns %23 |
| 647 |
in # position } |
| 648 |
UriGetAnchor(Uri, Anchor); |
| 649 |
SettingsFromAnchor.Clear; |
| 650 |
|
| 651 |
if Anchor = '' then |
| 652 |
Exit; |
| 653 |
|
| 654 |
SeekPos := 1; |
| 655 |
repeat |
| 656 |
AnchorPart := NextToken(Anchor, SeekPos, [',']); |
| 657 |
if AnchorPart = '' then |
| 658 |
Break; |
| 659 |
ProcessAnchorPart(AnchorPart); |
| 660 |
until false; |
| 661 |
end; |
| 662 |
|
| 663 |
function UriDeleteAnchor(const Uri: String): string; |
| 664 |
var |
| 665 |
Anchor: string; |
| 666 |
begin |
| 667 |
Result := Uri; |
| 668 |
UriExtractAnchor(Result, Anchor); |
| 669 |
end; |
| 670 |
|
| 671 |
function RawUriDecode(const S: string): string; |
| 672 |
|
| 673 |
{ Assume Position <= Length(S). |
| 674 |
Check is S[Positon] is a start of %xx sequence: |
| 675 |
- if not, exit false |
| 676 |
- if yes, but %xx is invalid, report WritelnWarning and exit false |
| 677 |
- if yes and %xx is valid, set DecodedChar and exit true } |
| 678 |
function ValidSequence(const S: string; Position: Integer; |
| 679 |
out DecodedChar: char): boolean; |
| 680 |
const |
| 681 |
ValidHexaChars = ['a'..'f', 'A'..'F', '0'..'9']; |
| 682 |
|
| 683 |
{ Assume C is valid hex digit, return it's value (in 0..15 range). } |
| 684 |
function HexDigit(const C: char): Byte; |
| 685 |
begin |
| 686 |
if CharInSet(C, ['0'..'9']) then |
| 687 |
Result := Ord(C) - Ord('0') |
| 688 |
else |
| 689 |
if CharInSet(C, ['a'..'f']) then |
| 690 |
Result := 10 + Ord(C) - Ord('a') |
| 691 |
else |
| 692 |
if CharInSet(C, ['A'..'F']) then |
| 693 |
Result := 10 + Ord(C) - Ord('A') |
| 694 |
else |
| 695 |
raise EInternalError.Create('Invalid hex character'); |
| 696 |
end; |
| 697 |
|
| 698 |
begin |
| 699 |
Result := S[Position] = '%'; |
| 700 |
if Result then |
| 701 |
begin |
| 702 |
if Position + 2 > Length(S) then |
| 703 |
begin |
| 704 |
WritelnWarning('URI', Format( |
| 705 |
'URI "%s" incorrectly encoded, %%xx sequence ends unexpectedly', [S])); |
| 706 |
Exit(false); |
| 707 |
end; |
| 708 |
|
| 709 |
if (not CharInSet(S[Position + 1], ValidHexaChars)) or |
| 710 |
(not CharInSet(S[Position + 2], ValidHexaChars)) then |
| 711 |
begin |
| 712 |
WritelnWarning('URI', Format( |
| 713 |
'URI "%s" incorrectly encoded, %s if not a valid hexadecimal number', |
| 714 |
[S, S[Position + 1] + S[Position + 2]])); |
| 715 |
Exit(false); |
| 716 |
end; |
| 717 |
|
| 718 |
DecodedChar := Chr( |
| 719 |
(HexDigit(S[Position + 1]) shl 4) or |
| 720 |
HexDigit(S[Position + 2]) |
| 721 |
); |
| 722 |
end; |
| 723 |
end; |
| 724 |
|
| 725 |
var |
| 726 |
I, ResultI: Integer; |
| 727 |
DecodedChar: char; |
| 728 |
begin |
| 729 |
{ Allocate Result string at the beginning, to save time later for |
| 730 |
memory reallocations. We can do this, since we know that final |
| 731 |
Result is shorter or equal to S. } |
| 732 |
SetLength(Result, Length(S)); |
| 733 |
|
| 734 |
ResultI := 1; |
| 735 |
I := 1; |
| 736 |
|
| 737 |
while I <= Length(S) do |
| 738 |
begin |
| 739 |
if ValidSequence(S, I, DecodedChar) then |
| 740 |
begin |
| 741 |
Result[ResultI] := DecodedChar; |
| 742 |
Inc(ResultI); |
| 743 |
Inc(I, 3); |
| 744 |
end else |
| 745 |
begin |
| 746 |
Result[ResultI] := S[I]; |
| 747 |
Inc(ResultI); |
| 748 |
Inc(I); |
| 749 |
end; |
| 750 |
end; |
| 751 |
|
| 752 |
SetLength(Result, ResultI - 1); |
| 753 |
end; |
| 754 |
|
| 755 |
const |
| 756 |
{ These constants match UriParser algorithm, which in turn follows RFC. } |
| 757 |
ALPHA = ['A'..'Z', 'a'..'z']; |
| 758 |
DIGIT = ['0'..'9']; |
| 759 |
ProtocolFirstChar = ALPHA; |
| 760 |
ProtocolChar = ALPHA + DIGIT + ['+', '-', '.']; |
| 761 |
|
| 762 |
function UriValidProtocol(const P: String): Boolean; |
| 763 |
var |
| 764 |
I: Integer; |
| 765 |
begin |
| 766 |
Result := (P <> '') and CharInSet(P[1], ProtocolFirstChar); |
| 767 |
if Result then |
| 768 |
for I := 2 to Length(P) do |
| 769 |
if not CharInSet(P[I], ProtocolChar) then |
| 770 |
Exit(false); |
| 771 |
end; |
| 772 |
|
| 773 |
{ Detect protocol delimiting positions. |
| 774 |
If returns true, then for sure: |
| 775 |
- FirstCharacter < Colon |
| 776 |
- FirstCharacter >= 1 |
| 777 |
- Colon > 1 } |
| 778 |
function UriProtocolIndex(const S: string; out FirstCharacter, Colon: Integer): boolean; |
| 779 |
var |
| 780 |
I: Integer; |
| 781 |
begin |
| 782 |
Result := false; |
| 783 |
Colon := Pos(':', S); |
| 784 |
if Colon <> 0 then |
| 785 |
begin |
| 786 |
(* Skip beginning whitespace from protocol. |
| 787 |
This allows us to detect properly "ecmascript:" protocol in VRML/X3D: |
| 788 |
Script { url " |
| 789 |
ecmascript:..." } |
| 790 |
*) |
| 791 |
FirstCharacter := 1; |
| 792 |
while (FirstCharacter < Colon) and CharInSet(S[FirstCharacter], WhiteSpaces) do |
| 793 |
Inc(FirstCharacter); |
| 794 |
if FirstCharacter >= Colon then |
| 795 |
Exit; |
| 796 |
|
| 797 |
{ Protocol name can only contain specific characters. } |
| 798 |
if not CharInSet(S[FirstCharacter], ProtocolFirstChar) then |
| 799 |
Exit; |
| 800 |
for I := FirstCharacter + 1 to Colon - 1 do |
| 801 |
if not CharInSet(S[I], ProtocolChar) then |
| 802 |
Exit; |
| 803 |
|
| 804 |
{ Do not treat drive names in Windows filenames as protocol. |
| 805 |
To allow stable testing, do this on all platforms, even non-Windows. |
| 806 |
We do not use any single-letter protocol, so no harm. } |
| 807 |
Result := not ((FirstCharacter = 1) and (Colon = 2)); |
| 808 |
end; |
| 809 |
end; |
| 810 |
|
| 811 |
function UriProtocol(const Uri: String): string; |
| 812 |
var |
| 813 |
FirstCharacter, Colon: Integer; |
| 814 |
begin |
| 815 |
if UriProtocolIndex(Uri, FirstCharacter, Colon) then |
| 816 |
Result := LowerCase(CopyPos(Uri, FirstCharacter, Colon - 1)) else |
| 817 |
Result := ''; |
| 818 |
end; |
| 819 |
|
| 820 |
function UriProtocolIs(const S: string; const Protocol: string; out Colon: Integer): boolean; |
| 821 |
var |
| 822 |
FirstCharacter, I: Integer; |
| 823 |
begin |
| 824 |
Result := false; |
| 825 |
if UriProtocolIndex(S, FirstCharacter, Colon) and |
| 826 |
(Colon - FirstCharacter = Length(Protocol)) then |
| 827 |
begin |
| 828 |
for I := 1 to Length(Protocol) do |
| 829 |
if LoCase(Protocol[I]) <> LoCase(S[I - FirstCharacter + 1]) then |
| 830 |
Exit; |
| 831 |
Result := true; |
| 832 |
end; |
| 833 |
end; |
| 834 |
|
| 835 |
function UriDeleteProtocol(const S: string): string; |
| 836 |
var |
| 837 |
FirstCharacter, Colon: Integer; |
| 838 |
begin |
| 839 |
if UriProtocolIndex(S, FirstCharacter, Colon) then |
| 840 |
{ Cut off also whitespace before FirstCharacter } |
| 841 |
Result := SEnding(S, Colon + 1) else |
| 842 |
Result := S; |
| 843 |
end; |
| 844 |
|
| 845 |
function CombineUri(const Base, Relative: string): string; |
| 846 |
// var |
| 847 |
// RelativeProtocol: string; |
| 848 |
begin |
| 849 |
{ Test for some special protocols first, that may have whitespace before |
| 850 |
the protocol name. } |
| 851 |
{ This is not necessary anymore -- check below for UriProtocol(Relative) <> '' |
| 852 |
will handle this case anyway. |
| 853 |
RelativeProtocol := UriProtocol(Relative); |
| 854 |
if (RelativeProtocol = 'ecmascript') or |
| 855 |
(RelativeProtocol = 'javascript') or |
| 856 |
(RelativeProtocol = 'castlescript') or |
| 857 |
(RelativeProtocol = 'kambiscript') or |
| 858 |
(RelativeProtocol = 'compiled') then |
| 859 |
Exit(Relative); |
| 860 |
} |
| 861 |
|
| 862 |
{ When Base is like 'castle-data:/CastleSettings.xml' |
| 863 |
and Relative is like '../gfx/font.ttf', |
| 864 |
you need to resolve the Base to use the file:/ protocol. } |
| 865 |
if (UriProtocol(Base) = 'castle-data') and IsPrefix('../', Relative) then |
| 866 |
Exit(CombineUri(ResolveCastleDataUrl(Base), Relative)); |
| 867 |
|
| 868 |
{ Relative is already an absolute Url, no point in doing anything, |
| 869 |
in particular no point for doing AbsoluteUri(Base) below, |
| 870 |
which could fail on NX in case Base='', calling ExpandFileName |
| 871 |
is not allowed on NX. } |
| 872 |
if UriProtocol(Relative) <> '' then |
| 873 |
Exit(Relative); |
| 874 |
|
| 875 |
try |
| 876 |
if not ResolveRelativeUri(AbsoluteUri(Base), Relative, Result) then |
| 877 |
begin |
| 878 |
{ The only case when ResolveRelativeUri may fail is when neither argument |
| 879 |
contains a protocol. But we just used AbsoluteUri, which makes sure |
| 880 |
that AbsoluteUri(Base) has some protocol. } |
| 881 |
raise EInternalError.CreateFmt('Failed to resolve relative URI "%s" with base "%s"', |
| 882 |
[Relative, Base]); |
| 883 |
end; |
| 884 |
except |
| 885 |
{ workaround http://bugs.freepascal.org/view.php?id=28496 , see also |
| 886 |
https://sourceforge.net/p/castle-engine/tickets/35/ } |
| 887 |
on E: EConvertError do |
| 888 |
begin |
| 889 |
WritelnWarning('URL', Format('Error when parsing URL. This usually indicates an incorrect Windows "file:" URL (it should have *three* slashes, like "file:///c:/blah..."): "%s"', |
| 890 |
[Relative])); |
| 891 |
Result := Relative; |
| 892 |
end; |
| 893 |
end; |
| 894 |
end; |
| 895 |
|
| 896 |
function AbsoluteUri(const Uri: String): string; |
| 897 |
begin |
| 898 |
if UriProtocol(Uri) = '' then |
| 899 |
Result := FilenameToUriSafe(Uri) else |
| 900 |
Result := Uri; |
| 901 |
end; |
| 902 |
|
| 903 |
function AbsoluteFileUri(const Uri: String): boolean; |
| 904 |
begin |
| 905 |
Result := (UriProtocol(Uri) = '') and IsPathAbsoluteOnDrive(Uri); |
| 906 |
end; |
| 907 |
|
| 908 |
function UriToFilenameSafe(const Uri: String): string; |
| 909 |
var |
| 910 |
P, CastleDataResolved: string; |
| 911 |
begin |
| 912 |
{ Use our UriProtocol instead of depending that UriToFilename will detect |
| 913 |
empty protocol case correctly. This allows to handle Windows absolute |
| 914 |
filenames like "c:\foo" as filenames. } |
| 915 |
P := UriProtocol(Uri); |
| 916 |
if P = '' then |
| 917 |
Result := Uri |
| 918 |
else |
| 919 |
if P = 'file' then |
| 920 |
begin |
| 921 |
try |
| 922 |
if not UriToFilename(Uri, Result) then Result := ''; |
| 923 |
except |
| 924 |
{ workaround http://bugs.freepascal.org/view.php?id=28496 , see also |
| 925 |
https://sourceforge.net/p/castle-engine/tickets/35/ } |
| 926 |
on E: EConvertError do |
| 927 |
begin |
| 928 |
WritelnWarning('URL', Format('Error when parsing URL. This usually indicates an incorrect Windows "file:" URL (it should have *three* slashes, like "file:///c:/blah..."): "%s"', |
| 929 |
[Uri])); |
| 930 |
Result := ''; |
| 931 |
end; |
| 932 |
end; |
| 933 |
end else |
| 934 |
if P = 'castle-data' then |
| 935 |
begin |
| 936 |
CastleDataResolved := ResolveCastleDataUrl(Uri); |
| 937 |
if UriProtocol(CastleDataResolved) = 'castle-data' then |
| 938 |
raise EInternalError.CreateFmt('ResolveCastleDataUrl cannot return URL with castle-data protocol. This probably indicates that ApplicationDataOverride (%s) contains castle-data protocol, which it should not.', [ |
| 939 |
ApplicationDataOverride |
| 940 |
]); |
| 941 |
Result := UriToFilenameSafe(CastleDataResolved); |
| 942 |
end else |
| 943 |
Result := ''; |
| 944 |
end; |
| 945 |
|
| 946 |
function FilenameToUriSafe(FileName: string): string; |
| 947 |
|
| 948 |
{ Code adjusted from FPC FilenameToUri (same license as our engine, |
| 949 |
so it's Ok to share code). Adjusted to call Escape on FileName. |
| 950 |
See http://bugs.freepascal.org/view.php?id=24324 : FPC FilenameToUri |
| 951 |
should be fixed in the future to follow this. |
| 952 |
|
| 953 |
We also make sure to call ExpandFileName, |
| 954 |
and so we don't need checks for IsAbsFilename. } |
| 955 |
|
| 956 |
{ Like ExpandFileName, but |
| 957 |
- guarantees that if input ends with directory separator, |
| 958 |
then output will end with it too. |
| 959 |
This is necessary on Delphi+Posix. |
| 960 |
- for S = '', outputs current dir. } |
| 961 |
function ExpandFileNameFixed(const S: String): String; |
| 962 |
begin |
| 963 |
if S = '' then |
| 964 |
Exit(InclPathDelim(GetCurrentDir)); |
| 965 |
|
| 966 |
Result := ExpandFileName(S); |
| 967 |
{$ifndef FPC} |
| 968 |
if (S <> '') and |
| 969 |
CharInSet(S[Length(S)], AllowDirectorySeparators) and |
| 970 |
( (Result = '') or |
| 971 |
(not CharInSet(Result[Length(Result)], AllowDirectorySeparators) ) ) then |
| 972 |
Result := InclPathDelim(Result); |
| 973 |
{$endif} |
| 974 |
end; |
| 975 |
|
| 976 |
var |
| 977 |
I: Integer; |
| 978 |
FilenamePart: String; |
| 979 |
begin |
| 980 |
FileName := ExpandFileNameFixed(FileName); |
| 981 |
|
| 982 |
Result := 'file:'; |
| 983 |
|
| 984 |
Assert(Filename <> ''); |
| 985 |
if Filename[1] <> PathDelim then |
| 986 |
Result := Result + '///' |
| 987 |
else |
| 988 |
Result := Result + '//'; |
| 989 |
|
| 990 |
FilenamePart := Filename; |
| 991 |
{ unreachable code warning is ok here } |
| 992 |
{$warnings off} |
| 993 |
if PathDelim <> '/' then |
| 994 |
begin |
| 995 |
I := Pos(PathDelim, FilenamePart); |
| 996 |
while I <> 0 do |
| 997 |
begin |
| 998 |
FilenamePart[I] := '/'; |
| 999 |
I := Pos(PathDelim, FilenamePart); |
| 1000 |
end; |
| 1001 |
end; |
| 1002 |
{$warnings on} |
| 1003 |
FilenamePart := InternalUriEscape(FilenamePart); |
| 1004 |
|
| 1005 |
Result := Result + FilenamePart; |
| 1006 |
end; |
| 1007 |
|
| 1008 |
var |
| 1009 |
FUriMimeExtensions: TStringStringMap; |
| 1010 |
|
| 1011 |
function UriMimeExtensions: TStringStringMap; |
| 1012 |
begin |
| 1013 |
if FUriMimeExtensions = nil then |
| 1014 |
FUriMimeExtensions := TStringStringMap.Create; |
| 1015 |
Result := FUriMimeExtensions; |
| 1016 |
end; |
| 1017 |
|
| 1018 |
function UriMimeType(const Uri: String; out Gzipped: boolean): string; |
| 1019 |
begin |
| 1020 |
Result := InternalUriMimeType(Uri, Gzipped); |
| 1021 |
end; |
| 1022 |
|
| 1023 |
function UriMimeType(const Uri: String): string; |
| 1024 |
var |
| 1025 |
Gzipped: boolean; |
| 1026 |
begin |
| 1027 |
Result := UriMimeType(Uri, Gzipped); |
| 1028 |
end; |
| 1029 |
|
| 1030 |
function MaybeUseDataProtocol(const Url: String): String; |
| 1031 |
var |
| 1032 |
DataPath: String; |
| 1033 |
begin |
| 1034 |
{ Use below ResolveCastleDataUrl, to get real location of data, |
| 1035 |
e.g. resolved to file:// on normal desktop. } |
| 1036 |
DataPath := ResolveCastleDataUrl('castle-data:/'); |
| 1037 |
if IsPrefix(DataPath, Url, not FileNameCaseSensitive) then |
| 1038 |
Result := 'castle-data:/' + PrefixRemove(DataPath, Url, not FileNameCaseSensitive) |
| 1039 |
else |
| 1040 |
Result := Url; |
| 1041 |
end; |
| 1042 |
|
| 1043 |
function UriDisplay(const Uri: String; const Short: boolean): string; |
| 1044 |
var |
| 1045 |
DataUri: TDataUri; |
| 1046 |
NewLinePos: Integer; |
| 1047 |
Parsed: TUri; |
| 1048 |
begin |
| 1049 |
Result := Trim(Uri); |
| 1050 |
|
| 1051 |
if TDataUri.IsDataUri(Uri) then |
| 1052 |
begin |
| 1053 |
DataUri := TDataUri.Create; |
| 1054 |
try |
| 1055 |
DataUri.Uri := Uri; |
| 1056 |
if DataUri.Valid then Result := DataUri.UriPrefix + ',...'; |
| 1057 |
finally FreeAndNil(DataUri) end; |
| 1058 |
end else |
| 1059 |
|
| 1060 |
begin |
| 1061 |
NewLinePos := CharsPos([#10, #13], Result); |
| 1062 |
if NewLinePos <> 0 then |
| 1063 |
begin |
| 1064 |
{ we have done Trim(Uri) to prevent starting from newline } |
| 1065 |
Assert(NewLinePos <> 1); |
| 1066 |
Result := Copy(Result, 1, NewLinePos - 1) + '...'; |
| 1067 |
end else |
| 1068 |
if Short then |
| 1069 |
begin |
| 1070 |
{ try to extract last path component } |
| 1071 |
try |
| 1072 |
Parsed := ParseUri(Uri); |
| 1073 |
except |
| 1074 |
on E: EConvertError do |
| 1075 |
begin |
| 1076 |
WritelnWarning('URL', Format('Error when parsing Uri. This usually indicates an incorrect Windows "file:" URL (it should have *three* slashes, like "file:///c:/blah..."): "%s"', |
| 1077 |
[Uri])); |
| 1078 |
Parsed.Document := ExtractUriName(Uri); |
| 1079 |
end; |
| 1080 |
end; |
| 1081 |
|
| 1082 |
Parsed.Document := Trim(Parsed.Document); |
| 1083 |
if Parsed.Document <> '' then |
| 1084 |
Result := Parsed.Document; |
| 1085 |
end; |
| 1086 |
end; |
| 1087 |
end; |
| 1088 |
|
| 1089 |
function UriCaption(const Uri: String): string; |
| 1090 |
begin |
| 1091 |
if Uri = '' then |
| 1092 |
Result := '' |
| 1093 |
else |
| 1094 |
Result := UriDisplay(AbsoluteUri(Uri), true); |
| 1095 |
end; |
| 1096 |
|
| 1097 |
function ChangeUriExt(const Url, Extension: string): string; |
| 1098 |
|
| 1099 |
{$ifndef FPC} |
| 1100 |
{ Mask default Delphi ChangeFileExt that behaves badly for filenames |
| 1101 |
like '.hidden'.} |
| 1102 |
function ChangeFileExt(const FileName, NewExtension: String): String; |
| 1103 |
var |
| 1104 |
I: Integer; |
| 1105 |
ExtDotPos: Integer; |
| 1106 |
begin |
| 1107 |
ExtDotPos := 0; |
| 1108 |
for I := Length(FileName) downto 1 do |
| 1109 |
if FileName[I] in AllowDirectorySeparators then |
| 1110 |
begin |
| 1111 |
// no extension, leave ExtDotPos = 0 |
| 1112 |
Break; |
| 1113 |
end else |
| 1114 |
if (FileName[I] = '.') and |
| 1115 |
(I > 1) and |
| 1116 |
(not (FileName[I - 1] in AllowDirectorySeparators)) then |
| 1117 |
begin |
| 1118 |
// dot, but not at the beginning of the name -> valid ExtDotPos |
| 1119 |
ExtDotPos := I; |
| 1120 |
Break; |
| 1121 |
end; |
| 1122 |
|
| 1123 |
if ExtDotPos <> 0 then |
| 1124 |
Result := Copy(FileName, 1, ExtDotPos - 1) + NewExtension |
| 1125 |
else |
| 1126 |
Result := FileName + NewExtension; |
| 1127 |
end; |
| 1128 |
{$endif} |
| 1129 |
|
| 1130 |
var |
| 1131 |
UrlWithoutAnchor, Anchor: String; |
| 1132 |
begin |
| 1133 |
UrlWithoutAnchor := Url; |
| 1134 |
UriExtractAnchor(UrlWithoutAnchor, Anchor); |
| 1135 |
Result := ChangeFileExt(UrlWithoutAnchor, Extension); |
| 1136 |
if Anchor <> '' then |
| 1137 |
Result := Result + '#' + Anchor; |
| 1138 |
end; |
| 1139 |
|
| 1140 |
function DeleteUriExt(const Url: String): string; |
| 1141 |
begin |
| 1142 |
Result := ChangeUriExt(Url, ''); |
| 1143 |
end; |
| 1144 |
|
| 1145 |
function ExtractUriName(const Url: String): string; |
| 1146 |
var |
| 1147 |
UrlWithoutAnchor: String; |
| 1148 |
{$ifndef FPC} I: Integer; {$endif} |
| 1149 |
begin |
| 1150 |
UrlWithoutAnchor := UriDeleteAnchor(Url); |
| 1151 |
{$ifdef FPC} |
| 1152 |
Result := ExtractFileName(UrlWithoutAnchor); |
| 1153 |
{$else} |
| 1154 |
{ In Delphi, / separator in paths is not recognized, so we cannot use ExtractFilePath. |
| 1155 |
TODO: our own solution should be just used for both compilers. |
| 1156 |
Need autotests to confirm it behaves the same, on both platforms. } |
| 1157 |
|
| 1158 |
I := BackCharsPos(['/'], UrlWithoutAnchor); |
| 1159 |
if I <> 0 then |
| 1160 |
Result := SEnding(UrlWithoutAnchor, I + 1) |
| 1161 |
else |
| 1162 |
Result := UrlWithoutAnchor; |
| 1163 |
{$endif} |
| 1164 |
end; |
| 1165 |
|
| 1166 |
function ExtractUriPath(const Url: String): string; |
| 1167 |
var |
| 1168 |
UrlWithoutAnchor: String; |
| 1169 |
{$ifndef FPC} I: Integer; {$endif} |
| 1170 |
begin |
| 1171 |
{ While on non-Windows ExtractFilePath would work on full URL as well, |
| 1172 |
but on Windows the ":" inside anchor (like |
| 1173 |
"castle-data:/starling/character_zombie_atlas.starling-xml#fps:8,anim-naming:strict-underscore") |
| 1174 |
would cause trouble: it would be considered a drive letter separator, |
| 1175 |
and change the result. } |
| 1176 |
UrlWithoutAnchor := UriDeleteAnchor(Url); |
| 1177 |
{$ifdef FPC} |
| 1178 |
Result := ExtractFilePath(UrlWithoutAnchor); |
| 1179 |
{$else} |
| 1180 |
{ In Delphi, / separator in paths is not recognized, so we cannot use ExtractFilePath. |
| 1181 |
TODO: our own solution should be just used for both compilers. |
| 1182 |
Need autotests to confirm it behaves the same, on both platforms. } |
| 1183 |
|
| 1184 |
I := BackCharsPos(['/'], UrlWithoutAnchor); |
| 1185 |
if I <> 0 then |
| 1186 |
Result := Copy(UrlWithoutAnchor, 1, I) |
| 1187 |
else |
| 1188 |
Result := UrlWithoutAnchor; |
| 1189 |
{$endif} |
| 1190 |
end; |
| 1191 |
|
| 1192 |
function UriIncludeSlash(const Url: String): String; |
| 1193 |
var |
| 1194 |
L: Integer; |
| 1195 |
begin |
| 1196 |
if Url = '' then |
| 1197 |
Exit(''); |
| 1198 |
|
| 1199 |
L := Length(Url); |
| 1200 |
case Url[L] of |
| 1201 |
'/': Result := Url; // nothing needs to be done |
| 1202 |
'\': Result := Copy(Url, 1, L - 1) + '/'; |
| 1203 |
else Result := Url + '/'; |
| 1204 |
end; |
| 1205 |
end; |
| 1206 |
|
| 1207 |
function UriExcludeSlash(const Url: String): String; |
| 1208 |
var |
| 1209 |
L: Integer; |
| 1210 |
begin |
| 1211 |
L := Length(Url); |
| 1212 |
if (L <> 0) and CharInSet(Url[L], ['/', '\']) then |
| 1213 |
Result := Copy(Url, 1, L - 1) |
| 1214 |
else |
| 1215 |
Result := Url; |
| 1216 |
end; |
| 1217 |
|
| 1218 |
function UriFileExists(const Url: String): Boolean; |
| 1219 |
begin |
| 1220 |
Result := UriExists(Url) in [ueFile, ueUnknown]; |
| 1221 |
end; |
| 1222 |
|
| 1223 |
function UriExists(Url: String): TUriExists; |
| 1224 |
|
| 1225 |
// Detect existence of castle-data:/xxx URL using DataDirectoryInformation. |
| 1226 |
function UseDataDirectoryInformation(const Url: String): TUriExists; |
| 1227 |
var |
| 1228 |
U: TUri; |
| 1229 |
UrlPath: String; |
| 1230 |
PathEntry: TDirectoryInformation.TEntry; |
| 1231 |
begin |
| 1232 |
U := ParseUri(Url); |
| 1233 |
UrlPath := PrefixRemove('/', U.Path + U.Document, false); |
| 1234 |
PathEntry := DataDirectoryInformation.FindEntry(UrlPath); |
| 1235 |
if PathEntry = nil then |
| 1236 |
Exit(ueNotExists) |
| 1237 |
else |
| 1238 |
if PathEntry is TDirectoryInformation.TDirectory then |
| 1239 |
Exit(ueDirectory) |
| 1240 |
else |
| 1241 |
Exit(ueFile); |
| 1242 |
end; |
| 1243 |
|
| 1244 |
{$ifdef CASTLE_NINTENDO_SWITCH} |
| 1245 |
// Detect existence of castle-nx-contents or castle-nx-save URL using NX-specific function. |
| 1246 |
function UseNXExists(const Url: String): TUriExists; |
| 1247 |
begin |
| 1248 |
Result := NXFileExists(Url); |
| 1249 |
end; |
| 1250 |
{$endif CASTLE_NINTENDO_SWITCH} |
| 1251 |
|
| 1252 |
// Detect existence of a filename using FileExists, DirectoryExists. |
| 1253 |
function UseFileDirectoryExists(const FileName: String): TUriExists; |
| 1254 |
var |
| 1255 |
F, D: Boolean; |
| 1256 |
begin |
| 1257 |
F := FileExists(FileName); |
| 1258 |
D := DirectoryExists(FileName); |
| 1259 |
|
| 1260 |
{ FileExists behaves inconsistently for directories. |
| 1261 |
On non-Windows, returns true. |
| 1262 |
On Windows, returns false. |
| 1263 |
See http://www.freepascal.org/docs-html/rtl/sysutils/fileexists.html |
| 1264 |
http://free-pascal-general.1045716.n5.nabble.com/FileExists-inconsistency-td2813433.html |
| 1265 |
So check both, and if DirectoryExists then assume it's a directory |
| 1266 |
(regardless of FileExists result). } |
| 1267 |
if D then |
| 1268 |
Exit(ueDirectory) |
| 1269 |
else |
| 1270 |
if F then |
| 1271 |
Exit(ueFile) |
| 1272 |
else |
| 1273 |
Exit(ueNotExists); |
| 1274 |
end; |
| 1275 |
|
| 1276 |
var |
| 1277 |
P: String; |
| 1278 |
begin |
| 1279 |
{ data: URI is like a file, since you can call Download() on it } |
| 1280 |
if TDataUri.IsDataUri(Url) then |
| 1281 |
Exit(ueFile); |
| 1282 |
|
| 1283 |
P := UriProtocol(Url); |
| 1284 |
|
| 1285 |
if (P = 'castle-data') and |
| 1286 |
(DisableDataDirectoryInformation = 0) and |
| 1287 |
(DataDirectoryInformation <> nil) then |
| 1288 |
Exit(UseDataDirectoryInformation(Url)); |
| 1289 |
|
| 1290 |
{ Resolve castle-data:/xxx now. |
| 1291 |
This way we can work in case we have castle-data:/xxx URL that resolves |
| 1292 |
to something handled below (like file:/xxx) but wasn't handled above |
| 1293 |
(e.g. because DataDirectoryInformation = nil). } |
| 1294 |
Url := ResolveCastleDataUrl(Url); |
| 1295 |
P := UriProtocol(Url); |
| 1296 |
|
| 1297 |
{$ifdef CASTLE_NINTENDO_SWITCH} |
| 1298 |
if (P = 'castle-nx-contents') or |
| 1299 |
(P = 'castle-nx-save') then |
| 1300 |
Exit(UseNXExists(Url)); |
| 1301 |
{$endif CASTLE_NINTENDO_SWITCH} |
| 1302 |
|
| 1303 |
if (P = '') or (P = 'file') then |
| 1304 |
Exit(UseFileDirectoryExists(UriToFilenameSafe(Url))); |
| 1305 |
|
| 1306 |
Result := ueUnknown; |
| 1307 |
end; |
| 1308 |
|
| 1309 |
function UriCurrentPath: string; |
| 1310 |
begin |
| 1311 |
Result := FilenameToUriSafe(InclPathDelim(GetCurrentDir)); |
| 1312 |
end; |
| 1313 |
|
| 1314 |
type |
| 1315 |
TFixCaseHandler = class |
| 1316 |
SearchName: String; |
| 1317 |
FoundName: String; |
| 1318 |
end; |
| 1319 |
|
| 1320 |
procedure FixCaseCallback(const FileInfo: TFileInfo; Data: Pointer; var StopSearch: boolean); |
| 1321 |
var |
| 1322 |
H: TFixCaseHandler; |
| 1323 |
begin |
| 1324 |
H := TFixCaseHandler(Data); |
| 1325 |
if AnsiSameText(FileInfo.Name, H.SearchName) then |
| 1326 |
begin |
| 1327 |
if H.FoundName <> '' then |
| 1328 |
raise Exception.CreateFmt('Ambiguous files found in directory "%s": "%s" and "%s" (and CastleDataIgnoreCase was used)', [ |
| 1329 |
ExtractUriPath(FileInfo.Url), |
| 1330 |
FileInfo.Name, |
| 1331 |
H.FoundName |
| 1332 |
]); |
| 1333 |
H.FoundName := FileInfo.Name; |
| 1334 |
end; |
| 1335 |
end; |
| 1336 |
|
| 1337 |
{ If Path ends with <platform>/<config>/, |
| 1338 |
return @true and put in StrippedPath the Path with the last 2 path components |
| 1339 |
removed. |
| 1340 |
Otherwise return @false. |
| 1341 |
|
| 1342 |
Path must end with path delimiter. |
| 1343 |
|
| 1344 |
When returns @true, StrippedPath is also guaranteed to end with path delimiter. |
| 1345 |
} |
| 1346 |
function StripExePathFromPlatformConfig(const Path: String; |
| 1347 |
out StrippedPath: String): Boolean; |
| 1348 |
|
| 1349 |
{ Platform name, like 'Win32', as used by Delphi in $(Platform) subdirectory |
| 1350 |
name, corresponding to this process OS / CPU. |
| 1351 |
Always lowercase. |
| 1352 |
|
| 1353 |
This is made to also compile and work with FPC, for testing, |
| 1354 |
so it doesn't use TOSVersion. } |
| 1355 |
function DelphiPlatformName: String; |
| 1356 |
begin |
| 1357 |
Result := |
| 1358 |
{$if defined(MSWINDOWS)} 'win' |
| 1359 |
{$elseif defined(LINUX)} 'linux' |
| 1360 |
{$elseif defined(DARWIN)} 'macos' // TODO: not yet confirmed by testing, just guessing |
| 1361 |
{$elseif defined(ANDROID)} 'android' // TODO: not yet confirmed by testing, just guessing |
| 1362 |
{$elseif defined(IOS)} 'ios' // TODO: not yet confirmed by testing, just guessing |
| 1363 |
{$else} '' |
| 1364 |
{$endif}; |
| 1365 |
|
| 1366 |
Result := Result + |
| 1367 |
{$if defined(CPU32)} '32' |
| 1368 |
{$elseif defined(CPU64)} '64' |
| 1369 |
{$else} '' |
| 1370 |
{$endif}; |
| 1371 |
end; |
| 1372 |
|
| 1373 |
var |
| 1374 |
Dir: String; |
| 1375 |
ParentName: String; |
| 1376 |
begin |
| 1377 |
Result := false; |
| 1378 |
|
| 1379 |
Dir := ExclPathDelim(Path); |
| 1380 |
// LowerCase, to detect <config> case-insensitively |
| 1381 |
ParentName := LowerCase(ExtractFileName(Dir)); |
| 1382 |
if (ParentName = 'debug') or |
| 1383 |
(ParentName = 'release') then |
| 1384 |
begin |
| 1385 |
Dir := ExtractFileDir(Dir); |
| 1386 |
// LowerCase, to detect <platform> case-insensitively |
| 1387 |
ParentName := LowerCase(ExtractFileName(Dir)); |
| 1388 |
if ParentName = DelphiPlatformName then |
| 1389 |
begin |
| 1390 |
StrippedPath := ExtractFilePath(Dir); |
| 1391 |
Result := true; |
| 1392 |
end; |
| 1393 |
end; |
| 1394 |
end; |
| 1395 |
|
| 1396 |
var |
| 1397 |
ApplicationDataIsCache: Boolean = false; |
| 1398 |
ApplicationDataCache: string; |
| 1399 |
|
| 1400 |
function ApplicationDataCore(const Path: string): string; |
| 1401 |
|
| 1402 |
{$ifndef ANDROID} |
| 1403 |
function GetApplicationDataPath: string; |
| 1404 |
{$ifdef MSWINDOWS} |
| 1405 |
var |
| 1406 |
ExePath, StrippedExePath: string; |
| 1407 |
begin |
| 1408 |
{$warnings off} |
| 1409 |
// knowingly using deprecated; ExeName should be undeprecated but internal one day |
| 1410 |
ExePath := ExtractFilePath(ExeName); |
| 1411 |
{$warnings on} |
| 1412 |
|
| 1413 |
Result := ExePath + 'data' + PathDelim; |
| 1414 |
if DirectoryExists(Result) then Exit; |
| 1415 |
|
| 1416 |
if StripExePathFromPlatformConfig(ExePath, StrippedExePath) then |
| 1417 |
begin |
| 1418 |
Result := StrippedExePath + 'data' + PathDelim; |
| 1419 |
if DirectoryExists(Result) then Exit; |
| 1420 |
end; |
| 1421 |
|
| 1422 |
Result := ExePath; |
| 1423 |
{$endif MSWINDOWS} |
| 1424 |
{$ifdef UNIX} |
| 1425 |
var |
| 1426 |
CurPath: string; |
| 1427 |
begin |
| 1428 |
{$ifdef DARWIN} |
| 1429 |
if BundlePath <> '' then |
| 1430 |
begin |
| 1431 |
{$ifdef CASTLE_IOS} |
| 1432 |
Result := BundlePath + 'data/'; |
| 1433 |
{$else} |
| 1434 |
Result := BundlePath + 'Contents/Resources/data/'; |
| 1435 |
{$endif} |
| 1436 |
if DirectoryExists(Result) then Exit; |
| 1437 |
|
| 1438 |
{$ifndef IOS} |
| 1439 |
Result := BundlePath + '../data/'; |
| 1440 |
if DirectoryExists(Result) then |
| 1441 |
begin |
| 1442 |
WritelnLog('"Contents/Resources/data/" subdirectory not found inside the macOS application bundle: ' + BundlePath + NL + |
| 1443 |
' Using instead "data/" directory that is sibling to the application bundle.' + NL + |
| 1444 |
' This makes sense only for debug.' + NL + |
| 1445 |
' The released application version should instead include the data inside the bundle.'); |
| 1446 |
Exit; |
| 1447 |
end; |
| 1448 |
{$endif} |
| 1449 |
end; |
| 1450 |
{$endif DARWIN} |
| 1451 |
|
| 1452 |
Result := HomePath + '.local/share/' + ApplicationName + '/'; |
| 1453 |
if DirectoryExists(Result) then Exit; |
| 1454 |
|
| 1455 |
Result := '/usr/local/share/' + ApplicationName + '/'; |
| 1456 |
if DirectoryExists(Result) then Exit; |
| 1457 |
|
| 1458 |
Result := '/usr/share/' + ApplicationName + '/'; |
| 1459 |
if DirectoryExists(Result) then Exit; |
| 1460 |
|
| 1461 |
CurPath := InclPathDelim(GetCurrentDir); |
| 1462 |
|
| 1463 |
Result := CurPath + 'data/'; |
| 1464 |
if DirectoryExists(Result) then Exit; |
| 1465 |
|
| 1466 |
Result := CurPath; |
| 1467 |
{$endif UNIX} |
| 1468 |
end; |
| 1469 |
{$endif not ANDROID} |
| 1470 |
|
| 1471 |
begin |
| 1472 |
if ApplicationDataOverride <> '' then |
| 1473 |
Exit(ApplicationDataOverride + Path); |
| 1474 |
|
| 1475 |
if Pos('\', Path) <> 0 then |
| 1476 |
WritelnWarning('ResolveCastleDataUrl', 'Do not use backslashes (or a PathDelim constant) in the ApplicationDataCore parameter. The ApplicationDataCore parameter should be a relative URL, with components separated by slash ("/"), regardless of the OS. Path given was: ' + Path); |
| 1477 |
|
| 1478 |
{ Cache directory returned by ApplicationDataCore. This has two reasons: |
| 1479 |
1. On Unix GetApplicationDataPath makes three DirectoryExists calls, |
| 1480 |
so it's not too fast, avoid calling it often. |
| 1481 |
2. It would be strange if ApplicationDataCore results |
| 1482 |
suddenly changed in the middle of the program (e.g. because user just |
| 1483 |
made appropriate symlink or such). |
| 1484 |
The only case where we allow it is by ApplicationDataOverride. } |
| 1485 |
|
| 1486 |
if not ApplicationDataIsCache then |
| 1487 |
begin |
| 1488 |
ApplicationDataCache := |
| 1489 |
{$if defined(CASTLE_NINTENDO_SWITCH)} |
| 1490 |
'castle-nx-contents:/' |
| 1491 |
{$elseif defined(ANDROID)} |
| 1492 |
'castle-android-assets:/' |
| 1493 |
{$else} |
| 1494 |
FilenameToUriSafe(GetApplicationDataPath) |
| 1495 |
{$endif} |
| 1496 |
; |
| 1497 |
WritelnLog('Path', Format('Program data path detected as "%s"', [ApplicationDataCache])); |
| 1498 |
ApplicationDataIsCache := true; |
| 1499 |
end; |
| 1500 |
|
| 1501 |
Result := ApplicationDataCache + Path; |
| 1502 |
end; |
| 1503 |
|
| 1504 |
function ResolveCastleDataUrl(const Url: String): String; |
| 1505 |
|
| 1506 |
{ Fix case for the Url relative to data. } |
| 1507 |
function FixCase(const RelativeToData: String): String; |
| 1508 |
var |
| 1509 |
Parts: TCastleStringList; |
| 1510 |
H: TFixCaseHandler; |
| 1511 |
ParentUrl: String; |
| 1512 |
I: Integer; |
| 1513 |
begin |
| 1514 |
ParentUrl := UriIncludeSlash(ApplicationDataCore('')); |
| 1515 |
Result := ''; |
| 1516 |
|
| 1517 |
H := TFixCaseHandler.Create; |
| 1518 |
try |
| 1519 |
Parts := SplitString(InternalUriUnescape(RelativeToData), '/'); |
| 1520 |
try |
| 1521 |
for I := 0 to Parts.Count - 1 do |
| 1522 |
begin |
| 1523 |
H.SearchName := Parts[I]; |
| 1524 |
H.FoundName := ''; |
| 1525 |
FindFiles(ParentUrl, '*', { assume that even final part may be directory } true, @FixCaseCallback, H, []); |
| 1526 |
if H.FoundName = '' then |
| 1527 |
raise Exception.CreateFmt('Cannot find file "%s" in directory "%s" (even with case-insensitive search, because CastleDataIgnoreCase was used)', [ |
| 1528 |
H.SearchName, |
| 1529 |
ParentUrl |
| 1530 |
]); |
| 1531 |
ParentUrl := SAppendPart(ParentUrl, '/', H.FoundName); |
| 1532 |
Result := SAppendPart(Result, '/', H.FoundName); |
| 1533 |
end; |
| 1534 |
finally FreeAndNil(Parts) end; |
| 1535 |
finally FreeAndNil(H) end; |
| 1536 |
|
| 1537 |
// if RelativeToData ends with slash, also make Result end with slash |
| 1538 |
if IsSuffix('/', RelativeToData) then |
| 1539 |
Result := UriIncludeSlash(Result); |
| 1540 |
end; |
| 1541 |
|
| 1542 |
var |
| 1543 |
U: TUri; |
| 1544 |
RelativeToData: String; |
| 1545 |
begin |
| 1546 |
if UriProtocol(Url) = 'castle-data' then |
| 1547 |
begin |
| 1548 |
U := ParseUri(Url); |
| 1549 |
RelativeToData := PrefixRemove('/', U.Path + U.Document, false); |
| 1550 |
{$warnings off} // don't warn that CastleDataIgnoreCase is experimental |
| 1551 |
if CastleDataIgnoreCase and FileNameCaseSensitive then |
| 1552 |
{$warnings on} |
| 1553 |
RelativeToData := FixCase(RelativeToData); |
| 1554 |
Result := ApplicationDataCore(RelativeToData); |
| 1555 |
end else |
| 1556 |
Result := Url; |
| 1557 |
end; |
| 1558 |
|
| 1559 |
function RelativeToCastleDataUrl(const Url: String; out WasInsideData: Boolean): String; |
| 1560 |
var |
| 1561 |
FullUrl, DataUrl: String; |
| 1562 |
begin |
| 1563 |
FullUrl := ResolveCastleDataUrl(AbsoluteUri(Url)); |
| 1564 |
DataUrl := ResolveCastleDataUrl('castle-data:/'); |
| 1565 |
WasInsideData := IsPrefix(DataUrl, FullUrl, true); |
| 1566 |
if WasInsideData then |
| 1567 |
Result := PrefixRemove(DataUrl, FullUrl, true) |
| 1568 |
else |
| 1569 |
Result := Url; |
| 1570 |
end; |
| 1571 |
|
| 1572 |
initialization |
| 1573 |
finalization |
| 1574 |
FreeAndNil(FUriMimeExtensions); |
| 1575 |
end. |