procedure TidHTTPService.CommandConnect(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo);
var
TCP: TidHTTPConnectProxy;
LCloseConnection: Boolean;
ConnectStr:
String;
Host:
String;
Port: Integer;
begin
TCP := TidHTTPConnectProxy.Create(AThread.Connection);
if FConnectForwardingHost <> '
'
then
begin
Host := FConnectForwardingHost;
Port := FConnectForwardingPort;
end
else
begin
Host := Copy(ARequestInfo.Document, 1, Pos('
:', ARequestInfo.Document) - 1);
Port := StrToInt( Copy(ARequestInfo.Document, Pos('
:', ARequestInfo.Document) + 1, MAXINT));
end;
if Assigned(OnBeforeConnect)
then
OnBeforeConnect(ARequestInfo, Host, Port);
TCP.Server.Host := Host;
TCP.Server.Port := Port;
LCloseConnection := false;
ConnectStr :=
TCP.Server.Host + '
:' + IntToStr(
TCP.Server.Port);
System.Writeln('
CONNECT ' + ConnectStr);
with AThread.Connection
do
try
try
TCP.Server.Connect;
OpenWriteBuffer;
try
WriteLn('
HTTP/1.1 200 Connection established');
WriteLn('
Proxy-agent: ' + ServerSoftware);
WriteLn;
finally
CloseWriteBuffer;
end;
except
on E:
Exception do
begin
OpenWriteBuffer;
try
WriteLn('
HTTP/1.1 502 Bad Gateway');
WriteLn('
Proxy-agent: ' + ServerSoftware);
WriteLn;
WriteLn('
Server unreachable ' + e.
Message);
TCP.Terminate;
Exit;
finally
CloseWriteBuffer;
end;
end;
end;
TCP.ConnectionHandle := TObject((AThread.Connection.IOHandler
as TIdIOHandlerSocket).Binding.Handle);
TCP.OutBoundHandle := TObject((
TCP.Server.IOHandler
as TIdIOHandlerSocket).Binding.Handle);
TCP.Resume;
repeat
{$IFDEF MSWINDOWS}
SleepEx(10, true);
{$ELSE}
Sleep(10);
{$ENDIF}
until (LCloseConnection)
or (
TCP.Terminated)
or (
not TCP.Server.Connected)
or (
not AThread.Connection.Connected);
finally
TCP.Server.Disconnect;
while not TCP.Terminated
do
{$IFDEF MSWINDOWS}
SleepEx(5, true);
{$ENDIF}
{$IFDEF LINUX}
Sleep(5);
{$ENDIF}
FreeAndNil(
TCP);
AThread.Connection.Disconnect;
System.Writeln('
DISCONNECT ' + ConnectStr);
end;
end;