Thema: Delphi Webproxy

Einzelnen Beitrag anzeigen

endeffects

Registriert seit: 27. Jun 2004
450 Beiträge
 
#1

Webproxy

  Alt 25. Jul 2004, 09:04
hallo,

ich möchte mir einen web proxy server mit indy basteln und hab mich dazu
ein wenig im netz umgesehen, gefunden hab ich dazu eine lösung
mit der indy mappedport und eine mit der httpserver komponente

schön wäre es wenn der webproxy die requests in folgender form annimmt
http://proxysserver/www.test.de/index2.html

vom (meinem) reinen verständnis her halte ich eine lösung mit hilfe von
idmappedport für die bessere variante, da ich ansonsten mit hilfe von
idhttp jeden gestellten request an den webserver modifizieren
und dann weiterleiten müßte, welche variante würdet ihr bevorzugen?

request > webserver > idhttp > webseite
request > webserver > idmappedport > webseite

anbei noch ein codeschnippsel für die variante mit hilfe von idhttp,
hier steckt allerdings noch ein fehler drin, die variante für
mappedport gibts in den indy demos

für anregungen und ideen wäre ich sehr dankbar

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPServer, IdCustomHTTPServer,
  IdHTTPServer, IdTCPConnection, IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
    IdHTTPServer1: TIdHTTPServer;
    IdHTTP1: TIdHTTP;
    procedure IdHTTP1ServerCreatePostStream(ASender: TIdPeerThread);
    procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
  // event handler for the OnCreatePostStream event...
procedure TForm1.IdHTTP1ServerCreatePostStream(ASender: TIdPeerThread);
var VPostStream: TStream;
begin
        // creating our own stream so that TIdHTTPServer does not automatically
        // free the PostStream before the OnCommandGet event is triggered
        VPostStream := TMemoryStream.Create;
    end;

    // event handler for the On CommandGet event...
procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
        LHTTP: TIdHTTP;
        LURL: String;
        LMethod: TIdHTTPMethod;
        LSource, LDest: TStream;
begin
        LSource := nil;
        LDest := nil;

        if ARequestInfo.Command = 'GETthen begin
            LMethod := hmGet;
        end else if ARequestInfo.Command = 'POSTthen begin
            LMethod := hmPost;
        end else if ARequestInfo.Command = 'HEADthen begin
            LMethod := hmHead;
        end else begin
            AResponseInfo.ResponseNo := 501; // not implemented
            Exit;
        end;

        if LMethod <> hmHead then begin
            AResponseInfo.ContentStream := TMemoryStream.Create;
            if LMethod = hmPost then LSource := ARequestInfo.PostStream;
            LDest := AResponseInfo.ContentStream;
        end;

        LURL := 'http://' + ARequestInfo.Document;
        if Length(ARequest.QueryParams) > 0 then begin
            LURL := LURL + '?' + ARequest.QueryParams;
        end;

        LHTTP := TIdHTTP.Create(nil);
        try
            try
                LHTTP.DoRequest(LMethod, LURL, LSource, LDest);
            finally
                AResponseInfo.ResponseNo := LHTTP.Response.ResponseCode;
                AResponseInfo.ResponseText := LHTTP.Response.ResponseText;
                AResponseInfo.RawHeaders.Assign(LHTTP.Response.RawHeaders);
                // copy over anything else that is needed...
                LHTTP.Free;
            end;
        except end;
    end;

end.
  Mit Zitat antworten Zitat