[fpc-devel] Customize TRequest and TResponse

Joao Morais jcmoraisjr at gmail.com
Sun Feb 5 20:46:36 CET 2012


Hello fpc-devel list.

I need to implement some customizations to the request and response
classes of the fastcgi implementation -- maybe others as well -- but
afaics I cannot override such classes without copy/paste the whole
FCGI.ProcessRecord method.

A draft of the implementation I'd like to see in the fcl-web is
described below. Is it possible to implement? Perhaps 2.6 branch?

Joao Morais



1. New virtual methods used to create requests and responses:

--- src/base/custweb.pp (revision 20261)
+++ src/base/custweb.pp (workcopy)
@@ -108,6 +108,8 @@
     Procedure SetBaseURL(AModule : TCustomHTTPModule; Const
AModuleName : String; ARequest : TRequest); virtual;
     function GetApplicationURL(ARequest : TRequest): String; virtual;
     procedure ShowRequestException(R: TResponse; E: Exception); virtual;
+    function CreateRequest: TRequest; virtual; abstract;
+    function CreateResponse(ARequest: TRequest): TResponse; virtual; abstract;
     Procedure InitRequest(ARequest : TRequest); virtual;
     Procedure InitResponse(AResponse : TResponse); virtual;
     Function GetEmail : String; virtual;

2. Patch every fcl-web implementation in order to use the virtual
method instead of a hardcoded class:

--- src/base/custfcgi.pp        (revision 20261)
+++ src/base/custfcgi.pp        (workcopy)
@@ -119,6 +119,8 @@
     function Read_FCGIRecord : PFCGI_Header;
     function DataAvailable : Boolean;
   protected
+    function CreateRequest: TRequest; override;
+    function CreateResponse(ARequest: TRequest): TResponse; override;
     Function DoFastCGIRead(AHandle : THandle; Var ABuf; ACount :
Integer) : Integer; virtual;
     Function DoFastCGIWrite(AHandle : THandle; Const ABuf; ACount :
Integer) : Integer; virtual;
     function  ProcessRecord(AFCGI_Record: PFCGI_Header; out ARequest:
TRequest;  out AResponse: TResponse): boolean; virtual;
@@ -793,6 +795,16 @@
 end;
 {$endif}

+function TFCgiHandler.CreateRequest: TRequest;
+begin
+  Result := TFCGIRequest.Create;
+end;
+
+function TFCgiHandler.CreateResponse(ARequest: TRequest): TResponse;
+begin
+  Result := TFCGIResponse.Create(ARequest);
+end;
+
 function TFCgiHandler.DoFastCGIRead(AHandle: THandle; var ABuf;
ACount: Integer): Integer;
 begin
 {$ifdef windowspipe}
@@ -831,7 +843,7 @@
       end;
     assert(not assigned(FRequestsArray[ARequestID].Request));
     assert(not assigned(FRequestsArray[ARequestID].Response));
-    ATempRequest:=TFCGIRequest.Create;
+    ATempRequest:=CreateRequest as TFCGIRequest;
     InitRequest(ATempRequest);
     ATempRequest.RequestID:=ARequestID;
     ATempRequest.Handle:=FHandle;
@@ -848,7 +860,7 @@
   else if FRequestsArray[ARequestID].Request.ProcessFCGIRecord(AFCGI_Record)
then
     begin
     ARequest:=FRequestsArray[ARequestID].Request;
-    FRequestsArray[ARequestID].Response := TFCGIResponse.Create(ARequest);
+    FRequestsArray[ARequestID].Response := CreateResponse(ARequest)
as TFCGIResponse;
     InitResponse(FRequestsArray[ARequestID].Response);
     FRequestsArray[ARequestID].Response.ProtocolOptions:=Self.ProtocolOptions;
     FRequestsArray[ARequestID].Response.FOnWrite:=@DoFastCGIWrite;



More information about the fpc-devel mailing list