Daniele Teti 2022-04-04 14:48:39 +02:00
parent d750b19e55
commit 52640cb1fb
5 changed files with 34 additions and 14 deletions

View File

@ -71,19 +71,25 @@ begin
FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
'/static2',
TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www2'),
'index.html',True,'UTF-8',
procedure(var PathInfo: String; var Allow: Boolean)
begin
// This rule disallow any .txt file
Allow := not PathInfo.EndsWith('.txt', True);
end)
TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www2'))
);
// FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
// '/',
// TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www2'))
// );
FMVC.AddMiddleware(TMVCStaticFilesMiddleware.Create(
'/static3',
TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www3'),
'index.html',True,'UTF-8',
procedure(const Context: TWebContext; var PathInfo: String; var Allow: Boolean)
begin
// This rule disallow any .txt file and translates file1.html into file2.html
Allow := not PathInfo.EndsWith('.txt', True);
if Allow and PathInfo.Contains('file1.html') then
PathInfo := PathInfo.Replace('file1.html','file2.html');
if not Allow then
begin
Context.Response.StatusCode := HTTP_STATUS.NotFound;
end;
end)
);
// To enable compression (deflate, gzip) just add this middleware as the last one
FMVC.AddMiddleware(TMVCCompressionMiddleware.Create);

View File

@ -0,0 +1,5 @@
<!doctype html>
<body>
<h2>File1</h2>
</body>
</html>

View File

@ -0,0 +1,5 @@
<!doctype html>
<body>
<h2>File2</h2>
</body>
</html>

View File

@ -0,0 +1,5 @@
<!doctype html>
<body>
<h2>File3</h2>
</body>
</html>

View File

@ -58,7 +58,7 @@ type
STATIC_FILES_CONTENT_CHARSET = TMVCConstants.DEFAULT_CONTENT_CHARSET;
end;
TMVCStaticFileRulesProc = reference to procedure(var PathInfo: String; var Allow: Boolean);
TMVCStaticFileRulesProc = reference to procedure(const Context: TWebContext; var PathInfo: String; var Handled: Boolean);
TMVCStaticFilesMiddleware = class(TInterfacedObject, IMVCMiddleware)
private
@ -242,10 +242,9 @@ begin
if Assigned(fRules) then
begin
lAllow := True;
fRules(lPathInfo, lAllow);
fRules(AContext, lPathInfo, lAllow);
if not lAllow then
begin
AContext.Response.StatusCode := HTTP_STATUS.NotFound;
AHandled := True;
Exit;
end;