Unit CastleUriUtils
Description
URI utilities. These extend standard FPC URIParser unit.
Uses
- SysUtils
- Classes
- CastleStringUtils
Overview
Functions and Procedures
procedure UriExtractAnchor(var Uri: String; out Anchor: string); |
procedure UriGetAnchor(const Uri: String; out Anchor: string); |
procedure UriGetSettingsFromAnchor(const Uri: String; const SettingsFromAnchor: TStringStringMap); |
function UriDeleteAnchor(const Uri: String): string; |
function RawUriDecode(const S: string): string; |
function UriProtocol(const Uri: String): string; |
function UriProtocolIs(const S: string; const Protocol: string; out Colon: Integer): boolean; |
function UriDeleteProtocol(const S: string): string; deprecated 'use ParseUri to extract Uri.Path + Uri.Document, instead of this routine that doesn''t do decoding'; |
function UriValidProtocol(const P: String): Boolean; |
function CombineUri(const Base, Relative: string): string; |
function AbsoluteUri(const Uri: String): string; |
function AbsoluteFileUri(const Uri: String): boolean; |
function UriToFilenameSafe(const Uri: String): string; |
function FilenameToUriSafe(FileName: string): string; |
function RelativeFilenameToUriSafe(const FileName: String): String; |
function MaybeUseDataProtocol(const Url: String): String; |
function UriMimeType(const Uri: String): string; overload; |
function UriMimeType(const Uri: String; out Gzipped: boolean): string; overload; |
function UriMimeExtensions: TStringStringMap; |
function UriDisplay(const Uri: String; const Short: boolean = false): string; |
function UriCaption(const Uri: String): string; |
function ChangeUriExt(const Url, Extension: string): string; |
function DeleteUriExt(const Url: String): string; |
function ExtractUriName(const Url: String): string; |
function ExtractUriPath(const Url: String): string; |
function UriIncludeSlash(const Url: String): String; |
function UriExcludeSlash(const Url: String): String; |
function UriFileExists(const Url: String): Boolean; |
function UriExists(Url: String): TUriExists; |
function UriCurrentPath: string; |
function ResolveCastleDataUrl(const Url: String): String; |
function RelativeToCastleDataUrl(const Url: String; out WasInsideData: Boolean): String; |
Types
TUriExists = (...); |
Variables
CastleDataIgnoreCase: Boolean = false; |
Description
Functions and Procedures
procedure UriExtractAnchor(var Uri: String; out Anchor: string); |
Extracts #anchor from URI. On input, URI contains full URI. On output, Anchor is removed from URI and saved in Anchor. If no #anchor existed, Anchor is set to ''. |
procedure UriGetAnchor(const Uri: String; out Anchor: string); |
Like UriExtractAnchor, but URI remains unchanged. |
procedure UriGetSettingsFromAnchor(const Uri: String; const SettingsFromAnchor: TStringStringMap); |
Calculate #anchor from an URI, and split it into a key-value map. This supports special CGE syntax within URL anchor to specify loading parameters for Spine, sprite sheets, images. On output, the key-value pairs from anchor are saved in TStringStringMap. The SettingsFromAnchor is always cleared at the beginning. If no anchor existed, SettingsFromAnchor will be empty when this ends. |
function UriDeleteAnchor(const Uri: String): string; |
Return URI with anchor (if was any) stripped. |
function RawUriDecode(const S: string): string; |
Replace all sequences like %xx with their actual 8-bit characters. The intention is that this is similar to PHP function with the same name. To account for badly encoded strings, invalid encoded URIs do not raise an error — they are only reported to WritelnWarning. So you can simply ignore them, or write a warning about them for user. This is done because often you will use this with URIs provided by the user, read from some file etc., so you can't be sure whether they are correctly encoded, and raising error unconditionally is not OK. (Considering the number of bad HTML pages on WWW.) The cases of badly encoded strings are:
|
function UriProtocol(const Uri: String): string; |
Get protocol from given URI. This is very similar to how UriParser.ParseUri function detects the protocol, although not 100% compatible:
|
function UriProtocolIs(const S: string; const Protocol: string; out Colon: Integer): boolean; |
Check does URI contain given Protocol. This is equivalent to checking UriProtocol(S) = Protocol, ignoring case, although may be a little faster. Given Protocol string cannot contain ":" character. |
function UriDeleteProtocol(const S: string): string; deprecated 'use ParseUri to extract Uri.Path + Uri.Document, instead of this routine that doesn''t do decoding'; |
Warning: this symbol is deprecated: use ParseUri to extract Uri.Path + Uri.Document, instead of this routine that doesn't do decoding Remove the protocol part from URI. |
function UriValidProtocol(const P: String): Boolean; |
Is the S a valid protocol scheme. Following https://datatracker.ietf.org/doc/html/rfc3986 , protocol scheme must
|
function CombineUri(const Base, Relative: string): string; |
Return absolute URI, given base and relative URI. Base URI must be either an absolute (with protocol) URI, or only an absolute filename (in which case we'll convert it to file:// URI under the hood, if necessary). This is usually the URI of the containing file, for example an HTML file referencing the image, processed by AbsoluteURI. Relative URI may be a relative URI or an absolute URI. In the former case it is merged with Base. In the latter case it is simply returned. If you want to support relative URIs, you want to use this routine. It treats Relative always as an URI (so it should be percent-escaped, with slashes and such). Other routines in our engine, like AbsoluteUri and Download, treat strings without protocol as a filename (so it's not percent-escaped, it uses PathDelim specific to OS — slash or backslash etc.). This routine, on the other hand, treats Relative string always as an URI (when it doesn't include protocol, it just means it's relative to Base). |
function AbsoluteUri(const Uri: String): string; |
Make sure that the URI is absolute (always has a protocol). This function treats an URI without a protocol as a simple filename (absolute or relative to the current directory). This includes treating empty string as equivalent to current directory. |
function AbsoluteFileUri(const Uri: String): boolean; |
Does URI contain only an absolute filename. Useful to detect unwanted paths in data files, you usually do not want to have such paths in data files, as they make it impossible to transfer the data (move/copy files) to other system/location. |
function UriToFilenameSafe(const Uri: String): string; |
Convert URI (or filename) to a filename. This is an improved UriToFilename from UriParser. When URI is already a filename, this does a better job than UriToFilename, as it handles also Windows absolute filenames (see UriProtocol). Returns empty string in case of problems, for example when this is not a file URI. Just like UriParser.UriToFilename, this percent-decodes the parameter. For example, It also handles our castle-data: protocol. |
function FilenameToUriSafe(FileName: string): string; |
Convert filename to URI. This is a fixed version of UriParser.FilenameToUri, that correctly percent-encodes the parameter, making it truly a reverse of UriToFilenameSafe. In FPC > 2.6.2 UriParser.FilenameToUri will also do this (after Michalis' patch, see http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=revision&revision=24321 ). It also makes sure the filename is absolute (it uses ExpandFileName, so if the FileName is relative — it will be expanded, treating it as relative to the current directory). |
function RelativeFilenameToUriSafe(const FileName: String): String; |
Convert filename to URI, if the filename is relative – the URI will also be relative. In contrast to FilenameToUriSafe, which always returns absolute URI, this will return relative URI if the filename is relative. If the given filename is absolute, this is equivalent to FilenameToUriSafe. The FileName = '' is also considered relative, and returns ''. |
function MaybeUseDataProtocol(const Url: String): String; |
Tries change URI to use castle-data: protocol. It's used in our editor to change absolute paths to relative to castle-data directory. |
function UriMimeType(const Uri: String): string; overload; |
Get MIME type for content of the URI without downloading the file. For local and remote files (file, http, and similar protocols) it guesses MIME type based on file extension. Using this function is not adviced if you want to properly support MIME types returned by http server for network resources. For this, you have to download the file, and look at what MIME type the http server reports. The Download function returns such proper MimeType. This function only guesses without downloading. Returns empty string if MIME type is unknown. Overloaded version returns also Gzipped to detect whether file contents are gzipped. The recognition mechanism can be enhanced by adding your own mappings to the UriMimeExtensions. |
function UriMimeType(const Uri: String; out Gzipped: boolean): string; overload; |
This item has no description. |
function UriMimeExtensions: TStringStringMap; |
Map from an extension to a MIME type, used by UriMimeType. The extension should be lowercase, and includes a leading dot, like |
function UriDisplay(const Uri: String; const Short: boolean = false): string; |
Convert URI to a nice form for display (to show in messages and such). It makes sure to nicely trim URLs that would be too long/unreadable otherwise (like "data:" URI, or multi-line URLs with inlined ECMAScript/CastleScript/shader code). When Short = When Short = It is safe to use this on both absolute and relative URLs. It does not resolve relative URLs in any way. It also means that it returns empty string for empty URI (contrary to most other routines that convert empty string to a current directory when resolving relative URLs). |
function UriCaption(const Uri: String): string; |
Convert URI to a nice form for a short caption. Returns empty string for empty URI (contrary to most other routines that treat empty string like a current directory). See UriDisplay documentation for details. This calls UriDisplay with Short = |
function ChangeUriExt(const Url, Extension: string): string; |
Change extension of the URL. |
function DeleteUriExt(const Url: String): string; |
Delete extension of the URL. |
function ExtractUriName(const Url: String): string; |
Extract filename (last part after slash) from URL. |
function ExtractUriPath(const Url: String): string; |
Extract path (everything before last part), including final slash, from URL. |
function UriIncludeSlash(const Url: String): String; |
Ensure URL ends with slash. For an empty URL, returns empty string (so it does not turn "" into "/"). For an URL ending with bashslash (which usually means you passed Windows path name), it removes the backslash before adding slash. This should be used instead of InclPathDelim or IncludeTrailingPathDelimiter, when you use URLs instead of filenames. |
function UriExcludeSlash(const Url: String): String; |
Ensure URL does not end with slash. In case you passed Windows path name, it also removes the backslash. This should be used instead of ExclPathDelim or ExcludeTrailingPathDelimiter, when you use URLs instead of filenames. |
function UriFileExists(const Url: String): Boolean; |
Does a file exist, that is: whether it makes sense to load it with the Download function. Returns This is simply a shortcut for |
function UriExists(Url: String): TUriExists; |
Does a file or directory exist under this URL. See TUriExists for possible return values. |
function UriCurrentPath: string; |
Current working directory of the application, expressed as URL, including always final slash at the end. |
function ResolveCastleDataUrl(const Url: String): String; |
If the given URL uses "castle-data:..." protocol, resolve it, returning a URL that does not use "castle-data:..." protocol any more. For example may resolve "castle-data:/xxx" into "file:///home/michalis/my-application/data/xxx". See https://castle-engine.io/data for documentation how our "data directory" works. If the URL has a different protocol, it is returned unchanged. |
function RelativeToCastleDataUrl(const Url: String; out WasInsideData: Boolean): String; |
If this URL indicates something inside the CGE data directory then return URL relative to this data directory. E.g. for "castle-data:/foo/bar.txt" it returns "foo/bar.txt". It accepts any URL, relative (to the current working directory) or absolute (with any protocol). It works when the URL starts with castle-data:/ protocol, it works when the URL starts with other (like file:/) protocol that still points to a file inside data. If the URL does not point to a file in data, it is returned untouched. |
Types
TUriExists = (...); |
Result of the UriExists query. Values
|
Variables
CastleDataIgnoreCase: Boolean = false; |
On systems where filesystems are usually case-sensitive (Unix; like Linux, FreeBSD, macOS), accept any This is a quick way to run applications prepared / tested on case-insensitive systems (like Windows) on Unix. It causes a small additional work when we open data files, but in many cases it is acceptable. |
Generated by PasDoc 0.16.0-snapshot.