URI Utilities Library 1.0.0
Delphi 3, 4, 5, 6, 7 and Kylix Implementation
by Dieter Köhler
LICENSE
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
"http://www.mozilla.org/MPL/"
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is "UriUtils.pas".
The Initial Developer of the Original Code is Dieter Köhler (Heidelberg,
Germany, "http://www.philo.de/"). Portions created by the Initial Developer
are Copyright (C) 2003 Dieter Köhler. All Rights Reserved.
Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case the
provisions of the GPL are applicable instead of those above. If you wish to
allow use of your version of this file only under the terms of the GPL, and
not to allow others to use your version of this file under the terms of the
MPL, indicate your decision by deleting the provisions above and replace them
with the notice and other provisions required by the GPL. If you do not delete
the provisions above, a recipient may use your version of this file under the
terms of any one of the MPL or the GPL.
Introduction
The URI Utilities Library contains helper functions for Universal Resource Identifier (URI) processing. For more information about URIs see the relevant specifications:
- [RFC 2396] Berners-Lee, T., R. Fielding, U.C. Irvine, L. Masinter: "Uniform Resource Identifiers (URI): Generic Syntax", RFC 2396, August 1998, see <http://www.ietf.org/rfc/rfc2396.txt>.
The latest version of this software is available at <http://www.philo.de/xml/>.
Using the unit
The URI Utilities Library does not contain any components to be registered. So using it from inside your own projects is very simple: Add "UriUtils" to the uses clause of your unit and make sure that the path to the location of the UriUtils.pas file is included in Delphi's list of library paths. To include it go to the Library section of Delphi's Environment Options dialog (see the menu item: "Tools/Environment Options ...").
Routines for Testing URI Rules
String and Character Test Functions
The following functions serve to test whether a string S or character C conforms to the respective Universal Resource Identifier (URI) rule (see [RFC 2396], App. A). All functions return 'true' if S or C conforms to the rule, otherwise 'false' is returned.
function IsUriURI_referenceStr(S: string): Boolean;
function IsUriAbsoluteURIStr(S: string): Boolean;
function IsUriRelativeURIStr(S: string): Boolean;
function IsUriHier_partStr(S: string): Boolean;
function IsUriOpaque_partStr(S: string): Boolean;
function IsUriNet_pathStr(S: string): Boolean;
function IsUriAbs_pathStr(S: string): Boolean;
function IsUriRel_pathStr(S: string): Boolean;
function IsUriRel_segmentStr(S: string): Boolean;
function IsUriSchemeStr(S: string): Boolean;
function IsUriAuthorityStr(S: string): Boolean;
function IsUriReg_nameStr(S: string): Boolean;
function IsUriServerStr(S: string): Boolean;
function IsUriUserinfoStr(S: string): Boolean;
function IsUriHostPortStr(S: string): Boolean;
function IsUriHostStr(S: string): Boolean;
function IsUriHostnameStr(S: string): Boolean;
function IsUriDomainlabelStr(S: string): Boolean;
function IsUriToplabelStr(S: string): Boolean;
function IsUriIPv4addressStr(S: string): Boolean;
function IsUriPortStr(S: string): Boolean;
function IsUriPathStr(S: string): Boolean;
function IsUriPath_segmentsStr(S: string): Boolean;
function IsUriSegmentStr(S: string): Boolean;
function IsUriParamStr(S: string): Boolean;
function IsUriQueryStr(S: string): Boolean;
function IsUriFragmentStr(S: string): Boolean;
function IsUriUricStr(S: string): Boolean;
function IsUriReservedChar(C: Char): Boolean;
function IsUriUnreservedChar(C: Char): Boolean;
function IsUriMarkChar(C: Char): Boolean;
function IsUriHexChar(C: Char): Boolean;
function IsUriAlphanumChar(C: Char): Boolean;
function IsUriAlphaChar(C: Char): Boolean;
function IsUriDigitChar(C: Char): Boolean;
Wide String and Wide Character Test Functions
The following functions serve to test whether a wideString S or wideChar C conforms to the respective Universal Resource Identifier (URI) rule (see [RFC 2396], App. A). All functions return 'True' if S or C conforms to the rule, otherwise 'False' is returned.
function IsUriURI_referenceWideStr(S: WideString): Boolean;
function IsUriAbsoluteURIWideStr(S: WideString): Boolean;
function IsUriRelativeURIWideStr(S: WideString): Boolean;
function IsUriHier_partWideStr(S: WideString): Boolean;
function IsUriOpaque_partWideStr(S: WideString): Boolean;
function IsUriNet_pathWideStr(S: WideString): Boolean;
function IsUriAbs_pathWideStr(S: WideString): Boolean;
function IsUriRel_pathWideStr(S: WideString): Boolean;
function IsUriRel_segmentWideStr(S: WideString): Boolean;
function IsUriSchemeWideStr(S: WideString): Boolean;
function IsUriAuthorityWideStr(S: WideString): Boolean;
function IsUriReg_nameWideStr(S: WideString): Boolean;
function IsUriServerWideStr(S: WideString): Boolean;
function IsUriUserinfoWideStr(S: WideString): Boolean;
function IsUriHostPortWideStr(S: WideString): Boolean;
function IsUriHostWideStr(S: WideString): Boolean;
function IsUriHostnameWideStr(S: WideString): Boolean;
function IsUriDomainlabelWideStr(S: WideString): Boolean;
function IsUriToplabelWideStr(S: WideString): Boolean;
function IsUriIPv4addressWideStr(S: WideString): Boolean;
function IsUriPortWideStr(S: WideString): Boolean;
function IsUriPathWideStr(S: WideString): Boolean;
function IsUriPath_segmentsWideStr(S: WideString): Boolean;
function IsUriSegmentWideStr(S: WideString): Boolean;
function IsUriParamWideStr(S: WideString): Boolean;
function IsUriQueryWideStr(S: WideString): Boolean;
function IsUriFragmentWideStr(S: WideString): Boolean;
function IsUriUricWideStr(S: WideString): Boolean;
function IsUriReservedWideChar(C: WideChar): Boolean;
function IsUriUnreservedWideChar(C: WideChar): Boolean;
function IsUriMarkWideChar(C: WideChar): Boolean;
function IsUriHexWideChar(C: WideChar): Boolean;
function IsUriAlphanumWideChar(C: WideChar): Boolean;
function IsUriAlphaWideChar(C: WideChar): Boolean;
function IsUriDigitWideChar(C: WideChar): Boolean;
URI Processing
The following functions and classes provide means for some general operations on Universal Resource Identifiers.
TdomFilenameToUriOptions = set of (fuSetLocalhost, fuPlainColon);
TdomFilenameToUriOptions defines a set of constants which determine filename to URI transformations.
function FilenameToUriStr(const Path: TFilename;
const Opt: TUtilsFilenameToUriOptions): string;
Generates a "file" scheme URI reference from a string that represents a file name, and returns it as a string. Unreserved characters are escaped according to [RFC 2396], § 2.4.
Parameters:
- Path
A string that represents a file name.
- Opt
A set of option parameters to customize the behavior of the transformation. If the 'fuSetLocalhost' option is set, the host field of the URI is set to 'localhost', i.e. the URI begins with 'file://localhost/', otherwise the host field of the URI is void, i.e. the URI begins with 'file:///'. Note that according to [RFC 1630] a void host field is equivalent to "localhost".
If the 'fuPlainColon' option is set, colon charcaters (':') in the path are not escaped, otherwise colons are escaped correctly. Use the 'fuPlainColon' option as a workaround for a bug in the TWebBrowser.Navigate() and the equivalent SHDOCVW.DLL method, which requires a colon after the drive label.
Return Value:
- The generated "file" scheme URI reference as a string. If the specified path is empty, 'file://localhost' or 'file://' respectively is returned.
function FilenameToUriWideStr(const Path: TFilename;
const Opt: TUtilsFilenameToUriOptions): WideString;
Generates a "file" scheme URI reference from a string that represents a file name, and returns it as a wideString. Unreserved characters are escaped according to [RFC 2396], § 2.4.
Parameters:
- Path
A string that represents a file name.
- Opt
A set of option parameters to customize the behavior of the transformation. If the 'fuSetLocalhost' option is set, the host field of the URI is set to 'localhost', i.e. the URI begins with 'file://localhost/', otherwise the host field of the URI is void, i.e. the URI begins with 'file:///'. Note that according to [RFC 1630] a void host field is equivalent to "localhost".
If the 'fuPlainColon' option is set, colon charcaters (':') in the path are not escaped, otherwise colons are escaped correctly. Use the 'fuPlainColon' option as a workaround for a bug in the TWebBrowser.Navigate() and the equivalent SHDOCVW.DLL method, which requires a colon after the drive label.
Return Value:
- The generated "file" scheme URI reference as a WideString. If the specified path is empty, 'file://localhost' or 'file://' respectively is returned.
function ResolveRelativeUriStr(const BaseUri,
RelUri: string;
var ResultUri: string): Boolean;
Resolves an URI reference relative to a given base URI. For details on URI resolvement see [RFC 2396], § 5.2 and App. C.
Parameters:
- BaseUri
A US-ASCII string containing the base URI.
- RelUri
A US-ASCII string containing the URI to be resolved.
Return Parameter:
- ResultUri
A US-ASCII string containing the resolved URI or an empty string, if the URI cannot be resolved.
Return Value:
- Returns 'True' if the URI was succesfully resolved, otherwise 'False' is return. This functions fails to resolve an URI, if the provided baseUri or relUri is malformed, or if baseUri is not an absolute URI reference, or if relUri is a relative-path reference which contains some unresolvable '..' segment.
function ResolveRelativeUriWideStr(const BaseUri,
RelUri: WideString;
var ResultUri: WideString): Boolean;
Resolves an URI reference relative to a given base URI. For details on URI resolvement see [RFC 2396], § 5.2 and App. C.
Parameters:
- BaseUri
A UTF-16BE WideString containing the base URI.
- RelUri
A UTF-16BE WideString containing the URI to be resolved.
Return Parameter:
- ResultUri
A (UTF-16BE WideString containing the resolved URI or an empty WideString, if the URI cannot be resolved.
Return Value:
- Returns 'True' if the URI was succesfully resolved, otherwise 'False' is return. This functions fails to resolve an URI, if the provided baseUri or relUri is malformed, or if baseUri is not an absolute URI reference, or if relUri is a relative-path reference which contains some unresolvable '..' segment.
function UriStrToFilename(const Uri: string;
var Path: TFilename;
var Authority,
Query,
Fragment: string): Boolean;
Extracts the relevant information from a "file" scheme URI reference.
Parameters:
- Uri
A US-ASCII string containing the "file" scheme URI.
Return Parameter:
- Path
The path part of the URI converted to a file path, i.e. the leading '/' is removed, any other '/' is replaced by '\', and every escape sequence is resolved.
- Authority
The authority part of the URI or an empty string if none was provided.
- Query
The query part of the URI starting with '?' or an empty string if none was provided.
- Fragment
The fragment part of the URI starting with '#' or an empty string if none was provided.
Return Value:
- Returns 'True' if the file scheme URI was succesfully analyzed, otherwise 'False' is return. This functions fails to analyze an URI if the provided URI is malformed or is not assigned to the "file" scheme. In this case 'Path', 'Authority', 'Query' and 'Fragment' return an empty string.
function UriWideStrToFilename(const Uri: WideString;
var Path: TFilename;
var Authority,
Query,
Fragment: string): Boolean;
Extracts the relevant information from a "file" scheme URI reference.
Parameters:
- Uri
A UTF-16BE wideString containing the "file" scheme URI.
Return Parameter:
- Path
The path part of the URI converted to a file path, i.e. the leading '/' is removed, any other '/' is replaced by '\', and every escape sequence is resolved.
- Authority
The authority part of the URI or an empty WideString if none was provided.
- Query
The query part of the URI starting with '?' or an empty WideString if none was provided.
- Fragment
The fragment part of the URI starting with '#' or an empty WideString if none was provided.
Return Value:
- Returns 'True' if the file scheme URI was succesfully analyzed, otherwise 'False' is return. This functions fails to analyze an URI if the provided URI is malformed or is not assigned to the "file" scheme. In this case 'Path', 'Authority', 'Query' and 'Fragment' return an empty WideString.
TUriStrAnalyzer = class
This class can be used to analyze an URI reference or to construct an URI reference from its parts by using strings (see TUriWideStrAnalyzer for an equivalent class designed for wideStrings). The relevant parts are the URI reference's scheme (omitting the final ':'), authority (omitting the leading '//', path (including the leading '/' if any), query (omitting the leading '?'), and fragment identifier (omitting the leading '#'). The following properties can be used to extract the URI information as a string:
- property UriAuthority: string (readonly)
- property UriFragment: string (readonly)
- property UriPath: string (readonly)
- property UriQuery: string (readonly)
- property UriReference: string (readonly)
- property UriScheme: string (readonly)
Since an URI's authority, fragment, query or scheme part can be undefined as well as be empty, it is not sufficient to check only the string information, which also returns an empty string if the respective part is undefined. The following properties can be used to check whether a specific part was defined or not:
- property HasUriAuthority: boolean (readonly)
- property HasUriFragment: boolean (readonly)
- property HasUriQuery: boolean (readonly)
- property HasUriScheme: boolean (readonly)
To specify the value of an URI reference or of one of its parts the following functions can be used. The 'Value' parameter contains the value to be set and the 'IsDefined' parameter when required must be set to 'True' to define the respective URI part and set to 'False' to undefine it (in this case the 'Value' parameter is not evaluated and the respective string information property will return an empty string). Setting the value of one of the URI parts will also effect the value of the 'UriReference' property as well as setting the value of the 'UriReference' property will effect the URI parts. If a set operation was successful the function returns 'True'; otherwise -- i.e. if 'IsDefined' when required was set to 'True' and the provided 'Value' was malformed -- 'False' is returned and the result of the function is the same as if it was called with the property 'Value' containing an empty string and 'IsDefined' when required set to 'False'.
- function SetUriAuthority(const Value: string;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriFragment(const Value: string;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriPath(const Value: string): Boolean; virtual;
- function SetUriQuery(const Value: string;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriReference(const Value: string): Boolean; virtual;
- function SetUriScheme(const Value: string;
const IsDefined: Boolean): Boolean; virtual;
TUriWideStrAnalyzer = class
This class can be used to analyze an URI reference or to construct an URI reference from its parts by using wideStrings (see TUriStrAnalyzer for an equivalent class designed for dtrings). The relevant parts are the URI reference's scheme (omitting the final ':'), authority (omitting the leading '//', path (including the leading '/' if any), query (omitting the leading '?'), and fragment identifier (omitting the leading '#'). The following properties can be used to extract the URI information as a WideString:
- property UriAuthority: WideString (readonly)
- property UriFragment: WideString (readonly)
- property UriPath: WideString (readonly)
- property UriQuery: WideString (readonly)
- property UriReference: WideString (readonly)
- property UriScheme: WideString (readonly)
Since an URI's authority, fragment, query or scheme part can be undefined as well as be empty, it is not sufficient to check only the string information, which also returns an empty WideString if the respective part is undefined. The following properties can be used to check whether a specific part was defined or not:
- property HasUriAuthority: boolean (readonly)
- property HasUriFragment: boolean (readonly)
- property HasUriQuery: boolean (readonly)
- property HasUriScheme: boolean (readonly)
To specify the value of an URI reference or of one of its parts the following functions can be used. The 'Value' parameter contains the value to be set and the 'IsDefined' parameter when required must be set to 'True' to define the respective URI part and set to 'False' to undefine it (in this case the 'Value' parameter is not evaluated and the respective string information property will return an empty WideString). Setting the value of one of the URI parts will also effect the value of the 'UriReference' property as well as setting the value of the 'UriReference' property will effect the URI parts. If a set operation was successful the function returns 'True'; otherwise -- i.e. if 'IsDefined' when required was set to 'True' and the provided 'Value' was malformed -- 'False' is returned and the result of the function is the same as if it was called with the property 'Value' containing an empty string and 'IsDefined' when required set to 'False'.
- function SetUriAuthority(const Value: WideString;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriFragment(const Value: WideString;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriPath(const Value: WideString): Boolean; virtual;
- function SetUriQuery(const Value: WideString;
const IsDefined: Boolean): Boolean; virtual;
- function SetUriReference(const Value: WideString): Boolean; virtual;
- function SetUriScheme(const Value: WideString;
const IsDefined: Boolean): Boolean; virtual;