Skip to content

Commit

Permalink
Merge pull request #8 from matheush9/master
Browse files Browse the repository at this point in the history
fix: ignore registered route
Thanks for the contribution
  • Loading branch information
cachopaweb authored Apr 26, 2024
2 parents 1cbe1e2 + d183283 commit 9fb83e9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Horse.ServerStatic.pas
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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}

Expand Down Expand Up @@ -114,4 +117,4 @@ procedure Middleware(Req: THorseRequest; Res: THorseResponse; Next: {$IF DEFINED
Next;
end;

end.
end.

0 comments on commit 9fb83e9

Please # to comment.