-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMiniREST.RequestInfo.pas
87 lines (76 loc) · 2.33 KB
/
MiniREST.RequestInfo.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
unit MiniREST.RequestInfo;
interface
uses Classes, SysUtils, MiniREST.Intf, MiniREST.Common, IdCustomHTTPServer;
type
TMiniRESTRequestInfo = class(TInterfacedObject, IMiniRESTRequestInfo)
private
FPathLength : Integer;
FRequestMethod : TMiniRESTRequestMethod;
FPathParams : TArray<string>;
function ParseMappingtoPathParams(AMapping : string) : TArray<string>;
public
constructor Create(AMapping : string; ARequestMethod : TMiniRESTRequestMethod);
function GetPathLength: Integer;
function GetRequestMethod: TMiniRESTRequestMethod;
function IsMatch(AMapping: string; ARequestMethod : TMiniRESTRequestMethod): Boolean;
end;
implementation
{ TMiniRESTRequestInfo }
constructor TMiniRESTRequestInfo.Create(AMapping: string;
ARequestMethod: TMiniRESTRequestMethod);
begin
FRequestMethod := ARequestMethod;
FPathParams := ParseMappingtoPathParams(AMapping);
FPathLength := Length(FPathParams);
end;
function TMiniRESTRequestInfo.GetPathLength: Integer;
begin
Result := FPathLength;
end;
function TMiniRESTRequestInfo.GetRequestMethod: TMiniRESTRequestMethod;
begin
Result := FRequestMethod;
end;
function TMiniRESTRequestInfo.IsMatch(AMapping: string; ARequestMethod : TMiniRESTRequestMethod): Boolean;
var LPath : TStringList;
LPathParams : TArray<string>;
I : Integer;
begin
Result := True;
if FRequestMethod <> ARequestMethod then
Exit(False);
LPathParams := ParseMappingtoPathParams(AMapping);
if Length(LPathParams) <> FPathLength then
Exit(False);
for I := 0 to FPathLength - 1 do
begin
if (FPathParams[I] = LPathParams[I]) or (Pos('{', LPathParams[I]) > 0 {LPathParams[I].IndexOf('{') > -1}) then
Continue
else
Exit(False);
end;
end;
function TMiniRESTRequestInfo.ParseMappingtoPathParams(
AMapping: string): TArray<string>;
var LPath : TStringList;
I : Integer;
S : string;
begin
SetLength(Result, 0);
LPath := TStringList.Create; { TODO : Refatorar: Mandar para classe utilitária }
LPath.StrictDelimiter := True;
LPath.Delimiter := '/';
try
LPath.DelimitedText := AMapping;
for I := 0 to LPath.Count - 1 do
if Trim(LPath[I]) <> '' then
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := LPath[I];
//Result := Result + [LPath[I]];
end;
finally
LPath.Free;
end;
end;
end.