| 1 |
The information below contains early thoughts and decisions about URL support |
| 2 |
throughout Castle Game Engine. |
| 3 |
It is still true, but probably goes into too much details to be much useful. |
| 4 |
See https://castle-engine.io/manual_network.php for more up-to-date documentation |
| 5 |
of our URL support. |
| 6 |
|
| 7 |
------------------------------------------------------------------------------ |
| 8 |
|
| 9 |
Castle Game Engine allows you to use filenames everywhere where URLs are |
| 10 |
expected. |
| 11 |
|
| 12 |
Gains: |
| 13 |
- This is for backward compatibility with engine < 4.1.0. |
| 14 |
|
| 15 |
- This is also for comfort, both for users and for developers. |
| 16 |
|
| 17 |
For developers, e.g. you can use TOpenDialog.FileName value |
| 18 |
with Load3D, or TCastleScene.FileName (which is really just an alias |
| 19 |
for TCastleScene.URL). |
| 20 |
|
| 21 |
For users, e.g. you can pass both filenames and URLs |
| 22 |
as command-line parameters to castle-model-viewer. No need to explicitly |
| 23 |
mark what is a filename and what is an URL. |
| 24 |
|
| 25 |
--------------- |
| 26 |
Q: Should I upgrade my code to just use URLs everywhere, that is: |
| 27 |
to always pass an absolute URL to the engine routines that take URL parameter? |
| 28 |
Even though the above statement says that I don't have to |
| 29 |
(since we allow filenames instead of URLs everywhere)? |
| 30 |
|
| 31 |
A: The short answer is "yes". |
| 32 |
|
| 33 |
If you have a filename, you can reliably convert it to an URI |
| 34 |
using FilenameToUriSafe(ExpandFileName(FileName)). |
| 35 |
|
| 36 |
FilenameToUri and UriToFilename are inside URIParser unit, part of FPC, |
| 37 |
but better use FilenameToUriSafe and UriToFilenameSafe inside CastleUriUtils. |
| 38 |
|
| 39 |
Besides obvious benefits of using URLs (you will be able |
| 40 |
to pull stuff from network using http protocol), there are some smaller |
| 41 |
advantages: |
| 42 |
|
| 43 |
- In case of relative filenames, there are obscure cases |
| 44 |
(weird relative filenames with colon inside the name) |
| 45 |
that may be mistaken for URIs. |
| 46 |
|
| 47 |
- Because an URL could have some characters encoded. |
| 48 |
Inside an URI, "%25" means a "%" and "%4d" means "M". |
| 49 |
UriToFilenameSafe and FilenameToUriSafe handle |
| 50 |
the percent-encoding/decoding consistently. |
| 51 |
|
| 52 |
- For the future: right now, absolute filenames are never mistaken |
| 53 |
for an URL (see description below how we do it). |
| 54 |
But this depends on special properties of Windows and Unix |
| 55 |
filenames, and depends on the fact that no protocol name is a single |
| 56 |
letter. What if we are ported some day to a weird OS that has different |
| 57 |
filename conventions? What if we use some single-letter protocol |
| 58 |
name in the future? |
| 59 |
|
| 60 |
--------------- |
| 61 |
Q: How does it work? |
| 62 |
|
| 63 |
A: URIProtocol is the judge. |
| 64 |
|
| 65 |
- When URIProtocol is not empty, we know that we have an absolute URI. |
| 66 |
Absolute URI is just an URI that has a protocol. |
| 67 |
|
| 68 |
This works 100% reliably for absolute filenames. |
| 69 |
For Unix, this works since absolute |
| 70 |
filename on Unix starts with "/" which cannot be a part of protocol name. |
| 71 |
For Windows, it works thanks to the mechanism mentioned above that |
| 72 |
ignores single-letter protocols. |
| 73 |
So absolute filename will never be mistaken for an URI. |
| 74 |
|
| 75 |
In case you used a relative filename, there is unfortunately |
| 76 |
a small possibility of mistakenly treating it as URL with protocol. |
| 77 |
That is because some filesystems allow to use colon inside a filename, |
| 78 |
so you could make a weird filename that even starts with "http:". |
| 79 |
|
| 80 |
- When URIProtocol is empty, it means that we have an absolute filename |
| 81 |
or a relative filename or URI. |
| 82 |
|
| 83 |
Relative filename or relative URI can always be differentiated |
| 84 |
by context. |
| 85 |
|
| 86 |
(Although the distinction doesn't really matter, since right now |
| 87 |
nothing encodes/decodes any special chars inside URIs. |
| 88 |
Except possibly CombineURI (that uses ParseURI that calls EncodeURI |
| 89 |
that calls Escape) but CombineURI returns always an absolute URI |
| 90 |
(with protocol).) |
| 91 |
|
| 92 |
- AbsoluteURI function treats them as relative filenames, |
| 93 |
relative to the current directory. Almost all URL function parameters |
| 94 |
in code are passed to the AbsoluteURI (with the exception of parameters |
| 95 |
to special CombineURI). |
| 96 |
|
| 97 |
This means that passing any filenames (absolute or relative) or |
| 98 |
absolute URIs to AbsoluteURI is a good way to convert them to URI. |
| 99 |
Although when you *know* that you have a filename (not URI), |
| 100 |
it's better to use FilenameToUriSafe(ExpandFileName(FileName)) |
| 101 |
(this avoids the ambiguous relative filenames with colons problems). |
| 102 |
|
| 103 |
- CombineURI treats the 2nd parameter (Relative) as relative URIs, |
| 104 |
relative to the given base URI. All URLs present inside documents |
| 105 |
(for example inside VRML/X3D "url" fields) are passed to CombineURI, |
| 106 |
as they are always relative to the base document URL. |
| 107 |
|
| 108 |
Passing a filename as the 2nd parameter to CombineURI is *not* allowed. |
| 109 |
Although a relative filename using slashes is in practice just like |
| 110 |
a relative URI, so not a problem. |
| 111 |
But absolute filename (Unix or Windows) or using backslashes |
| 112 |
*may* fail. CombineURI depends on ResolveRelativeURI which simply |
| 113 |
assumes that we really have an URI, not a filename. |