Board index » delphi » HtmlEncode function in HttpApp unit

HtmlEncode function in HttpApp unit


2006-10-03 05:21:09 AM
delphi94
I have Delphi 6 build 6.240 update pack 2 installed, and I am looking for the
HtmlEncode in HttpApp.pas/dcu that people talk about on the internet (incl.
BDN) but I can not find it. I do have to use that function in HttpApp to get
a legacy app to compile. Which Delphi version exactly has the function in
HttpApp? How can I get a copy of the unit? Thanks in advance.
 
 

Re:HtmlEncode function in HttpApp unit

Hello,
The earliest version of Delphi I have on the machine I am currently on is
Delphi7. It does contains the routines you mentioned. I am including an
excerpt below. If you need a copy of the file, I would be happy to email a copy
over. Just let me know.
Cheers,
Bruneau.
============== [HTTPApp.pas] ==================
function HTTPDecode(const AStr: String): String;
var
Sp, Rp, Cp: PChar;
S: String;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <>#0 do
begin
case Sp^ of
'+': Rp^ := ' ';
'%': begin
// Look for an escaped % (%%) or %<hex>encoded character
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <>#0) and (Sp^ <>#0) then
begin
S := '$' + Cp^ + Sp^;
Rp^ := Chr(StrToInt(S));
end
else
raise
EWebBrokerException.CreateFmt(sErrorDecodingURLText, [Cp - PChar(AStr)]);
end;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidURLEncodedChar,
['%' + Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTTPEncode(const AStr: String): String;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
'0'..'9','$','!','''','(',')'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <>#0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp,2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTMLEncode(const AStr: String): String;
const
Convert = ['&','<','>','"'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 10);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
FormatBuf(Rp^, 5, '&', 5, []);
Inc(Rp,4);
end;
'<',
'>': begin
if Sp^ = '<' then
FormatBuf(Rp^, 4, '<', 4, [])
else
FormatBuf(Rp^, 4, '>', 4, []);
Inc(Rp,3);
end;
'"': begin
FormatBuf(Rp^, 6, '"', 6, []);
Inc(Rp,5);
end;
else
Rp^ := Sp^
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTMLDecode(const AStr: String): String;
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of
'a': if AnsiStrPos(Sp, 'amp;') = Sp then { do not
localize }
begin
Inc(Sp, 3);
Rp^ := '&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp,
'gt;') = Sp) then { do not localize }
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
if Cp^ = 'l' then
Rp^ := '<'
else
Rp^ := '>';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then { do not
localize }
begin
Inc(Sp,4);
Rp^ := '"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
Rp^ := Chr((I));
end;
else
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
end
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;
============== [HTTPApp.pas] ==================
 

Re:HtmlEncode function in HttpApp unit

Thanks for the response, Jean. I apologize for the inappropriately-setup
name I posted my original question with, now it is fixed :)
Now it seems that there's a confusion between HtmlEncode and HttpEncode
I've got legacy source code which calls HtmlEncode (not Http) and the
developer who wrote the code said that it is a built-in function from HttpApp
unit and he was using Delphi 6, no change was made to standard units that
came from Delphi installation.
If Delphi 7 that you have doesn't have HtmlEncode then it seems less
possible that the function is a part of Delphi's HttpApp unit.
Anyways, thanks a lot for checking. I really appreciate it :)
Have a nice day,
Andy
"Jean-Marie Babet" <XXXX@XXXXX.COM>writes
Quote
Hello,

The earliest version of Delphi I have on the machine I am currently on is
Delphi7. It does contains the routines you mentioned. I am including an
excerpt below. If you need a copy of the file, I would be happy to email a
copy
over. Just let me know.

Cheers,

Bruneau.


============== [HTTPApp.pas] ==================


function HTTPDecode(const AStr: String): String;
var
Sp, Rp, Cp: PChar;
S: String;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <>#0 do
begin
case Sp^ of
'+': Rp^ := ' ';
'%': begin
// Look for an escaped % (%%) or %<hex>encoded character
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <>#0) and (Sp^ <>#0) then
begin
S := '$' + Cp^ + Sp^;
Rp^ := Chr(StrToInt(S));
end
else
raise
EWebBrokerException.CreateFmt(sErrorDecodingURLText, [Cp - PChar(AStr)]);
end;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidURLEncodedChar,
['%' + Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;

function HTTPEncode(const AStr: String): String;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
'0'..'9','$','!','''','(',')'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <>#0 do
begin
if Sp^ in NoConversion then
Rp^ := Sp^
else
if Sp^ = ' ' then
Rp^ := '+'
else
begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp,2);
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

function HTMLEncode(const AStr: String): String;
const
Convert = ['&','<','>','"'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 10);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
FormatBuf(Rp^, 5, '&', 5, []);
Inc(Rp,4);
end;
'<',
'>': begin
if Sp^ = '<' then
FormatBuf(Rp^, 4, '<', 4, [])
else
FormatBuf(Rp^, 4, '>', 4, []);
Inc(Rp,3);
end;
'"': begin
FormatBuf(Rp^, 6, '"', 6, []);
Inc(Rp,5);
end;
else
Rp^ := Sp^
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;

function HTMLDecode(const AStr: String): String;
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of
'a': if AnsiStrPos(Sp, 'amp;') = Sp then { do not
localize }
begin
Inc(Sp, 3);
Rp^ := '&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp,
'gt;') = Sp) then { do not localize }
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
if Cp^ = 'l' then
Rp^ := '<'
else
Rp^ := '>';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then { do not
localize }
begin
Inc(Sp,4);
Rp^ := '"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
Rp^ := Chr((I));
end;
else
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
end
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;

============== [HTTPApp.pas] ==================


 

Re:HtmlEncode function in HttpApp unit

Oops!
My apologies. HttpApp has both Html[Encode|Decode] and Http[Encode|Decode]
methods. Later today I will pull the sources from Delphi6 to check there too.
Here's the excerpt for the Html* methods from the Delphi7 source:
============== [HTTPApp.pas] ==================
function HTMLEncode(const AStr: String): String;
const
Convert = ['&','<','>','"'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 10);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
FormatBuf(Rp^, 5, '&', 5, []);
Inc(Rp,4);
end;
'<',
'>': begin
if Sp^ = '<' then
FormatBuf(Rp^, 4, '<', 4, [])
else
FormatBuf(Rp^, 4, '>', 4, []);
Inc(Rp,3);
end;
'"': begin
FormatBuf(Rp^, 6, '"', 6, []);
Inc(Rp,5);
end;
else
Rp^ := Sp^
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTMLDecode(const AStr: String): String;
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <>#0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of
'a': if AnsiStrPos(Sp, 'amp;') = Sp then { do not
localize }
begin
Inc(Sp, 3);
Rp^ := '&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp,
'gt;') = Sp) then { do not localize }
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
if Cp^ = 'l' then
Rp^ := '<'
else
Rp^ := '>';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then { do not
localize }
begin
Inc(Sp,4);
Rp^ := '"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <>';') and (Sp^ <>#0) do
Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
Rp^ := Chr((I));
end;
else
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
end
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;
============== [HTTPApp.pas] ==================
Cheers,
Bruneau.
 

Re:HtmlEncode function in HttpApp unit

I've found the function, it IS indeed in update pack 2, somehow the update
didn't go through (I didn't see anything wrong when I applied the update)
and the HttpApp file didn't get updated.
Thanks for helping me look into it :)
"news.microsoft.com" <XXXX@XXXXX.COM>writes
Quote
I have Delphi 6 build 6.240 update pack 2 installed, and I am looking for
the HtmlEncode in HttpApp.pas/dcu that people talk about on the internet
(incl. BDN) but I can not find it. I do have to use that function in HttpApp
to get a legacy app to compile. Which Delphi version exactly has the
function in HttpApp? How can I get a copy of the unit? Thanks in
advance.