Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2009-05

Indy IdHTTP

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ABorka

Hi,

I came across some strange thing while making a simple program using Indy 10.

If I use an IdHTTP object to load a web page that redirects to another one, the new destination URL is not passed properly through the event handler.
Whether this is an indy or fpc problem, I'm not sure.

Here is the event handler in a way that it should work:

Declaration in Indy:
   TIdHTTPOnRedirectEvent = procedure(Sender: TObject; var dest: string;
var NumRedirect: Integer; var Handled: boolean; var VMethod:
TIdHTTPMethod) of object;
In FPC/Lazarus:
procedure TForm1.IdHTTP1Redirect(Sender: TObject; var dest: String;  var
NumRedirect: Integer; var Handled: Boolean;  var VMethod: IdHTTPMethod);
begin
   ShowMessage('Redirected to:' + dest);
end;

However, it seems that the parameter "dest" is passing a pointer to a pointer and not just a pointer to the string (displaying only some strange characters instead of the redirecting URL).

Therefore, the following works properly (while it should not):

procedure TForm1.IdHTTP1Redirect(Sender: TObject; var dest: String; var NumRedirect: Integer; var Handled: Boolean; var VMethod: IdHTTPMethod); var p:pchar;
begin
asm
pushad
lea esi, dest
mov eax, [esi]
mov eax, [eax]
mov p, eax
popad

   end;
   ShowMessage('Redirected to:' + p);{dest}

end;

Am I missing something or there is a problem somewhere? Indy and FPC both was compiled from the latest source available (as well as Lazarus)

Thanks for any thoughts,

AB

PS: I did not post this on the indy support forums because it seems that there are no questions answered (or even posted) in the past two years there (the latest indy source is form 2007 Nov).

Reply by email

Jonas Maebe
to ABorka
ABorka wrote 11 lines
In FPC/Lazarus: procedure TForm1.IdHTTP1Redirect(Sender: TObject; var dest: String; var NumRedirect: Integer; var Handled: Boolean; var VMethod: IdHTTPMethod); begin ShowMessage('Redirected to:' + dest); end; However, it seems that the parameter "dest" is passing a pointer to a pointer and not just a pointer to the string (displaying only some strange characters instead of the redirecting URL).

It is normal that dest is (implicitly) a pointer to a pointer. A "var" parameter means that the address of some variable is passed to a function, rather than only its value.

Now, an (Ansi)String is a pointer to an array of characters, with some extra data before it. So if you have a "var" parameter of the type (Ansi)String, you get a pointer to that "pointer to an array of characters". Otherwise you would be unable to change the address stored inside that pointer (you would only be able to permanently change the data in the array it points to, but not make it point to a different array in a way that persists after the function exits).

If you are only using Pascal code in the real program, this should all be handled transparently for you.

Jonas

Reply by email

ABorka
to Jonas Maebe

"If you are only using Pascal code in the real program, this should all be handled transparently for you."

I agree, so why it doesn't?
It only shows a few strange characters but not the string, as if it is just trying to display the pointer as a string.

As you say, the

ShowMessage('Redirected to:' + dest);

should work as is. But it does not.
UTF8 and ANSI conversion problems cannot happen here because "dest" is a URL so the UTF8 and ANSI characters are the same.

Jonas Maebe wrote 36 lines
On 24 May 2009, at 04:32, ABorka wrote: > In FPC/Lazarus: > procedure TForm1.IdHTTP1Redirect(Sender: TObject; var dest: String; > var NumRedirect: Integer; var Handled: Boolean; var VMethod: > IdHTTPMethod); > begin > ShowMessage('Redirected to:' + dest); > end; > > However, it seems that the parameter "dest" is passing a pointer to a > pointer and not just a pointer to the string (displaying only some > strange characters instead of the redirecting URL). It is normal that dest is (implicitly) a pointer to a pointer. A "var" parameter means that the address of some variable is passed to a function, rather than only its value. Now, an (Ansi)String is a pointer to an array of characters, with some extra data before it. So if you have a "var" parameter of the type (Ansi)String, you get a pointer to that "pointer to an array of characters". Otherwise you would be unable to change the address stored inside that pointer (you would only be able to permanently change the data in the array it points to, but not make it point to a different array in a way that persists after the function exits). If you are only using Pascal code in the real program, this should all be handled transparently for you. Jonas _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Jonas Maebe
to ABorka
ABorka wrote
"If you are only using Pascal code in the real program, this should all be handled transparently for you." I agree, so why it doesn't?

I don't know. The problem is not that ansistring var parameters are completely broken, since there is a lot of working code out there that uses them. If you can make a compilable example program that demonstrates the problem (preferably one that does not require indy, since it will have to be simplified anyway for adding to the testsuite), please submit a bug report.

Jonas

Reply by email