Einzelnen Beitrag anzeigen

hinnack

Registriert seit: 18. Nov 2004
22 Beiträge
 
#11

Re: Proxy-Server leitet keine https-Seiten weiter

  Alt 29. Okt 2007, 07:31
stimmt, INDY10 will man nicht

Ich habe mich schon immer bei 9 gefragt, ob das je schon mal jemand benutzt hat - vieles ist einfach nur buggy, angefangen, etc...

du musst eine weiters Verb neben GET, POST, etc. unterstützen: CONNECT

darin mache ich folgendes:
Delphi-Quellcode:
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;

dazu braucht man:
Delphi-Quellcode:
   TidHTTPConnectProxy = class(TThread)
      private
         FClient: TidTCPServerConnection;
         FServer: TidTCPClient;
         FReadList: TList;
         FNetData: String;
         FConnectionHandle: TObject;
         FOutBoundHandle: TObject;

      protected
         procedure Execute; override;

      public
         constructor Create(Owner: TidTCPServerConnection);
         destructor Destroy; override;

         property Server: TidTCPClient read FServer write FServer;
         property ReadList: TList read FReadList write FReadList;
         property ConnectionHandle: TObject read FConnectionHandle write FConnectionHandle;
         property OutBoundHandle: TObject read FOutBoundHandle write FOutBoundHandle;
   end;
mit:
Delphi-Quellcode:
{ TidHTTPConnectProxy }

constructor TidHTTPConnectProxy.Create(Owner: TidTCPServerConnection);
begin
   FClient := Owner;
   FReadList := TList.Create;
   FServer := TIdTCPClient.Create(nil);
   inherited Create(true);
end;

destructor TidHTTPConnectProxy.Destroy;
begin
  FreeAndNil(FServer);
  FreeAndNil(FReadList);
  inherited;
end;

procedure TidHTTPConnectProxy.Execute;
begin
   while not Terminated do
   begin
      if (Server.Connected) and (FClient.Connected) then
      begin
         try
            FReadList.Clear;
            FReadList.Add(FConnectionHandle);
            FReadList.Add(FOutBoundHandle);

            if GStack.WSSelect(FReadList, nil, nil, IdTimeoutInfinite) > 0 then
            begin
               if FReadList.IndexOf(FConnectionHandle) > -1 then
               begin
                  // TODO: WSAECONNRESET (Exception [EIdSocketError] Socket Error # 10054 Connection reset by peer)
                  FNetData := FClient.CurrentReadBuffer;
                  if Length(FNetData) > 0 then
                  begin
                     Server.Write(FNetData);
                  end;
               end;
               if FReadList.IndexOf(FOutBoundHandle) > -1 then
               begin
                  FNetData := Server.CurrentReadBuffer;
                 if Length(FNetData)>0 then
                 begin
                     FClient.Write(FNetData);
                 end;
               end;
            end
            else
               System.WriteLn('seems nothing to do...');
         except
            Terminate;
         end;
      end
      else
      begin
         Terminate;
      end;
   end;
end;
die System.Writeln kannst du wegschmeissen, wenn du keine Console hast => ist aber immer gut zum debuggen...
  Mit Zitat antworten Zitat