Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 May : Re: TIdTCPClient, TIdIOHandler and a Headache

www.cryer.info
Managed Newsgroup Archive

Re: TIdTCPClient, TIdIOHandler and a Headache

Subject:Re: TIdTCPClient, TIdIOHandler and a Headache
Posted by:"Lloyd Kinsella" (lloydkinsel..@gmail.com)
Date:31 May 2006 15:37:45

OK here is my current code, excuse some of the var's and things as it's
still work in progress:

procedure THTTPProxy.TCPServerCommandGET(ASender: TIdCommand);
var
  Client: TIdTCPClient;
  Host, Document: string;
  Headers: TIdHeaderList;
  RemoteHeaders: TIdHeaderList;
  URI: TIdURI;
  PageSize, NumRead, BufSize: Int64;
  Buffer: TBytes;
  Stream: TMemoryStream;
begin
  ASender.PerformReply := False;

  Headers := TIdHeaderList.Create;
    try
      ASender.Context.Connection.IOHandler.Capture(Headers,'');

      Client := TIdTCPClient.Create(nil);
        try
          URI := TIdURI.Create(ASender.Params.Strings[0]);
            try
              Host := URI.Host;
              Document := URI.Path + URI.Document + URI.Params;

              Client.Host := Host;
              Client.Port := Sys.StrToInt(URI.Port,80);
            finally
              Sys.FreeAndNil(URI);
            end;

          Client.Connect;
            try
              Client.IOHandler.WriteLn('GET ' + Document + ' HTTP/1.0');
              //Client.IOHandler.WriteLn('Host: ' + Host);
              Client.IOHandler.Write(Headers);
              Client.IOHandler.WriteLn('');

              RemoteHeaders := TIdHeaderList.Create;
                try
                  Client.IOHandler.Capture(RemoteHeaders, '');

ASender.Context.Connection.IOHandler.Write(RemoteHeaders);
                  ASender.Context.Connection.IOHandler.WriteLn('');
                  PageSize :=
Sys.StrToInt(RemoteHeaders.Values['Content-Length'], -1) ;


        If PageSize > -1 then
        begin
            NumRead := PageSize;

            while NumRead > 0 do
            begin
                SetLength(Buffer, Max(NumRead, MaxBufSize));
                Client.IOHandler.ReadBytes(Buffer, Length(Buffer),
False);
                ASender.Context.Connection.IOHandler.Write(Buffer);
                Dec(NumRead, Length(Buffer));
            end;
        end else
        begin
            while Client.Connected do
            begin
                if Client.IOHandler.InputBuffer.Size < MaxBufSize then
                    Client.IOHandler.CheckForDataOnSource(1000);
                if Client.IOHandler.InputBuffer.Size > 0 then
                begin
                    NumRead := Max(Client.IOHandler.InputBuffer.Size,
MaxBufSize);
                    SetLength(Buffer,NumRead);
                    Client.IOHandler.InputBuffer.ExtractToBytes(Buffer,
NumRead, False);
                    ASender.Context.Connection.IOHandler.Write(Buffer);
                end;
            end;
        end;
    end;

                finally
                  Sys.FreeAndNil(RemoteHeaders);
                end;


            finally
              Client.Disconnect;
            end;

        finally
          Sys.FreeAndNil(Client);
        end;

    finally
      Sys.FreeAndNil(Headers);
    end;
end;

Remy Lebeau (TeamB) wrote:

>
> "Lloyd Kinsella" <lloydkinsella@gmail.com> wrote in message
> news:447e1275@newsgroups.borland.com...
>
> > Grrr still can't get this working.
>
> Then please show your actual code.
>
> > Your code doesn't work
>
> Just saying it doesn't work says nothing about the actual problem you
> are having.  Always provide specific details.
>
>
> Gambit


--

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive