unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IdAntiFreezeBase, IdAntiFreeze, IdBaseComponent, IdComponent,
IdTCPServer, IdCustomHTTPServer, IdHTTPServer, IdTCPConnection,
IdTCPClient, IdHTTP, StdCtrls;
type
TForm1 =
class(TForm)
IdHTTPServer1: TIdHTTPServer;
IdAntiFreeze1: TIdAntiFreeze;
Label1: TLabel;
ListBox1: TListBox;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure IdHTTPServer1CreatePostStream(ASender: TIdPeerThread;
var VPostStream: TStream);
procedure IdHTTPServer1CommandGet(AThread: TIdPeerThread;
ARequestInfo: TIdHTTPRequestInfo;
AResponseInfo: TIdHTTPResponseInfo);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.IdHTTPServer1CreatePostStream(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;
procedure TForm1.IdHTTPServer1CommandGet(AThread: TIdPeerThread; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
LHTTP: TIdHTTP;
LURL:
string;
LMethod: TIdHTTPMethod;
LSource, LDest: TStream;
begin
LURL := '
http://' + ARequestInfo.Host;
Label1.Caption := LURL;
Label2.Caption := LURL + '
?' + ARequestInfo.QueryParams;
Label3.Caption := ARequestInfo.Command;
begin
LSource :=
nil;
LDest :=
nil;
if ARequestInfo.Command = '
GET'
then
begin
LMethod := hmGet;
end
else
if ARequestInfo.Command = '
POST'
then
begin
LMethod := hmPost;
end
else
if ARequestInfo.Command = '
HEAD'
then
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;
if Length(ARequestInfo.QueryParams) > 0
then
begin
LURL := LURL + '
?' + ARequestInfo.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;
end.