2023-02-05 00:36:01 +01:00
|
|
|
|
// ***************************************************************************
|
2016-06-22 17:49:16 +02:00
|
|
|
|
//
|
|
|
|
|
// Delphi MVC Framework
|
|
|
|
|
//
|
2024-01-02 17:04:27 +01:00
|
|
|
|
// Copyright (c) 2010-2024 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
|
//
|
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
|
//
|
2022-03-22 12:38:57 +01:00
|
|
|
|
// Collaborators on this file: Ezequiel Juliano Müller (ezequieljuliano@gmail.com)
|
2017-03-13 20:52:11 +01:00
|
|
|
|
//
|
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,
|
2022-01-26 23:00:32 +01:00
|
|
|
|
IdURI, System.Classes;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
|
|
type
|
2017-06-02 00:10:31 +02:00
|
|
|
|
TMVCActionParamCacheItem = class
|
|
|
|
|
private
|
|
|
|
|
FValue: string;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
FParams: TList<TPair<String, String>>;
|
2024-04-19 13:21:45 +02:00
|
|
|
|
FRegEx: TRegEx;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
public
|
2024-05-09 23:50:01 +02:00
|
|
|
|
constructor Create(aValue: string; aParams: TList<TPair<String, String>>); virtual;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
function Value: string;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
function Params: TList<TPair<String, String>>; // this should be read-only...
|
2024-04-19 13:21:45 +02:00
|
|
|
|
function Match(const Value: String): TMatch; inline;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
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;
|
2024-03-24 16:58:08 +01:00
|
|
|
|
FControllerInjectableConstructor: TRttiMethod;
|
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;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
function GetParametersNames(const V: string): TList<TPair<string, string>>;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
protected
|
2022-01-26 23:00:32 +01:00
|
|
|
|
procedure FillControllerMappedPaths(
|
2017-06-02 00:10:31 +02:00
|
|
|
|
const aControllerName: string;
|
2022-01-26 23:00:32 +01:00
|
|
|
|
const aControllerAttributes: TArray<TCustomAttribute>;
|
|
|
|
|
const aControllerMappedPaths: TStringList);
|
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;
|
2024-03-24 16:58:08 +01:00
|
|
|
|
property ControllerInjectableConstructor: TRttiMethod read FControllerInjectableConstructor;
|
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,
|
2024-03-25 00:15:50 +01:00
|
|
|
|
System.NetEncoding, MVCFramework.Rtti.Utils, MVCFramework.Container;
|
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;
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LControllerMappedPaths: TStringList;
|
2017-03-13 20:52:11 +01:00
|
|
|
|
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;
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LItem: 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;
|
2022-03-22 12:38:57 +01:00
|
|
|
|
LRequestPathInfo := TIdURI.PathEncode(Trim(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
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LControllerMappedPaths := TStringList.Create;
|
|
|
|
|
try
|
|
|
|
|
for LControllerDelegate in AControllers do
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LControllerMappedPaths.Clear;
|
|
|
|
|
SetLength(LAttributes, 0);
|
|
|
|
|
LRttiType := FRttiContext.GetType(LControllerDelegate.Clazz.ClassInfo);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
2022-01-26 23:00:32 +01:00
|
|
|
|
lURLSegment := LControllerDelegate.URLSegment;
|
|
|
|
|
if lURLSegment.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
LAttributes := LRttiType.GetAttributes;
|
|
|
|
|
if (LAttributes = nil) then
|
|
|
|
|
Continue;
|
|
|
|
|
FillControllerMappedPaths(LRttiType.Name, LAttributes, LControllerMappedPaths);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
LControllerMappedPaths.Add(lURLSegment);
|
|
|
|
|
end;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
|
2022-01-26 23:00:32 +01:00
|
|
|
|
for LItem in LControllerMappedPaths do
|
|
|
|
|
begin
|
|
|
|
|
LControllerMappedPath := LItem;
|
|
|
|
|
if (LControllerMappedPath = '/') then
|
|
|
|
|
begin
|
|
|
|
|
LControllerMappedPath := '';
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
{$IF defined(TOKYOORBETTER)}
|
|
|
|
|
if not LRequestPathInfo.StartsWith(APathPrefix + LControllerMappedPath, True) then
|
|
|
|
|
{$ELSE}
|
|
|
|
|
if not TMVCStringHelper.StartsWith(APathPrefix + LControllerMappedPath, LRequestPathInfo, True) then
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
begin
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
|
|
|
|
LMethods := LRttiType.GetMethods; { do not use GetDeclaredMethods because JSON-RPC rely on this!! }
|
|
|
|
|
for LMethod in LMethods do
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2022-01-26 23:00:32 +01:00
|
|
|
|
if LMethod.Visibility <> mvPublic then // 2020-08-08
|
|
|
|
|
Continue;
|
2023-02-05 00:36:01 +01:00
|
|
|
|
if not (LMethod.MethodKind in [mkProcedure, mkFunction]) then
|
2022-01-26 23:00:32 +01:00
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
LAttributes := LMethod.GetAttributes;
|
|
|
|
|
if Length(LAttributes) = 0 then
|
|
|
|
|
Continue;
|
|
|
|
|
|
|
|
|
|
for LAtt in LAttributes do
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2022-01-26 23:00:32 +01:00
|
|
|
|
if LAtt is MVCPathAttribute then
|
2013-11-08 23:10:25 +01:00
|
|
|
|
begin
|
2022-01-26 23:00:32 +01: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);
|
|
|
|
|
|
|
|
|
|
if IsHTTPMethodCompatible(ARequestMethodType, LAttributes) and
|
|
|
|
|
IsHTTPContentTypeCompatible(ARequestMethodType, LRequestContentType, LAttributes) and
|
|
|
|
|
IsHTTPAcceptCompatible(ARequestMethodType, LRequestAccept, LAttributes) then
|
2014-04-01 00:02:31 +02:00
|
|
|
|
begin
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LMethodPath := MVCPathAttribute(LAtt).Path;
|
|
|
|
|
if IsCompatiblePath(APathPrefix + LControllerMappedPath + LMethodPath,
|
|
|
|
|
LRequestPathInfo, ARequestParams) then
|
|
|
|
|
begin
|
|
|
|
|
FMethodToCall := LMethod;
|
|
|
|
|
FControllerClazz := LControllerDelegate.Clazz;
|
|
|
|
|
FControllerCreateAction := LControllerDelegate.CreateAction;
|
2024-03-24 16:58:08 +01:00
|
|
|
|
FControllerInjectableConstructor := nil;
|
|
|
|
|
|
2024-03-27 00:10:48 +01:00
|
|
|
|
// select the constructor with the most mumber of parameters
|
2024-03-24 16:58:08 +01:00
|
|
|
|
if not Assigned(FControllerCreateAction) then
|
|
|
|
|
begin
|
2024-03-28 23:57:59 +01:00
|
|
|
|
FControllerInjectableConstructor := TRttiUtils.GetConstructorWithAttribute<MVCInjectAttribute>(LRttiType);
|
2024-03-24 16:58:08 +01:00
|
|
|
|
end;
|
2024-03-27 00:10:48 +01:00
|
|
|
|
// end - select the constructor with the most mumber of parameters
|
2024-03-24 16:58:08 +01:00
|
|
|
|
|
2022-01-26 23:00:32 +01:00
|
|
|
|
LProduceAttribute := GetAttribute<MVCProducesAttribute>(LAttributes);
|
|
|
|
|
if LProduceAttribute <> nil then
|
|
|
|
|
begin
|
|
|
|
|
AResponseContentMediaType := LProduceAttribute.Value;
|
|
|
|
|
AResponseContentCharset := LProduceAttribute.Charset;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
AResponseContentMediaType := ADefaultContentType;
|
|
|
|
|
AResponseContentCharset := ADefaultContentCharset;
|
|
|
|
|
end;
|
|
|
|
|
Exit(True);
|
|
|
|
|
end;
|
2014-04-01 00:02:31 +02:00
|
|
|
|
end;
|
2022-01-26 23:00:32 +01:00
|
|
|
|
end; // if MVCPathAttribute
|
|
|
|
|
end; // for in Attributes
|
|
|
|
|
end; // for in Methods
|
|
|
|
|
end;
|
|
|
|
|
end; // for in Controllers
|
|
|
|
|
finally
|
|
|
|
|
LControllerMappedPaths.Free;
|
|
|
|
|
end;
|
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;
|
|
|
|
|
|
2022-01-26 23:00:32 +01:00
|
|
|
|
procedure TMVCRouter.FillControllerMappedPaths(
|
|
|
|
|
const aControllerName: string;
|
|
|
|
|
const aControllerAttributes: TArray<TCustomAttribute>;
|
|
|
|
|
const aControllerMappedPaths: TStringList);
|
2017-06-02 00:10:31 +02:00
|
|
|
|
var
|
|
|
|
|
LFound: Boolean;
|
|
|
|
|
LAtt: TCustomAttribute;
|
|
|
|
|
begin
|
|
|
|
|
LFound := False;
|
|
|
|
|
for LAtt in aControllerAttributes do
|
|
|
|
|
begin
|
|
|
|
|
if LAtt is MVCPathAttribute then
|
|
|
|
|
begin
|
|
|
|
|
LFound := True;
|
2022-01-26 23:00:32 +01:00
|
|
|
|
aControllerMappedPaths.Add(MVCPathAttribute(LAtt).Path);
|
2017-06-02 00:10:31 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
if not LFound then
|
2022-01-26 23:00:32 +01:00
|
|
|
|
begin
|
2017-06-02 00:10:31 +02:00
|
|
|
|
raise EMVCException.CreateFmt('Controller %s does not have MVCPath attribute', [aControllerName]);
|
2022-01-26 23:00:32 +01:00
|
|
|
|
end;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
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
|
|
|
|
|
2024-05-09 23:50:01 +02:00
|
|
|
|
function ToPattern(const V: string; const Names: TList<TPair<String, String>>): string;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
var
|
2024-05-09 23:50:01 +02:00
|
|
|
|
S: TPair<String, String>;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
|
|
|
|
Result := V;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
if Names.Count > 0 then
|
|
|
|
|
begin
|
|
|
|
|
for S in Names do
|
|
|
|
|
begin
|
|
|
|
|
Result := StringReplace(
|
|
|
|
|
Result,
|
|
|
|
|
'($' + S.Key + S.Value + ')', '([' + TMVCConstants.URL_MAPPED_PARAMS_ALLOWED_CHARS + ']*)',
|
|
|
|
|
[rfReplaceAll]);
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
var
|
|
|
|
|
lMatch: TMatch;
|
|
|
|
|
lPattern: string;
|
|
|
|
|
I: Integer;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
lNames: TList<TPair<String, String>>;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
lCacheItem: TMVCActionParamCacheItem;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
P: TPair<string, string>;
|
|
|
|
|
lConv: string;
|
|
|
|
|
lParValue: String;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
2024-04-19 13:21:45 +02:00
|
|
|
|
if (APath = AMVCPath) or ((APath = '/') and (AMVCPath = '')) then
|
|
|
|
|
begin
|
|
|
|
|
Exit(True);
|
|
|
|
|
end;
|
|
|
|
|
|
2017-06-02 00:10:31 +02:00
|
|
|
|
if not FActionParamsCache.TryGetValue(AMVCPath, lCacheItem) then
|
|
|
|
|
begin
|
|
|
|
|
lNames := GetParametersNames(AMVCPath);
|
|
|
|
|
lPattern := ToPattern(AMVCPath, lNames);
|
2024-01-25 19:32:04 +01:00
|
|
|
|
lCacheItem := TMVCActionParamCacheItem.Create('^' + lPattern + '$', lNames);
|
2024-05-09 23:50:01 +02:00
|
|
|
|
FActionParamsCache.Add(AMVCPath, lCacheItem); {do not commit this!}
|
2017-06-02 00:10:31 +02:00
|
|
|
|
end;
|
|
|
|
|
|
2024-04-19 13:21:45 +02:00
|
|
|
|
lMatch := lCacheItem.Match(APath);
|
|
|
|
|
Result := lMatch.Success;
|
|
|
|
|
if Result then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2024-04-19 13:21:45 +02:00
|
|
|
|
for I := 1 to Pred(lMatch.Groups.Count) do
|
2019-05-02 17:38:57 +02:00
|
|
|
|
begin
|
2024-05-09 23:50:01 +02:00
|
|
|
|
P := lCacheItem.Params[I - 1];
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
P.Key = Parameter name
|
|
|
|
|
P.Value = Converter applied to the value before to be injected (eg. :sqid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lParValue := TIdURI.URLDecode(lMatch.Groups[I].Value);
|
|
|
|
|
if P.Value.IsEmpty then
|
|
|
|
|
begin
|
|
|
|
|
{no converter}
|
|
|
|
|
aParams.Add(P.Key, lParValue);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
lConv := P.Value;
|
2024-05-10 16:16:47 +02:00
|
|
|
|
if SameText(lConv, ':sqids') then
|
2024-05-09 23:50:01 +02:00
|
|
|
|
begin
|
|
|
|
|
{sqids converter (so far the only one)}
|
|
|
|
|
aParams.Add(P.Key, TMVCSqids.SqidToInt(lParValue).ToString);
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
2024-05-10 16:16:47 +02:00
|
|
|
|
raise EMVCException.CreateFmt('Unknown converter [%s]', [lConv]);
|
2024-05-09 23:50:01 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2019-05-02 17:38:57 +02:00
|
|
|
|
end;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2024-05-09 23:50:01 +02:00
|
|
|
|
function TMVCRouter.GetParametersNames(const V: string): TList<TPair<string, string>>;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
var
|
|
|
|
|
S: string;
|
|
|
|
|
Matches: TMatchCollection;
|
|
|
|
|
M: TMatch;
|
|
|
|
|
I: Integer;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
lList: TList<TPair<string, string>>;
|
|
|
|
|
lNameFound: Boolean;
|
|
|
|
|
lConverter: string;
|
|
|
|
|
lName: string;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
2024-05-09 23:50:01 +02:00
|
|
|
|
lList := TList<TPair<string, string>>.Create;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
try
|
2024-05-09 23:50:01 +02:00
|
|
|
|
S := '\(\$([A-Za-z0-9\_]+)(\:[a-z]+)?\)';
|
2017-03-13 20:52:11 +01:00
|
|
|
|
Matches := TRegEx.Matches(V, S, [roIgnoreCase, roCompiled, roSingleLine]);
|
|
|
|
|
for M in Matches do
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
2024-05-09 23:50:01 +02:00
|
|
|
|
lNameFound := False;
|
|
|
|
|
lConverter := '';
|
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;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
if Length(S) > 0 then
|
2013-10-30 00:48:23 +01:00
|
|
|
|
begin
|
2024-05-09 23:50:01 +02:00
|
|
|
|
if (not lNameFound) and (S.Chars[0] <> '(') and (S.Chars[0] <> ':') then
|
|
|
|
|
begin
|
|
|
|
|
lName := S;
|
|
|
|
|
lNameFound := True;
|
|
|
|
|
Continue;
|
|
|
|
|
end;
|
|
|
|
|
if lNameFound and (S.Chars[0] = ':') then
|
|
|
|
|
begin
|
|
|
|
|
lConverter := S;
|
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end;
|
|
|
|
|
end;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
if lNameFound then
|
|
|
|
|
begin
|
|
|
|
|
lList.Add(TPair<string,string>.Create(lName,lConverter));
|
|
|
|
|
end;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
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;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
aParams: TList<TPair<String, String>>);
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
|
|
|
|
inherited Create;
|
2024-05-09 23:50:01 +02:00
|
|
|
|
fValue := aValue;
|
|
|
|
|
fParams := aParams;
|
|
|
|
|
fRegEx := TRegEx.Create(FValue, [roIgnoreCase, roCompiled, roSingleLine]);
|
2017-06-02 00:10:31 +02:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
destructor TMVCActionParamCacheItem.Destroy;
|
|
|
|
|
begin
|
|
|
|
|
FParams.Free;
|
|
|
|
|
inherited;
|
|
|
|
|
end;
|
|
|
|
|
|
2024-04-19 13:21:45 +02:00
|
|
|
|
function TMVCActionParamCacheItem.Match(const Value: String): TMatch;
|
|
|
|
|
begin
|
|
|
|
|
Result := fRegEx.Match(Value);
|
|
|
|
|
end;
|
|
|
|
|
|
2024-05-09 23:50:01 +02:00
|
|
|
|
function TMVCActionParamCacheItem.Params: TList<TPair<String, String>>;
|
2017-06-02 00:10:31 +02:00
|
|
|
|
begin
|
|
|
|
|
Result := FParams;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
function TMVCActionParamCacheItem.Value: string;
|
|
|
|
|
begin
|
|
|
|
|
Result := FValue;
|
|
|
|
|
end;
|
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
|
end.
|