From d1832830a93032b632fe6d549e1298e56f9483a0 Mon Sep 17 00:00:00 2001 From: Matheus Henrique Date: Sun, 14 Apr 2024 14:10:35 -0300 Subject: [PATCH] fix: ignore registered route --- src/Horse.ServerStatic.pas | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Horse.ServerStatic.pas b/src/Horse.ServerStatic.pas index 001e59c..66e06e6 100644 --- a/src/Horse.ServerStatic.pas +++ b/src/Horse.ServerStatic.pas @@ -17,7 +17,7 @@ interface Horse, Horse.Commons; -function ServerStatic(PathRoot: String): THorseCallback; +function ServerStatic(PathRoot: String; RegisteredRoute: String = ''): THorseCallback; procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED(FPC)}TNextProc{$ELSE}TProc{$ENDIF}); implementation @@ -29,11 +29,14 @@ implementation System.Net.Mime, System.IOUtils, System.Types, System.StrUtils; {$ENDIF} -var Path: string; +var Path, RegRoute: string; -function ServerStatic(PathRoot: String): THorseCallback; +function ServerStatic(PathRoot, RegisteredRoute: String): THorseCallback; begin Path := PathRoot; + RegRoute := RegisteredRoute; + if not AnsiStartsStr('/', RegRoute) then + RegRoute := '/'+RegRoute; Result := {$IF DEFINED(FPC)}@Middleware{$ELSE}Middleware{$ENDIF}; end; @@ -58,11 +61,11 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED begin {$IF DEFINED(FPC)} LPath := ConcatPaths([GetCurrentDir]); - LFullPath := ConcatPaths([LPath,Req.RawWebRequest.PathInfo.Replace('/',PathDelim)]); + LFullPath := ConcatPaths([LPath,Req.RawWebRequest.PathInfo.Replace(RegRoute, EmptyStr).Replace('/',PathDelim)]); LFullPath := LFullPath.Replace(PathDelim+PathDelim,PathDelim); {$ELSE} LPath := TPath.Combine(TPath.GetLibraryPath,Path); - LFullPath := LPath + TPath.DirectorySeparatorChar + Req.RawWebRequest.PathInfo.Replace('/',TPath.DirectorySeparatorChar); + LFullPath := LPath + TPath.DirectorySeparatorChar + Req.RawWebRequest.PathInfo.Replace(RegRoute, EmptyStr).Replace('/',TPath.DirectorySeparatorChar); LFullPath := LFullPath.Replace(TPath.DirectorySeparatorChar+TPath.DirectorySeparatorChar,TPath.DirectorySeparatorChar); {$ENDIF} @@ -114,4 +117,4 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED Next; end; -end. +end. \ No newline at end of file