2007-05-05 19:05:01 +00:00
|
|
|
{
|
|
|
|
This file is part of the Web Service Toolkit
|
|
|
|
Copyright (c) 2006 by Inoussa OUEDRAOGO
|
|
|
|
|
|
|
|
This file is provide under modified LGPL licence
|
|
|
|
( the files COPYING.modifiedLGPL and COPYING.LGPL).
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
}
|
|
|
|
|
2007-07-13 22:33:55 +00:00
|
|
|
{$INCLUDE wst_global.inc}
|
|
|
|
{$IFDEF FPC}
|
|
|
|
//{$UNDEF INDY_9}
|
|
|
|
//{$DEFINE INDY_10}
|
|
|
|
{$ELSE}
|
|
|
|
//{$UNDEF INDY_10}
|
|
|
|
//{$DEFINE INDY_9}
|
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
|
2007-07-13 22:33:55 +00:00
|
|
|
unit indy_http_server;
|
2007-05-05 19:05:01 +00:00
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils,
|
|
|
|
IdCustomHTTPServer,
|
2007-07-13 22:33:55 +00:00
|
|
|
IdHTTPServer,
|
|
|
|
{$IFDEF INDY_10}
|
|
|
|
IdContext,
|
|
|
|
{$ENDIF}
|
|
|
|
{$IFDEF INDY_9}
|
|
|
|
IdTCPServer,
|
|
|
|
{$ENDIF}
|
2007-07-14 23:17:03 +00:00
|
|
|
IdSocketHandle,
|
2008-12-17 21:29:09 +00:00
|
|
|
server_listener, wst_types;
|
2007-05-05 19:05:01 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
{ TwstIndyHttpListener }
|
2007-05-05 19:05:01 +00:00
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
TwstIndyHttpListener = class(TwstListener)
|
2007-05-05 19:05:01 +00:00
|
|
|
private
|
|
|
|
FHTTPServerObject: TIdHTTPServer;
|
|
|
|
FRootAddress : string;
|
|
|
|
private
|
|
|
|
procedure ProcessWSDLRequest(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
AContext : TIdContext;
|
2007-07-13 22:33:55 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo;
|
|
|
|
var APath : string
|
|
|
|
);
|
|
|
|
procedure ProcessServiceRequest(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
AContext : TIdContext;
|
2007-07-13 22:33:55 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo;
|
|
|
|
var APath : string
|
|
|
|
);
|
|
|
|
private
|
|
|
|
procedure Handler_CommandGet(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
AContext : TIdContext;
|
2007-07-13 22:33:55 +00:00
|
|
|
{$ENDIF}
|
|
|
|
{$IFDEF INDY_9}
|
|
|
|
AThread: TIdPeerThread;
|
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo
|
|
|
|
);
|
|
|
|
public
|
2007-07-13 22:33:55 +00:00
|
|
|
constructor Create(
|
|
|
|
const AServerIpAddress : string = '127.0.0.1';
|
|
|
|
const AListningPort : Integer = 8000;
|
|
|
|
const ADefaultClientPort : Integer = 25000;
|
|
|
|
const AServerSoftware : string = 'Web Service Toolkit Application'
|
|
|
|
);
|
2007-05-05 19:05:01 +00:00
|
|
|
destructor Destroy(); override;
|
2007-07-14 23:17:03 +00:00
|
|
|
class function GetDescription() : string;override;
|
|
|
|
procedure Start();override;
|
|
|
|
procedure Stop();override;
|
2010-09-01 00:35:41 +00:00
|
|
|
function IsActive : Boolean; override;
|
2007-05-05 19:05:01 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
2007-07-18 11:23:56 +00:00
|
|
|
uses
|
2008-12-17 21:29:09 +00:00
|
|
|
{$IFDEF WST_DELPHI}
|
2007-07-18 11:23:56 +00:00
|
|
|
ActiveX,
|
2009-01-27 16:53:43 +00:00
|
|
|
{$ENDIF WST_DELPHI}
|
|
|
|
{$IFDEF INDY_9}
|
2007-07-20 00:25:29 +00:00
|
|
|
wst_indy9_utils,
|
2009-01-27 16:53:43 +00:00
|
|
|
{$ENDIF INDY_9}
|
|
|
|
{$IFDEF INDY_10}
|
2008-12-17 21:29:09 +00:00
|
|
|
wst_indy10_utils, IdSchedulerOfThread, IdSchedulerOfThreadDefault,
|
2009-01-27 16:53:43 +00:00
|
|
|
{$ENDIF}
|
2007-07-18 11:23:56 +00:00
|
|
|
base_service_intf,
|
|
|
|
server_service_intf, server_service_imputils,
|
|
|
|
metadata_wsdl;
|
2007-05-05 19:05:01 +00:00
|
|
|
|
2007-12-19 23:31:52 +00:00
|
|
|
{$IFDEF WST_DBG}
|
|
|
|
procedure Display(const AMsg : string);
|
|
|
|
begin
|
|
|
|
if IsConsole then
|
|
|
|
WriteLn(AMsg);
|
|
|
|
end;
|
|
|
|
{$ENDIF}
|
|
|
|
|
2007-05-05 19:05:01 +00:00
|
|
|
function ExtractNextPathElement(var AFullPath : string):string;
|
|
|
|
var
|
|
|
|
i : SizeInt;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
if ( Length(AFullPath) > 0 ) then begin
|
|
|
|
while ( Length(AFullPath) > 0 ) and ( AFullPath[1] = sSEPARATOR ) do begin
|
|
|
|
Delete(AFullPath,1,1);
|
|
|
|
end;
|
|
|
|
i := Pos(sSEPARATOR,AFullPath);
|
|
|
|
if ( i < 1 ) then begin
|
|
|
|
Result := AFullPath;
|
|
|
|
AFullPath := '';
|
|
|
|
end else begin
|
|
|
|
Result := Copy(AFullPath,1,Pred(i));
|
|
|
|
Delete(AFullPath,1,i);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
{ TwstIndyHttpListener }
|
2007-05-05 19:05:01 +00:00
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
procedure TwstIndyHttpListener.ProcessWSDLRequest(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
AContext : TIdContext;
|
2007-07-13 22:33:55 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo;
|
|
|
|
var APath : string
|
|
|
|
);
|
|
|
|
var
|
|
|
|
locRepName, strBuff : string;
|
|
|
|
i : Integer;
|
|
|
|
begin
|
|
|
|
locRepName := ExtractNextPathElement(APath);
|
|
|
|
if AnsiSameText(sWSDL,locRepName) then
|
|
|
|
locRepName := ExtractNextPathElement(APath);
|
2007-07-18 11:23:56 +00:00
|
|
|
strBuff := GenerateWSDL(locRepName,FRootAddress);
|
2007-05-05 19:05:01 +00:00
|
|
|
i := Length(strBuff);
|
|
|
|
if ( i > 0 ) then begin
|
|
|
|
AResponseInfo.ContentType := 'text/xml';
|
|
|
|
if not Assigned(AResponseInfo.ContentStream) then
|
|
|
|
AResponseInfo.ContentStream := TMemoryStream.Create();
|
|
|
|
AResponseInfo.ContentStream.Write(strBuff[1],i);
|
|
|
|
Exit;
|
|
|
|
end;
|
2007-07-18 11:23:56 +00:00
|
|
|
AResponseInfo.ContentText := GenerateWSDLHtmlTable();
|
2007-05-05 19:05:01 +00:00
|
|
|
AResponseInfo.ContentType := 'text/html';
|
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
procedure TwstIndyHttpListener.ProcessServiceRequest(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
AContext : TIdContext;
|
2007-07-13 22:33:55 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo;
|
|
|
|
var APath : string
|
|
|
|
);
|
|
|
|
var
|
2007-06-28 23:33:38 +00:00
|
|
|
trgt,ctntyp, frmt : string;
|
2007-05-05 19:05:01 +00:00
|
|
|
rqst : IRequestBuffer;
|
2008-09-06 15:50:08 +00:00
|
|
|
inStream : TMemoryStream;
|
2007-05-05 19:05:01 +00:00
|
|
|
begin
|
|
|
|
trgt := ExtractNextPathElement(APath);
|
|
|
|
if AnsiSameText(sWSDL,trgt) then begin
|
2007-07-13 22:33:55 +00:00
|
|
|
ProcessWSDLRequest({$IFDEF INDY_10}AContext,{$ENDIF}ARequestInfo,AResponseInfo,APath);
|
2007-05-05 19:05:01 +00:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
inStream := nil;
|
|
|
|
try
|
|
|
|
try
|
2008-09-06 15:50:08 +00:00
|
|
|
inStream := TMemoryStream.Create();
|
2007-05-05 19:05:01 +00:00
|
|
|
AResponseInfo.ContentStream := TMemoryStream.Create();
|
|
|
|
|
|
|
|
ctntyp := ARequestInfo.ContentType;
|
|
|
|
inStream.CopyFrom(ARequestInfo.PostStream,0);
|
|
|
|
inStream.Position := 0;
|
|
|
|
AResponseInfo.ContentType := ctntyp;
|
2007-06-28 23:33:38 +00:00
|
|
|
frmt := Trim(ARequestInfo.Params.Values['format']);
|
|
|
|
rqst := TRequestBuffer.Create(trgt,ctntyp,inStream,AResponseInfo.ContentStream,frmt);
|
2008-09-25 02:24:25 +00:00
|
|
|
rqst.GetPropertyManager().SetProperty(sREMOTE_IP,ARequestInfo.RemoteIP);
|
2007-05-05 19:05:01 +00:00
|
|
|
HandleServiceRequest(rqst);
|
|
|
|
finally
|
|
|
|
inStream.Free();
|
|
|
|
end;
|
|
|
|
except
|
|
|
|
on e : Exception do begin
|
2007-07-14 23:17:03 +00:00
|
|
|
NotifyMessage('ProcessData()>> Exception = '+e.Message);
|
2007-05-05 19:05:01 +00:00
|
|
|
raise;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
procedure TwstIndyHttpListener.Handler_CommandGet(
|
2007-07-13 22:33:55 +00:00
|
|
|
{$IFDEF INDY_10}
|
|
|
|
AContext : TIdContext;
|
|
|
|
{$ENDIF}
|
|
|
|
{$IFDEF INDY_9}
|
|
|
|
AThread: TIdPeerThread;
|
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
ARequestInfo : TIdHTTPRequestInfo;
|
|
|
|
AResponseInfo : TIdHTTPResponseInfo
|
|
|
|
);
|
|
|
|
var
|
2007-07-14 23:17:03 +00:00
|
|
|
{$IFDEF WST_DBG}
|
|
|
|
s : string;
|
2007-08-19 00:29:43 +00:00
|
|
|
j : SizeInt;
|
2007-07-14 23:17:03 +00:00
|
|
|
{$ENDIF}
|
|
|
|
locPath, locPathPart : string;
|
2007-05-05 19:05:01 +00:00
|
|
|
begin
|
2007-07-14 23:17:03 +00:00
|
|
|
{$IFDEF WST_DBG}
|
2007-05-05 19:05:01 +00:00
|
|
|
if Assigned(ARequestInfo.PostStream) and ( ARequestInfo.PostStream.Size > 0 ) then begin
|
|
|
|
j := ARequestInfo.PostStream.Size;
|
|
|
|
SetLength(s,j);
|
|
|
|
ARequestInfo.PostStream.Read(s[1],j);
|
2007-07-14 23:17:03 +00:00
|
|
|
NotifyMessage('----------- QUERY ----------------------');
|
2007-05-05 19:05:01 +00:00
|
|
|
Display(s);
|
|
|
|
end;
|
2007-07-14 23:17:03 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
locPath := ARequestInfo.Document;
|
|
|
|
locPathPart := ExtractNextPathElement(locPath);
|
|
|
|
if AnsiSameText(sSERVICES_PREFIXE,locPathPart) then begin
|
2007-07-13 22:33:55 +00:00
|
|
|
ProcessServiceRequest({$IFDEF INDY_10}AContext,{$ENDIF}ARequestInfo,AResponseInfo,locPath);
|
2007-07-14 23:17:03 +00:00
|
|
|
{$IFDEF WST_DBG}
|
2007-05-05 19:05:01 +00:00
|
|
|
if Assigned(AResponseInfo.ContentStream) and ( AResponseInfo.ContentStream.Size > 0 ) then begin
|
|
|
|
j := AResponseInfo.ContentStream.Size;
|
|
|
|
SetLength(s,j);
|
|
|
|
AResponseInfo.ContentStream.Position := 0;
|
|
|
|
AResponseInfo.ContentStream.Read(s[1],j);
|
|
|
|
Display('--------- RESPONSE ------------------------');
|
|
|
|
Display(s);
|
|
|
|
end;
|
2007-07-14 23:17:03 +00:00
|
|
|
{$ENDIF}
|
2007-05-05 19:05:01 +00:00
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
|
2007-07-13 22:33:55 +00:00
|
|
|
ProcessWSDLRequest({$IFDEF INDY_10}AContext,{$ENDIF}ARequestInfo,AResponseInfo,locPath);
|
2007-05-05 19:05:01 +00:00
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
constructor TwstIndyHttpListener.Create(
|
2007-07-13 22:33:55 +00:00
|
|
|
const AServerIpAddress : string;
|
|
|
|
const AListningPort : Integer;
|
|
|
|
const ADefaultClientPort : Integer;
|
|
|
|
const AServerSoftware : string
|
|
|
|
);
|
2007-05-05 19:05:01 +00:00
|
|
|
var
|
|
|
|
b : TIdSocketHandle;
|
|
|
|
begin
|
|
|
|
inherited Create();
|
2007-07-20 00:25:29 +00:00
|
|
|
FHTTPServerObject := TIdHTTPServer.Create({$IFDEF INDY_9}nil{$ENDIF});
|
|
|
|
{$IFDEF INDY_9}
|
2007-07-13 22:33:55 +00:00
|
|
|
FHTTPServerObject.ThreadClass := TwstIndy9Thread;
|
2008-12-17 21:29:09 +00:00
|
|
|
{$ENDIF INDY_9}
|
|
|
|
{$IFDEF INDY_10}
|
|
|
|
FHTTPServerObject.Scheduler := TIdSchedulerOfThreadDefault.Create(FHTTPServerObject);
|
|
|
|
TIdSchedulerOfThread(FHTTPServerObject.Scheduler).ThreadClass := TwstIndy10Thread;
|
|
|
|
{$ENDIF INDY_10}
|
2007-05-05 19:05:01 +00:00
|
|
|
b := FHTTPServerObject.Bindings.Add();
|
2007-07-13 22:33:55 +00:00
|
|
|
b.IP := AServerIpAddress;
|
|
|
|
b.port := AListningPort;
|
|
|
|
FRootAddress := Format('http://%s:%d/',[AServerIpAddress,AListningPort]);
|
|
|
|
|
|
|
|
FHTTPServerObject.DefaultPort := ADefaultClientPort;
|
|
|
|
FHTTPServerObject.ServerSoftware := AServerSoftware;
|
2007-07-14 23:17:03 +00:00
|
|
|
//FHTTPServerObject.Active := True;
|
2007-07-13 22:33:55 +00:00
|
|
|
FHTTPServerObject.OnCommandGet := {$IFDEF FPC}@{$ENDIF}Handler_CommandGet;
|
2007-05-05 19:05:01 +00:00
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
destructor TwstIndyHttpListener.Destroy();
|
2007-05-05 19:05:01 +00:00
|
|
|
begin
|
2007-07-14 23:17:03 +00:00
|
|
|
if ( FHTTPServerObject <> nil ) then
|
|
|
|
Stop();
|
2007-05-05 19:05:01 +00:00
|
|
|
FreeAndNil(FHTTPServerObject);
|
|
|
|
inherited Destroy();
|
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
procedure TwstIndyHttpListener.Start();
|
2007-07-13 22:33:55 +00:00
|
|
|
begin
|
|
|
|
if not FHTTPServerObject.Active then
|
|
|
|
FHTTPServerObject.Active := True;
|
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
procedure TwstIndyHttpListener.Stop();
|
2007-07-13 22:33:55 +00:00
|
|
|
begin
|
|
|
|
if FHTTPServerObject.Active then
|
|
|
|
FHTTPServerObject.Active := False;
|
2007-05-05 19:05:01 +00:00
|
|
|
end;
|
|
|
|
|
2007-07-14 23:17:03 +00:00
|
|
|
class function TwstIndyHttpListener.GetDescription: string;
|
|
|
|
begin
|
2007-07-20 00:25:29 +00:00
|
|
|
Result := 'WST Indy HTTP Listener';
|
2007-07-14 23:17:03 +00:00
|
|
|
end;
|
|
|
|
|
2010-09-01 00:35:41 +00:00
|
|
|
function TwstIndyHttpListener.IsActive: Boolean;
|
|
|
|
begin
|
|
|
|
Result := FHTTPServerObject.Active;
|
|
|
|
end;
|
|
|
|
|
2007-05-05 19:05:01 +00:00
|
|
|
initialization
|
2007-07-13 22:33:55 +00:00
|
|
|
|
|
|
|
|
2007-05-05 19:05:01 +00:00
|
|
|
end.
|