* Allow max-age for cache control

git-svn-id: trunk@43081 -
This commit is contained in:
michael 2019-09-26 13:54:23 +00:00
parent 413b5adb59
commit 1cd501229f

View File

@ -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);