delphimvcframework/lib/crosssocket/Net.CrossHttpRouter.pas

168 lines
4.6 KiB
ObjectPascal
Raw Normal View History

{******************************************************************************}
{ }
{ Delphi cross platform socket library }
{ }
{ Copyright (c) 2017 WiNDDRiVER(soulawing@gmail.com) }
{ }
{ Homepage: https://github.com/winddriver/Delphi-Cross-Socket }
{ }
{******************************************************************************}
unit Net.CrossHttpRouter;
interface
uses
Net.CrossHttpServer;
type
/// <summary>
/// ·<><C2B7>
/// </summary>
/// <remarks>
/// <20><><EFBFBD><EFBFBD> TCrossHttpServer.Route(), Get(), Post() <20><>
/// </remarks>
TNetCrossRouter = class
public
/// <summary>
/// <20><>̬<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
/// </summary>
/// <param name="ALocalDir">
/// <20><><EFBFBD><EFBFBD>Ŀ¼
/// </param>
class function &Static(const ALocalDir, AFileParamName: string): TCrossHttpRouterProc2; static;
/// <summary>
/// <20>ļ<EFBFBD><C4BC>б<EFBFBD>·<EFBFBD><C2B7>
/// </summary>
/// <param name="APath">
/// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7>, <20>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>Ŀ¼<C4BF>б<EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD>ж<EFBFBD>λ<EFBFBD><CEBB>·<EFBFBD><C2B7>
/// </param>
/// <param name="ALocalDir">
/// <20><><EFBFBD><EFBFBD>Ŀ¼
/// </param>
class function Dir(const APath, ALocalDir, ADirParamName: string): TCrossHttpRouterProc2; static;
/// <summary>
/// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ҳ<EFBFBD>ļ<EFBFBD><C4BC>ľ<EFBFBD>̬<EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7>
/// </summary>
/// <param name="ALocalDir">
/// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD>ҳ<EFBFBD>ļ<EFBFBD><C4BC>ı<EFBFBD><C4B1><EFBFBD>Ŀ¼
/// </param>
/// <param name="ADefIndexFiles">
/// Ĭ<>ϵ<EFBFBD><CFB5><EFBFBD>ҳ<EFBFBD>ļ<EFBFBD>,<2C><>˳<EFBFBD><CBB3>ѡ<EFBFBD><D1A1>,<2C><><EFBFBD>ҵ<EFBFBD><D2B5>ĸ<EFBFBD><C4B8><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>ĸ<EFBFBD>
/// </param>
class function Index(const ALocalDir, AFileParamName: string; const ADefIndexFiles: TArray<string>): TCrossHttpRouterProc2; static;
end;
implementation
uses
System.SysUtils,
System.Classes,
System.IOUtils,
Net.CrossHttpRouterDirUtils,
Net.CrossHttpUtils;
{ TNetCrossRouter }
class function TNetCrossRouter.Index(const ALocalDir, AFileParamName: string;
const ADefIndexFiles: TArray<string>): TCrossHttpRouterProc2;
var
LDefIndexFiles: TArray<string>;
begin
if (ADefIndexFiles <> nil) then
LDefIndexFiles := ADefIndexFiles
else
LDefIndexFiles := [
'index.html',
'main.html',
'index.js',
'main.js',
'index.htm',
'main.htm'
];
Result :=
procedure(const ARequest: ICrossHttpRequest; const AResponse: ICrossHttpResponse; var AHandled: Boolean)
var
LPath, LFile, LDefMainFile: string;
begin
LPath := ALocalDir;
LFile := ARequest.Params[AFileParamName];
if (LFile = '') then
begin
for LDefMainFile in LDefIndexFiles do
begin
LFile := TCrossHttpUtils.CombinePath(LPath, LDefMainFile);
if TFile.Exists(LFile) then
begin
AResponse.SendFile(LFile);
AHandled := True;
Exit;
end;
end;
end else
begin
LFile := TCrossHttpUtils.CombinePath(LPath, LFile);
if TFile.Exists(LFile) then
begin
AResponse.SendFile(LFile);
AHandled := True;
Exit;
end;
end;
AHandled := False;
end;
end;
class function TNetCrossRouter.Static(
const ALocalDir, AFileParamName: string): TCrossHttpRouterProc2;
begin
Result :=
procedure(const ARequest: ICrossHttpRequest; const AResponse: ICrossHttpResponse; var AHandled: Boolean)
var
LFile: string;
begin
AHandled := True;
LFile := TCrossHttpUtils.CombinePath(ALocalDir, ARequest.Params[AFileParamName]);
if (LFile = '') then
begin
AHandled := False;
Exit;
end;
LFile := TPath.GetFullPath(LFile);
AResponse.SendFile(LFile);
end;
end;
class function TNetCrossRouter.Dir(
const APath, ALocalDir, ADirParamName: string): TCrossHttpRouterProc2;
begin
Result :=
procedure(const ARequest: ICrossHttpRequest; const AResponse: ICrossHttpResponse; var AHandled: Boolean)
var
LFile: string;
begin
AHandled := True;
LFile := TCrossHttpUtils.CombinePath(ALocalDir, ARequest.Params[ADirParamName]);
if (LFile = '') then
begin
AHandled := False;
Exit;
end;
LFile := TPath.GetFullPath(LFile);
if (TDirectory.Exists(LFile)) then
AResponse.Send(BuildDirList(LFile, ARequest.Path, APath))
else if TFile.Exists(LFile) then
AResponse.SendFile(LFile)
else
AHandled := False;
end;
end;
end.