From 1cd501229f7575e445a9fc63b9a937256bf58e9e Mon Sep 17 00:00:00 2001 From: michael Date: Thu, 26 Sep 2019 13:54:23 +0000 Subject: [PATCH] * Allow max-age for cache control git-svn-id: trunk@43081 - --- packages/fcl-web/src/base/fpwebfile.pp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/fcl-web/src/base/fpwebfile.pp b/packages/fcl-web/src/base/fpwebfile.pp index 31fbaf1735..5dc5e7cde7 100644 --- a/packages/fcl-web/src/base/fpwebfile.pp +++ b/packages/fcl-web/src/base/fpwebfile.pp @@ -22,7 +22,12 @@ interface uses SysUtils, Classes, httpdefs, fphttp, httproute; Type + + { TFPCustomFileModule } + TFPCustomFileModule = Class(TCustomHTTPModule) + private + FCacheControlMaxAge: Integer; Protected // Determine filename frome request. Function GetRequestFileName(Const ARequest : TRequest) : String; virtual; @@ -33,8 +38,10 @@ Type // Actually Send file to client. Procedure SendFile(Const AFileName : String; AResponse : TResponse); virtual; Public + Constructor CreateNew(AOwner: TComponent; CreateMode: Integer); override; overload; // Overrides TCustomHTTPModule to implement file serving. Procedure HandleRequest(ARequest : TRequest; AResponse : TResponse); override; + Property CacheControlMaxAge : Integer Read FCacheControlMaxAge Write FCacheControlMaxAge; end; TFPCustomFileModuleClass = Class of TFPCustomFileModule; @@ -69,6 +76,7 @@ Var DefaultFileModuleClass : TFPCustomFileModuleClass = TFPCustomFileModule; // Setting this will load mime types from that file. MimeTypesFile : string; + DefaultCacheControlMaxAge : Integer = 0; // use this to map locations (relative to BaseURL of the application) to physical directories. // More than one location can be registered. Directory must exist, location must not have / or \ @@ -250,6 +258,8 @@ begin AResponse.ContentType:=MimeTypes.GetMimeType(ExtractFileExt(AFileName)); If (AResponse.ContentType='') then AResponse.ContentType:='Application/octet-stream'; + if CacheControlMaxAge>0 then + aResponse.CacheControl:=Format('max-age=%d',[CacheControlMaxAge]); F:=TFileStream.Create(AFileName,fmOpenRead or fmShareDenyWrite); try AResponse.ContentLength:=F.Size; @@ -261,6 +271,12 @@ begin end; end; +constructor TFPCustomFileModule.CreateNew(AOwner: TComponent; CreateMode: Integer); +begin + inherited CreateNew(aOwner,CreateMode); + CacheControlMaxAge:=DefaultCacheControlMaxAge; +end; + Procedure TFPCustomFileModule.HandleRequest(ARequest : TRequest; AResponse : TResponse);