2016-06-22 17:49:16 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
2017-03-13 20:52:11 +01:00
|
|
|
|
// Collaborators on this file: Ezequiel Juliano M<>ller (ezequieljuliano@gmail.com)
|
|
|
|
|
//
|
2016-06-22 17:49:16 +02:00
|
|
|
|
// ***************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
unit MVCFramework.Router;
|
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
2017-03-13 20:52:11 +01:00
|
|
|
|
System.Rtti,
|
|
|
|
|
System.SysUtils,
|
|
|
|
|
System.Generics.Collections,
|
|
|
|
|
System.RegularExpressions,
|
2013-10-30 00:48:23 +01:00
|
|
|
|
MVCFramework,
|
2017-03-13 20:52:11 +01:00
|
|
|
|
MVCFramework.Commons,
|
|
|
|
|
IdURI;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
|
|
type
|
2017-06-02 00:10:31 +02:00
|
|
|
|
TMVCActionParamCacheItem = class
|
|
|
|
|
private
|
|
|
|
|
FValue: string;
|
|
|
|
|
FParams: TList<string>;
|
|
|
|
|
public
|
|
|
|
|
constructor Create(aValue: string; aParams: TList<string>); virtual;
|
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
function Value: string;
|
|
|
|
|
function Params: TList<string>; // this should be read-only...
|
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
2020-02-26 13:10:41 +01:00
|
|
|
|
TMVCRouter = class(TMVCCustomRouter)
|
2013-10-30 00:48:23 +01:00
|
|
|
|
private
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FRttiContext: TRttiContext;
|
|
|
|
|
FConfig: TMVCConfig;
|
|
|
|
|
FMethodToCall: TRttiMethod;
|
|
|
|
|
FControllerClazz: TMVCControllerClazz;
|
|
|
|
|
FControllerCreateAction: TMVCControllerCreateAction;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
FActionParamsCache: TMVCStringObjectDictionary<TMVCActionParamCacheItem>;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function GetAttribute<T: TCustomAttribute>(const AAttributes: TArray<TCustomAttribute>): T;
|
|
|
|
|
function GetFirstMediaType(const AContentType: string): string;
|
|
|
|
|
|
|
|
|
|
function IsHTTPContentTypeCompatible(
|
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
var AContentType: string;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
|
|
|
|
|
|
|
|
|
function IsHTTPAcceptCompatible(
|
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
var AAccept: string;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
|
|
|
|
|
|
|
|
|
function IsHTTPMethodCompatible(
|
|
|
|
|
const AMethodType: TMVCHTTPMethodType;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
|
|
|
|
|
|
|
|
|
function IsCompatiblePath(
|
|
|
|
|
const AMVCPath: string;
|
|
|
|
|
const APath: string;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
var aParams: TMVCRequestParamsTable): Boolean;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
function GetParametersNames(const V: string): TList<string>;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
protected
|
2017-06-02 00:10:31 +02:00
|
|
|
|
function GetControllerMappedPath(
|
|
|
|
|
const aControllerName: string;
|
|
|
|
|
const aControllerAttributes: TArray<TCustomAttribute>): string;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
public
|
2020-09-16 15:56:14 +02:00
|
|
|
|
class function StringMethodToHTTPMetod(const aValue: string): TMVCHTTPMethodType; static;
|
|
|
|
|
constructor Create(const aConfig: TMVCConfig;
|
|
|
|
|
const aActionParamsCache: TMVCStringObjectDictionary<TMVCActionParamCacheItem>);
|
2017-03-13 20:52:11 +01:00
|
|
|
|
destructor Destroy; override;
|
2021-02-03 16:04:36 +01:00
|
|
|
|
function ExecuteRouting(const ARequestPathInfo: string;
|
2021-05-19 21:17:47 +02:00
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
const ARequestContentType, ARequestAccept: string;
|
|
|
|
|
const AControllers: TObjectList<TMVCControllerDelegate>;
|
|
|
|
|
const ADefaultContentType: string;
|
|
|
|
|
const ADefaultContentCharset: string;
|
|
|
|
|
const APathPrefix: string;
|
|
|
|
|
var ARequestParams: TMVCRequestParamsTable;
|
|
|
|
|
out AResponseContentMediaType: string;
|
|
|
|
|
out AResponseContentCharset: string): Boolean;
|
2020-02-26 13:10:41 +01:00
|
|
|
|
function GetQualifiedActionName: string; override;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
|
|
|
|
property MethodToCall: TRttiMethod read FMethodToCall;
|
|
|
|
|
property ControllerClazz: TMVCControllerClazz read FControllerClazz;
|
|
|
|
|
property ControllerCreateAction: TMVCControllerCreateAction read FControllerCreateAction;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
2018-08-08 17:11:45 +02:00
|
|
|
|
uses
|
2021-05-19 21:17:47 +02:00
|
|
|
|
System.TypInfo,
|
|
|
|
|
System.NetEncoding;
|
2018-08-08 17:11:45 +02:00
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
{ TMVCRouter }
|
|
|
|
|
|
2020-09-16 15:56:14 +02:00
|
|
|
|
constructor TMVCRouter.Create(const aConfig: TMVCConfig;
|
|
|
|
|
const aActionParamsCache: TMVCStringObjectDictionary<TMVCActionParamCacheItem>);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FRttiContext := TRttiContext.Create;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
FConfig := aConfig;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FMethodToCall := nil;
|
|
|
|
|
FControllerClazz := nil;
|
|
|
|
|
FControllerCreateAction := nil;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
FActionParamsCache := aActionParamsCache;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCRouter.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
FRttiContext.Free;
|
|
|
|
|
inherited Destroy;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.ExecuteRouting(const ARequestPathInfo: string;
|
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
const ARequestContentType, ARequestAccept: string;
|
|
|
|
|
const AControllers: TObjectList<TMVCControllerDelegate>;
|
|
|
|
|
const ADefaultContentType: string;
|
|
|
|
|
const ADefaultContentCharset: string;
|
2021-02-03 16:04:36 +01:00
|
|
|
|
const APathPrefix: string;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
var ARequestParams: TMVCRequestParamsTable;
|
2017-09-08 16:59:02 +02:00
|
|
|
|
out AResponseContentMediaType: string;
|
|
|
|
|
out AResponseContentCharset: string): Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LRequestPathInfo: string;
|
|
|
|
|
LRequestAccept: string;
|
|
|
|
|
LRequestContentType: string;
|
|
|
|
|
LControllerMappedPath: string;
|
|
|
|
|
LControllerDelegate: TMVCControllerDelegate;
|
|
|
|
|
LAttributes: TArray<TCustomAttribute>;
|
|
|
|
|
LAtt: TCustomAttribute;
|
|
|
|
|
LRttiType: TRttiType;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
LMethods: TArray<TRttiMethod>;
|
|
|
|
|
LMethod: TRttiMethod;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LMethodPath: string;
|
|
|
|
|
LProduceAttribute: MVCProducesAttribute;
|
2017-09-24 19:40:40 +02:00
|
|
|
|
lURLSegment: string;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
// JUST FOR DEBUG
|
|
|
|
|
// lMethodCompatible: Boolean;
|
|
|
|
|
// lContentTypeCompatible: Boolean;
|
|
|
|
|
// lAcceptCompatible: Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
Result := False;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FMethodToCall := nil;
|
|
|
|
|
FControllerClazz := nil;
|
|
|
|
|
FControllerCreateAction := nil;
|
|
|
|
|
|
|
|
|
|
LRequestAccept := ARequestAccept;
|
|
|
|
|
LRequestContentType := ARequestContentType;
|
|
|
|
|
LRequestPathInfo := ARequestPathInfo;
|
|
|
|
|
if (Trim(LRequestPathInfo) = EmptyStr) then
|
|
|
|
|
LRequestPathInfo := '/'
|
2013-10-30 00:48:23 +01:00
|
|
|
|
else
|
|
|
|
|
begin
|
2019-05-02 17:38:57 +02:00
|
|
|
|
if not LRequestPathInfo.StartsWith('/') then
|
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LRequestPathInfo := '/' + LRequestPathInfo;
|
2019-05-02 17:38:57 +02:00
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
2021-08-11 00:21:04 +02:00
|
|
|
|
//LRequestPathInfo := TNetEncoding.URL.EncodePath(LRequestPathInfo, [Ord('$')]);
|
|
|
|
|
LRequestPathInfo := TIdURI.PathEncode(LRequestPathInfo); //regression introduced in fix for issue 492
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2019-05-08 20:20:14 +02:00
|
|
|
|
TMonitor.Enter(gLock);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
try
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LControllerMappedPath := EmptyStr;
|
|
|
|
|
for LControllerDelegate in AControllers do
|
2014-04-01 00:02:31 +02:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
SetLength(LAttributes, 0);
|
|
|
|
|
LRttiType := FRttiContext.GetType(LControllerDelegate.Clazz.ClassInfo);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-09-24 19:40:40 +02:00
|
|
|
|
lURLSegment := LControllerDelegate.URLSegment;
|
|
|
|
|
if lURLSegment.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
LAttributes := LRttiType.GetAttributes;
|
|
|
|
|
if (LAttributes = nil) then
|
|
|
|
|
Continue;
|
|
|
|
|
LControllerMappedPath := GetControllerMappedPath(LRttiType.Name, LAttributes);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
LControllerMappedPath := lURLSegment;
|
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if (LControllerMappedPath = '/') then
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LControllerMappedPath := '';
|
2019-05-02 17:38:57 +02:00
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2021-07-16 23:55:33 +02:00
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
2021-02-03 16:04:36 +01:00
|
|
|
|
if not LRequestPathInfo.StartsWith(APathPrefix + LControllerMappedPath, True) then
|
2021-07-16 23:55:33 +02:00
|
|
|
|
{$ELSE}
|
|
|
|
|
if not TMVCStringHelper.StartsWith(APathPrefix + LControllerMappedPath, LRequestPathInfo, True) then
|
|
|
|
|
{$ENDIF}
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2014-04-01 00:02:31 +02:00
|
|
|
|
Continue;
|
2019-05-02 17:38:57 +02:00
|
|
|
|
end;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
|
2020-09-16 15:56:14 +02:00
|
|
|
|
LMethods := LRttiType.GetMethods; { do not use GetDeclaredMethods because JSON-RPC rely on this!! }
|
2017-03-13 20:52:11 +01:00
|
|
|
|
for LMethod in LMethods do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if LMethod.Visibility <> mvPublic then // 2020-08-08
|
2020-08-08 01:03:37 +02:00
|
|
|
|
Continue;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if (LMethod.MethodKind <> mkProcedure) { or LMethod.IsClassMethod } then
|
2018-08-08 17:11:45 +02:00
|
|
|
|
Continue;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LAttributes := LMethod.GetAttributes;
|
2020-08-08 01:03:37 +02:00
|
|
|
|
if Length(LAttributes) = 0 then
|
|
|
|
|
Continue;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
for LAtt in LAttributes do
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if LAtt is MVCPathAttribute then
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2020-09-16 15:56:14 +02:00
|
|
|
|
// THIS BLOCK IS HERE JUST FOR DEBUG
|
|
|
|
|
// if LMethod.Name.Contains('GetProject') then
|
|
|
|
|
// begin
|
|
|
|
|
// lMethodCompatible := True; //debug here
|
|
|
|
|
// end;
|
|
|
|
|
// lMethodCompatible := IsHTTPMethodCompatible(ARequestMethodType, LAttributes);
|
|
|
|
|
// lContentTypeCompatible := IsHTTPContentTypeCompatible(ARequestMethodType, LRequestContentType, LAttributes);
|
|
|
|
|
// lAcceptCompatible := IsHTTPAcceptCompatible(ARequestMethodType, LRequestAccept, LAttributes);
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if IsHTTPMethodCompatible(ARequestMethodType, LAttributes) and
|
|
|
|
|
IsHTTPContentTypeCompatible(ARequestMethodType, LRequestContentType, LAttributes) and
|
|
|
|
|
IsHTTPAcceptCompatible(ARequestMethodType, LRequestAccept, LAttributes) then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
LMethodPath := MVCPathAttribute(LAtt).Path;
|
2021-02-03 16:04:36 +01:00
|
|
|
|
if IsCompatiblePath(APathPrefix + LControllerMappedPath + LMethodPath,
|
2021-01-29 19:30:09 +01:00
|
|
|
|
LRequestPathInfo, ARequestParams) then
|
2013-11-08 23:10:25 +01:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FMethodToCall := LMethod;
|
|
|
|
|
FControllerClazz := LControllerDelegate.Clazz;
|
|
|
|
|
FControllerCreateAction := LControllerDelegate.CreateAction;
|
|
|
|
|
LProduceAttribute := GetAttribute<MVCProducesAttribute>(LAttributes);
|
2020-08-08 01:03:37 +02:00
|
|
|
|
if LProduceAttribute <> nil then
|
2014-04-01 00:02:31 +02:00
|
|
|
|
begin
|
2017-09-08 16:59:02 +02:00
|
|
|
|
AResponseContentMediaType := LProduceAttribute.Value;
|
|
|
|
|
AResponseContentCharset := LProduceAttribute.Charset;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
2017-09-08 16:59:02 +02:00
|
|
|
|
AResponseContentMediaType := ADefaultContentType;
|
|
|
|
|
AResponseContentCharset := ADefaultContentCharset;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
end;
|
2014-10-26 20:48:52 +01:00
|
|
|
|
Exit(True);
|
2017-03-13 20:52:11 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
end; // if MVCPathAttribute
|
|
|
|
|
end; // for in Attributes
|
|
|
|
|
end; // for in Methods
|
|
|
|
|
end; // for in Controllers
|
2014-04-01 00:02:31 +02:00
|
|
|
|
finally
|
2019-05-08 20:20:14 +02:00
|
|
|
|
TMonitor.Exit(gLock);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.GetAttribute<T>(const AAttributes: TArray<TCustomAttribute>): T;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
Att: TCustomAttribute;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
Result := nil;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
for Att in AAttributes do
|
|
|
|
|
if Att is T then
|
|
|
|
|
Exit(T(Att));
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
function TMVCRouter.GetControllerMappedPath(
|
|
|
|
|
const aControllerName: string;
|
|
|
|
|
const aControllerAttributes: TArray<TCustomAttribute>): string;
|
|
|
|
|
var
|
|
|
|
|
LFound: Boolean;
|
|
|
|
|
LAtt: TCustomAttribute;
|
|
|
|
|
begin
|
|
|
|
|
LFound := False;
|
|
|
|
|
for LAtt in aControllerAttributes do
|
|
|
|
|
begin
|
|
|
|
|
if LAtt is MVCPathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
LFound := True;
|
|
|
|
|
Result := MVCPathAttribute(LAtt).Path;
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if not LFound then
|
|
|
|
|
raise EMVCException.CreateFmt('Controller %s does not have MVCPath attribute', [aControllerName]);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.GetFirstMediaType(const AContentType: string): string;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := AContentType;
|
|
|
|
|
while Pos(',', Result) > 0 do
|
|
|
|
|
Result := Copy(Result, 1, Pos(',', Result) - 1);
|
|
|
|
|
while Pos(';', Result) > 0 do
|
2014-10-26 20:48:52 +01:00
|
|
|
|
Result := Copy(Result, 1, Pos(';', Result) - 1);
|
2014-06-27 15:30:39 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.IsCompatiblePath(
|
|
|
|
|
const AMVCPath: string;
|
|
|
|
|
const APath: string;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
var aParams: TMVCRequestParamsTable): Boolean;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
function ToPattern(const V: string; const Names: TList<string>): string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
S: string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
Result := V;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
for S in Names do
|
2020-09-16 15:56:14 +02:00
|
|
|
|
Result := StringReplace(Result, '($' + S + ')', '([' + TMVCConstants.URL_MAPPED_PARAMS_ALLOWED_CHARS + ']*)',
|
|
|
|
|
[rfReplaceAll]);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
var
|
|
|
|
|
lRegEx: TRegEx;
|
|
|
|
|
lMatch: TMatch;
|
|
|
|
|
lPattern: string;
|
|
|
|
|
I: Integer;
|
|
|
|
|
lNames: TList<string>;
|
|
|
|
|
lCacheItem: TMVCActionParamCacheItem;
|
|
|
|
|
begin
|
|
|
|
|
if not FActionParamsCache.TryGetValue(AMVCPath, lCacheItem) then
|
|
|
|
|
begin
|
|
|
|
|
lNames := GetParametersNames(AMVCPath);
|
|
|
|
|
lPattern := ToPattern(AMVCPath, lNames);
|
|
|
|
|
lCacheItem := TMVCActionParamCacheItem.Create(lPattern, lNames);
|
|
|
|
|
FActionParamsCache.Add(AMVCPath, lCacheItem);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
if (APath = AMVCPath) then
|
|
|
|
|
Exit(True)
|
|
|
|
|
else
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-06-02 00:10:31 +02:00
|
|
|
|
lRegEx := TRegEx.Create('^' + lCacheItem.Value + '$', [roIgnoreCase, roCompiled, roSingleLine]);
|
2021-08-11 00:21:04 +02:00
|
|
|
|
lMatch := lRegEx.Match(APath);
|
2017-06-02 00:10:31 +02:00
|
|
|
|
Result := lMatch.Success;
|
|
|
|
|
if Result then
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2017-06-02 00:10:31 +02:00
|
|
|
|
for I := 1 to pred(lMatch.Groups.Count) do
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2020-09-16 15:56:14 +02:00
|
|
|
|
aParams.Add(lCacheItem.Params[I - 1], TIdURI.URLDecode(lMatch.Groups[I].Value));
|
2019-05-02 17:38:57 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCRouter.GetParametersNames(const V: string): TList<string>;
|
|
|
|
|
var
|
|
|
|
|
S: string;
|
|
|
|
|
Matches: TMatchCollection;
|
|
|
|
|
M: TMatch;
|
|
|
|
|
I: Integer;
|
|
|
|
|
lList: TList<string>;
|
|
|
|
|
begin
|
|
|
|
|
lList := TList<string>.Create;
|
|
|
|
|
try
|
2017-03-13 20:52:11 +01:00
|
|
|
|
S := '\(\$([A-Za-z0-9\_]+)\)';
|
|
|
|
|
Matches := TRegEx.Matches(V, S, [roIgnoreCase, roCompiled, roSingleLine]);
|
|
|
|
|
for M in Matches do
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
for I := 0 to M.Groups.Count - 1 do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
S := M.Groups[I].Value;
|
2019-05-02 17:38:57 +02:00
|
|
|
|
if (Length(S) > 0) and (S.Chars[0] <> '(') then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2017-06-02 00:10:31 +02:00
|
|
|
|
lList.Add(S);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Break;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
Result := lList;
|
|
|
|
|
except
|
|
|
|
|
lList.Free;
|
|
|
|
|
raise;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2020-02-26 13:10:41 +01:00
|
|
|
|
function TMVCRouter.GetQualifiedActionName: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := Self.FControllerClazz.QualifiedClassName + '.' + Self.MethodToCall.Name;
|
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.IsHTTPAcceptCompatible(
|
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
var AAccept: string;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
I: Integer;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
MethodAccept: string;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FoundOneAttProduces: Boolean;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := False;
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if AAccept.Contains('*/*') then // 2020-08-08
|
2019-12-17 14:52:11 +01:00
|
|
|
|
begin
|
|
|
|
|
Exit(True);
|
|
|
|
|
end;
|
2014-06-27 15:30:39 +02:00
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FoundOneAttProduces := False;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
for I := 0 to high(AAttributes) do
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if AAttributes[I] is MVCProducesAttribute then
|
|
|
|
|
begin
|
|
|
|
|
FoundOneAttProduces := True;
|
|
|
|
|
MethodAccept := MVCProducesAttribute(AAttributes[I]).Value;
|
|
|
|
|
AAccept := GetFirstMediaType(AAccept);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
Result := SameText(AAccept, MethodAccept, loInvariantLocale);
|
|
|
|
|
if Result then
|
|
|
|
|
Break;
|
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
|
|
|
|
Result := (not FoundOneAttProduces) or (FoundOneAttProduces and Result);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.IsHTTPContentTypeCompatible(
|
|
|
|
|
const ARequestMethodType: TMVCHTTPMethodType;
|
|
|
|
|
var AContentType: string;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
I: Integer;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
MethodContentType: string;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FoundOneAttConsumes: Boolean;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
begin
|
2017-09-08 16:59:02 +02:00
|
|
|
|
if ARequestMethodType in MVC_HTTP_METHODS_WITHOUT_CONTENT then
|
2014-10-26 20:48:52 +01:00
|
|
|
|
Exit(True);
|
2014-03-24 17:37:08 +01:00
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
Result := False;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
|
|
|
|
FoundOneAttConsumes := False;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
for I := 0 to high(AAttributes) do
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if AAttributes[I] is MVCConsumesAttribute then
|
2013-11-09 14:22:11 +01:00
|
|
|
|
begin
|
2017-03-13 20:52:11 +01:00
|
|
|
|
FoundOneAttConsumes := True;
|
|
|
|
|
MethodContentType := MVCConsumesAttribute(AAttributes[I]).Value;
|
|
|
|
|
AContentType := GetFirstMediaType(AContentType);
|
2014-04-01 00:02:31 +02:00
|
|
|
|
Result := SameText(AContentType, MethodContentType, loInvariantLocale);
|
|
|
|
|
if Result then
|
|
|
|
|
Break;
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
|
|
|
|
Result := (not FoundOneAttConsumes) or (FoundOneAttConsumes and Result);
|
2013-11-09 14:22:11 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-03-13 20:52:11 +01:00
|
|
|
|
function TMVCRouter.IsHTTPMethodCompatible(
|
|
|
|
|
const AMethodType: TMVCHTTPMethodType;
|
|
|
|
|
const AAttributes: TArray<TCustomAttribute>): Boolean;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2017-03-13 20:52:11 +01:00
|
|
|
|
I: Integer;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
MustBeCompatible: Boolean;
|
|
|
|
|
CompatibleMethods: TMVCHTTPMethods;
|
|
|
|
|
begin
|
|
|
|
|
Result := False;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
MustBeCompatible := False;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
for I := 0 to high(AAttributes) do
|
2017-03-13 20:52:11 +01:00
|
|
|
|
if AAttributes[I] is MVCHTTPMethodAttribute then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2014-10-26 20:48:52 +01:00
|
|
|
|
MustBeCompatible := True;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
CompatibleMethods := MVCHTTPMethodAttribute(AAttributes[I]).MVCHTTPMethods;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Result := (AMethodType in CompatibleMethods);
|
|
|
|
|
end;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
|
2013-11-09 14:22:11 +01:00
|
|
|
|
Result := (not MustBeCompatible) or (MustBeCompatible and Result);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2020-09-16 15:56:14 +02:00
|
|
|
|
class function TMVCRouter.StringMethodToHTTPMetod(const aValue: string): TMVCHTTPMethodType;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'GET' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpGET);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'POST' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpPOST);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'DELETE' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpDELETE);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'PUT' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpPUT);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'HEAD' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpHEAD);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'OPTIONS' then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
Exit(httpOPTIONS);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'PATCH' then
|
2014-03-24 17:37:08 +01:00
|
|
|
|
Exit(httpPATCH);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
if aValue = 'TRACE' then
|
2014-03-24 17:37:08 +01:00
|
|
|
|
Exit(httpTRACE);
|
2020-09-16 15:56:14 +02:00
|
|
|
|
raise EMVCException.CreateFmt('Unknown HTTP method [%s]', [aValue]);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
{ TMVCActionParamCacheItem }
|
|
|
|
|
|
|
|
|
|
constructor TMVCActionParamCacheItem.Create(aValue: string;
|
|
|
|
|
aParams: TList<string>);
|
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
|
|
|
|
FValue := aValue;
|
|
|
|
|
FParams := aParams;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCActionParamCacheItem.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
FParams.Free;
|
|
|
|
|
inherited;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCActionParamCacheItem.Params: TList<string>;
|
|
|
|
|
begin
|
|
|
|
|
Result := FParams;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCActionParamCacheItem.Value: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := FValue;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end.
|