1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
{
$Id: uriparser.pp,v 1.5 2005/02/14 17:13:19 peter Exp $
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
Original author: Sebastian Guenther
Unit to parse complete URI in its parts or to reassemble an URI
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$MODE objfpc}
{$H+}
unit URIParser;
interface
type
TURI = record
Protocol: String;
Username: String;
Password: String;
Host: String;
Port: Word;
Path: String;
Document: String;
Params: String;
Bookmark: String;
end;
function EncodeURI(const URI: TURI): String;
function ParseURI(const URI: String): TURI;
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word): TURI;
implementation
uses SysUtils;
const
HexTable: array[0..15] of Char = '0123456789abcdef';
function EncodeURI(const URI: TURI): String;
function Escape(const s: String): String;
var
i: Integer;
begin
SetLength(Result, 0);
for i := 1 to Length(s) do
if not (s[i] in ['0'..'9', 'A'..'Z', 'a'..'z', ',', '-', '.', '_',
'/', '\']) then
Result := Result + '%' + HexTable[Ord(s[i]) shr 4] +
HexTable[Ord(s[i]) and $f]
else
Result := Result + s[i];
end;
begin
SetLength(Result, 0);
if Length(URI.Protocol) > 0 then
Result := LowerCase(URI.Protocol) + ':';
if Length(URI.Host) > 0 then
begin
Result := Result + '//';
if Length(URI.Username) > 0 then
begin
Result := Result + URI.Username;
if Length(URI.Password) > 0 then
Result := Result + ':' + URI.Password;
Result := Result + '@';
end;
Result := Result + URI.Host;
end;
if URI.Port <> 0 then
Result := Result + ':' + IntToStr(URI.Port);
Result := Result + Escape(URI.Path);
if Length(URI.Document) > 0 then
begin
if (Length(Result) = 0) or (Result[Length(Result)] <> '/') then
Result := Result + '/';
Result := Result + Escape(URI.Document);
end;
if Length(URI.Params) > 0 then
Result := Result + '?' + URI.Params;
if Length(URI.Bookmark) > 0 then
Result := Result + '#' + Escape(URI.Bookmark);
end;
function ParseURI(const URI: String): TURI;
begin
Result := ParseURI(URI, '', 0);
end;
function ParseURI(const URI, DefaultProtocol: String; DefaultPort: Word): TURI;
function Unescape(const s: String): String;
function HexValue(c: Char): Integer;
begin
if (c >= '0') and (c <= '9') then
Result := Ord(c) - Ord('0')
else if (c >= 'A') and (c <= 'F') then
Result := Ord(c) - Ord('A') + 10
else if (c >= 'a') and (c <= 'f') then
Result := Ord(c) - Ord('a') + 10
else
Result := 0;
end;
var
i, RealLength: Integer;
begin
SetLength(Result, Length(s));
i := 1;
RealLength := 0;
while i <= Length(s) do
begin
Inc(RealLength);
if s[i] = '%' then
begin
Result[RealLength] := Chr(HexValue(s[i + 1]) shl 4 or HexValue(s[i + 2]));
Inc(i, 3);
end else
begin
Result[RealLength] := s[i];
Inc(i);
end;
end;
SetLength(Result, RealLength);
end;
var
s: String;
i, LastValidPos: Integer;
begin
Result.Protocol := LowerCase(DefaultProtocol);
Result.Port := DefaultPort;
s := URI;
// Extract the protocol
for i := 1 to Length(s) do
if s[i] = ':' then
begin
Result.Protocol := Copy(s, 1, i - 1);
s := Copy(s, i + 1, Length(s));
break;
end else if not (s[i] in ['0'..'9', 'A'..'Z', 'a'..'z']) then
break;
// Extract the bookmark name
for i := Length(s) downto 1 do
if s[i] = '#' then
begin
Result.Bookmark := Unescape(Copy(s, i + 1, Length(s)));
s := Copy(s, 1, i - 1);
break;
end else if s[i] = '/' then
break;
// Extract the params
for i := Length(s) downto 1 do
if s[i] = '?' then
begin
Result.Params := Copy(s, i + 1, Length(s));
s := Copy(s, 1, i - 1);
break;
end else if s[i] = '/' then
break;
// Extract the document name
for i := Length(s) downto 1 do
if s[i] = '/' then
begin
Result.Document := Unescape(Copy(s, i + 1, Length(s)));
s := Copy(s, 1, i - 1);
break;
end else if s[i] = ':' then
break;
// Extract the path
LastValidPos := 0;
for i := Length(s) downto 1 do
if (s[i] = '/')
and ((I>1) and (S[i-1]<>'/'))
and ((I<Length(S)) and (S[I+1]<>'/')) then
LastValidPos := i
else if s[i] in [':', '@'] then
break;
if (LastValidPos > 0) and
(Length(S)>LastValidPos) and
(S[LastValidPos+1]<>'/') then
begin
Result.Path := Unescape(Copy(s, LastValidPos, Length(s)));
s := Copy(s, 1, LastValidPos - 1);
end;
// Extract the port number
for i := Length(s) downto 1 do
if s[i] = ':' then
begin
Result.Port := StrToInt(Copy(s, i + 1, Length(s)));
s := Copy(s, 1, i - 1);
break;
end else if s[i] in ['@', '/'] then
break;
// Extract the hostname
if ((Length(s) > 2) and (s[1] = '/') and (s[2] = '/')) or
((Length(s) > 1) and (s[1] <> '/')) then
begin
if s[1] <> '/' then
s := '//' + s;
for i := Length(s) downto 1 do
if s[i] in ['@', '/'] then
begin
Result.Host := Copy(s, i + 1, Length(s));
s := Copy(s, 3, i - 3);
break;
end;
// Extract username and password
if Length(s) > 0 then
begin
i := Pos(':', s);
if i = 0 then
Result.Username := s
else
begin
Result.Username := Copy(s, 1, i - 1);
Result.Password := Copy(s, i + 1, Length(s));
end;
end;
end;
end;
end.
|