Basic URI Parsing topic
Basic usage of URI parsing functions
Declaration
Basic URI ParsingDescription
The URIParser unit provides simple functions to parse and construct URIs.
The main function is ParseURI which parses a URI string into a TURI record containing all the components.
The following example demonstrates basic URI parsing and reconstruction:
program BasicURIExample;
{$mode objfpc}{$h+}
uses
URIParser, SysUtils;
procedure ParseAndDisplay(const URIString: string);
var
URI: TURI;
begin
WriteLn('Original URI: ', URIString);
// Parse the URI
URI := ParseURI(URIString);
// Display all components
WriteLn(' Protocol: ', URI.Protocol);
WriteLn(' Username: ', URI.Username);
WriteLn(' Password: ', URI.Password);
WriteLn(' Host: ', URI.Host);
WriteLn(' Port: ', URI.Port);
WriteLn(' Path: ', URI.Path);
WriteLn(' Document: ', URI.Document);
WriteLn(' Params: ', URI.Params);
WriteLn(' Bookmark: ', URI.Bookmark);
WriteLn(' Has Authority: ', URI.HasAuthority);
// Reconstruct the URI
WriteLn(' Reconstructed: ', EncodeURI(URI));
WriteLn;
end;
begin
// Parse different types of URIs
ParseAndDisplay('https://user:pass@www.example.com:8080/path/to/page.html?param1=value1¶m2=value2#section1');
ParseAndDisplay('ftp://files.example.org/pub/software/myfile.tar.gz');
ParseAndDisplay('mailto:user@example.com');
ParseAndDisplay('file:///home/user/document.txt');
ParseAndDisplay('/relative/path/file.html');
end.
The following example shows how to work with relative URIs and file paths:
program URIUtilitiesExample;
{$mode objfpc}{$h+}
uses
URIParser, SysUtils;
var
BaseURI, RelativeURI, AbsoluteURI: string;
Filename: string;
URI: string;
begin
WriteLn('=== Relative URI Resolution ===');
BaseURI := 'https://www.example.com/docs/manual/';
RelativeURI := '../images/logo.png';
if ResolveRelativeURI(BaseURI, RelativeURI, AbsoluteURI) then
begin
WriteLn('Base URI: ', BaseURI);
WriteLn('Relative URI: ', RelativeURI);
WriteLn('Absolute URI: ', AbsoluteURI);
end
else
WriteLn('Failed to resolve relative URI');
WriteLn;
WriteLn('=== File/URI Conversion ===');
// Convert filename to URI
Filename := '/home/user/documents/readme.txt';
URI := FilenameToURI(Filename);
WriteLn('Filename: ', Filename);
WriteLn('File URI: ', URI);
// Convert URI back to filename
if URIToFilename(URI, Filename) then
begin
WriteLn('Converted back to filename: ', Filename);
end
else
WriteLn('Failed to convert URI to filename');
WriteLn;
WriteLn('=== URI Type Checking ===');
// Check if URIs are absolute
URI := 'https://www.example.com/page.html';
WriteLn('URI: ', URI, ' - Is absolute: ', IsAbsoluteURI(URI));
URI := '/relative/path.html';
WriteLn('URI: ', URI, ' - Is absolute: ', IsAbsoluteURI(URI));
URI := 'mailto:test@example.com';
WriteLn('URI: ', URI, ' - Is absolute: ', IsAbsoluteURI(URI));
end.
The following example demonstrates URI parsing with default values:
program DefaultsURIExample;
{$mode objfpc}{$h+}
uses
URIParser, SysUtils;
procedure ParseWithDefaults(const URIString: string; const DefaultProtocol: string; DefaultPort: Word);
var
URI: TURI;
begin
WriteLn('Parsing: ', URIString);
WriteLn('Default protocol: ', DefaultProtocol);
WriteLn('Default port: ', DefaultPort);
// Parse with default protocol and port
URI := ParseURI(URIString, DefaultProtocol, DefaultPort);
WriteLn('Results:');
WriteLn(' Protocol: ', URI.Protocol);
WriteLn(' Host: ', URI.Host);
WriteLn(' Port: ', URI.Port);
WriteLn(' Path: ', URI.Path);
WriteLn(' Document: ', URI.Document);
WriteLn(' Full URI: ', EncodeURI(URI));
WriteLn;
end;
begin
// Parse URIs with missing protocol/port using defaults
ParseWithDefaults('www.example.com/index.html', 'http', 80);
ParseWithDefaults('ftp.example.org/pub/files/', 'ftp', 21);
ParseWithDefaults('mail.example.com:993/inbox', 'imaps', 993);
// Parse complete URIs (defaults are ignored)
ParseWithDefaults('https://secure.example.com:443/login', 'http', 80);
end.
Key functions in the URIParser unit:
-
ParseURI- Parses a URI string into components -
EncodeURI- Reconstructs a URI string from components -
ResolveRelativeURI- Resolves relative URIs against a base URI -
URIToFilename- Converts file URIs to local filenames -
FilenameToURI- Converts local filenames to file URIs -
IsAbsoluteURI- Checks if a URI is absolute (has a protocol)
See also
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
