delphimvcframework/sources/MVCFramework.pas

4084 lines
136 KiB
ObjectPascal
Raw Normal View History

// ***************************************************************************
//
// Delphi MVC Framework
//
2021-02-25 10:50:57 +01:00
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
//
// https://github.com/danieleteti/delphimvcframework
//
// Collaborators on this file:
// Ezequiel Juliano Müller (ezequieljuliano@gmail.com)
// João Antônio Duarte (https://github.com/joaoduarte19)
//
// ***************************************************************************
//
// 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.
//
// *************************************************************************** }
2013-10-30 00:48:23 +01:00
unit MVCFramework;
{$I dmvcframework.inc}
2019-05-02 17:38:57 +02:00
{$IF IOS}
{$MESSAGE Fatal 'This unit is not compilable on iOS'}
{$ENDIF}
2013-10-30 00:48:23 +01:00
{$RTTI EXPLICIT
METHODS(DefaultMethodRttiVisibility)
FIELDS(DefaultFieldRttiVisibility)
PROPERTIES(DefaultPropertyRttiVisibility)}
{$WARNINGS OFF}
2013-10-30 00:48:23 +01:00
interface
uses
System.Classes,
System.SysUtils,
System.TypInfo,
System.IOUtils,
System.SyncObjs,
System.DateUtils,
System.Generics.Collections,
System.Rtti,
Data.DB,
MVCFramework.Session,
MVCFramework.DuckTyping,
MVCFramework.Logger,
MVCFramework.ApplicationSession,
MVCFramework.Serializer.Intf,
{$IFDEF WEBAPACHEHTTP}
Web.ApacheHTTP,
// Apache Support since XE6 http://docwiki.embarcadero.com/Libraries/XE6/de/Web.ApacheHTTP
{$ENDIF}
2017-10-16 22:57:27 +02:00
// Delphi XE4 (all update) and XE5 (with no update) don't contains this unit. Look for the bug in QC
// https://quality.embarcadero.com/browse/RSP-17216
2019-05-09 20:53:52 +02:00
{$IFNDEF MOBILE} // file upload is not supported on mobile
2019-08-02 12:32:23 +02:00
{$IF Defined(SeattleOrBetter)}
Web.ReqMulti,
2019-08-02 12:32:23 +02:00
{$ELSE}
ReqMulti,
{$ENDIF}
{$ENDIF}
Web.HTTPApp,
2019-05-02 17:38:57 +02:00
{$IFDEF MSWINDOWS}
Web.Win.IsapiHTTP,
{$ENDIF}
Web.WebReq,
LoggerPro,
IdGlobal,
IdGlobalProtocols,
// IdHTTPWebBrokerBridge,
Swag.Doc,
Swag.Common.Types,
MVCFramework.Commons,
MVCFramework.Serializer.Commons;
2013-10-30 00:48:23 +01:00
type
TSessionData = TDictionary<string, string>;
TMVCCustomData = TSessionData;
TMVCBaseViewEngine = class;
TMVCViewEngineClass = class of TMVCBaseViewEngine;
MVCBaseAttribute = class(TCustomAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCRequiresAuthenticationAttribute = class(MVCBaseAttribute)
end;
MVCHTTPMethodsAttribute = class(MVCBaseAttribute)
private
FMVCHTTPMethods: TMVCHTTPMethods;
function GetMVCHTTPMethodsAsString: string;
protected
{ protected declarations }
public
constructor Create(const AMVCHTTPMethods: TMVCHTTPMethods);
property MVCHTTPMethods: TMVCHTTPMethods read FMVCHTTPMethods;
property MVCHTTPMethodsAsString: string read GetMVCHTTPMethodsAsString;
end;
MVCHTTPMethodAttribute = MVCHTTPMethodsAttribute;
MVCStringAttribute = class(MVCBaseAttribute)
private
FValue: string;
protected
{ protected declarations }
public
constructor Create(const AValue: string);
property Value: string read FValue;
end;
MVCIntegerAttribute = class(MVCBaseAttribute)
private
FValue: Int64;
protected
{ protected declarations }
public
constructor Create(const AValue: Int64);
property Value: Int64 read FValue;
end;
MVCConsumesAttribute = class(MVCStringAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCProducesAttribute = class(MVCStringAttribute)
private
FCharset: string;
protected
{ protected declarations }
public
constructor Create(const AValue: string); overload;
constructor Create(const AValue: string; const ACharset: string); overload;
property Charset: string read FCharset;
end;
MVCDocAttribute = class(MVCStringAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCFormatAttribute = class(MVCStringAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCMaxLengthAttribute = class(MVCIntegerAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCMinimumAttribute = class(MVCIntegerAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCMaximumAttribute = class(MVCIntegerAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCInheritableAttribute = class(MVCBaseAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
MVCPathAttribute = class(MVCBaseAttribute)
private
FPath: string;
protected
{ protected declarations }
public
constructor Create(const APath: string); overload;
property Path: string read FPath;
end;
MVCResponseAttribute = class(MVCBaseAttribute)
2019-07-26 13:51:11 +02:00
private
FStatusCode: Integer;
FDescription: string;
FResponseClass: TClass;
2019-07-26 13:51:11 +02:00
protected
{ protected declarations }
public
constructor Create(inStatusCode: Integer; const inDescription: string;
inResponseClass: TClass = nil); overload;
property StatusCode: Integer read FStatusCode;
property Description: string read FDescription;
property ResponseClass: TClass read FResponseClass;
end;
MVCResponseListAttribute = class(MVCBaseAttribute)
private
FStatusCode: Integer;
FDescription: string;
FResponseClass: TClass;
protected
{ protected declarations }
public
constructor Create(inStatusCode: Integer; const inDescription: string;
inResponseClass: TClass = nil); overload;
property StatusCode: Integer read FStatusCode;
property Description: string read FDescription;
property ResponseClass: TClass read FResponseClass;
end;
MVCPathParamAttribute = class(MVCBaseAttribute)
private
FType: TSwagTypeParameter;
FFormat: string;
FValue: string;
public
constructor Create(AType: TSwagTypeParameter; APattern: string = ''; AFormat: string = '');
property ParamType: TSwagTypeParameter read FType;
property Format: string read FFormat;
property Pattern: string read FValue;
end;
MVCParamAttribute = class(MVCStringAttribute)
private
FName: string;
FLocation: TSwagRequestParameterInLocation;
FType: TSwagTypeParameter;
FClassType: TClass;
FPattern: string;
FFormat: string;
public
property name: string read FName write FName;
property Location: TSwagRequestParameterInLocation read FLocation write FLocation;
property ParamType: TSwagTypeParameter read FType write FType;
property ClassType: TClass read FClassType write FClassType;
property Pattern: string read FPattern write FPattern;
property Format: string read FFormat write FFormat;
constructor Create(name: string; Location: TSwagRequestParameterInLocation;
AType: TSwagTypeParameter; APattern: string = ''; AFormat: string = ''); overload;
constructor Create(name: string; Location: TSwagRequestParameterInLocation; AType: TClass;
APattern: string = ''; AFormat: string = ''); overload;
end;
MVCPatternAttribute = class(MVCStringAttribute)
end;
MVCStringEnumAttribute = class(MVCBaseAttribute)
private
fValues: string;
public
constructor Create(const enumValue: string);
property Values: string read fValues write fValues;
end;
MVCFromBodyAttribute = class(MVCBaseAttribute)
private
fRootNode: string;
fDataType: TMVCDataType;
public
constructor Create(const RootNode: string = ''; const DataType: TMVCDataType = TMVCDataType.dtObject);
function DataType: TMVCDataType;
function RootNode: String;
end;
MVCInjectableParamAttribute = class(MVCBaseAttribute)
private
FParamName: string;
FDefaultValueAsString: string;
FCanBeUsedADefaultValue: Boolean;
public
constructor Create(const AParamName: string); overload;
constructor Create(const AParamName: string; const DefaultAsString: string); overload;
property ParamName: string read FParamName;
property DefaultValueAsString: string read FDefaultValueAsString;
property CanBeUsedADefaultValue: Boolean read FCanBeUsedADefaultValue;
end;
MVCFromQueryStringAttribute = class(MVCInjectableParamAttribute)
end;
MVCFromHeaderAttribute = class(MVCInjectableParamAttribute)
end;
MVCFromCookieAttribute = class(MVCInjectableParamAttribute)
end;
2020-08-22 15:37:08 +02:00
// test
// TMVCHackHTTPAppRequest = class(TIdHTTPAppRequest)
// private
// function GetHeaders: TStringList;
// public
// property Headers: TStringList read GetHeaders;
// end;
2020-08-22 15:37:08 +02:00
// test-end
TMVCWebRequest = class
private
FQueryParams: TDictionary<string, string>;
2019-07-26 19:28:45 +02:00
FContentFields: TDictionary<string, string>;
FWebRequest: TWebRequest;
FSerializers: TDictionary<string, IMVCSerializer>;
FBody: string;
FContentType: string;
FCharset: string;
FParamsTable: TMVCRequestParamsTable;
FContentMediaType: string;
procedure DefineContentType;
function GetContentFields: TDictionary<string, string>;
function GetQueryParams: TDictionary<string, string>;
function GetHeader(const AName: string): string;
function GetPathInfo: string;
function GetParams(const AParamName: string): string;
function GetIsAjax: Boolean;
function GetHTTPMethod: TMVCHTTPMethodType;
function GetHTTPMethodAsString: string;
function GetParamAsInteger(const AParamName: string): Integer;
function GetParamAsInt64(const AParamName: string): Int64;
function GetFiles: TAbstractWebRequestFiles;
function GetParamNames: TArray<string>;
function GetParamsMulti(const AParamName: string): TArray<string>;
protected
{ protected declarations }
procedure EnsureINDY;
public
constructor Create(const AWebRequest: TWebRequest;
const ASerializers: TDictionary<string, IMVCSerializer>);
destructor Destroy; override;
function ClientIp: string;
function ClientPrefer(const AMediaType: string): Boolean;
2017-03-20 19:08:01 +01:00
function ClientPreferHTML: Boolean;
function GetOverwrittenHTTPMethod: TMVCHTTPMethodType;
function SegmentParam(const AParamName: string; out AValue: string): Boolean;
function SegmentParamsCount: Integer;
function ThereIsRequestBody: Boolean;
procedure EnsureQueryParamExists(const AName: string);
function QueryStringParam(const AName: string): string;
function QueryStringParamExists(const AName: string): Boolean;
function QueryStringParams: TStrings;
function Accept: string;
2020-09-16 15:56:14 +02:00
function BestAccept: string;
function ContentParam(const AName: string): string;
function Cookie(const AName: string): string;
function Body: string;
function BodyAs<T: class, constructor>(const RootNode: string = ''): T;
function BodyAsListOf<T: class, constructor>(const RootNode: string = ''): TObjectList<T>;
procedure BodyFor<T: class, constructor>(const AObject: T);
procedure BodyForListOf<T: class, constructor>(const AObjectList: TObjectList<T>);
// function HeaderNames: TArray<String>;
property RawWebRequest: TWebRequest read FWebRequest;
property ContentMediaType: string read FContentMediaType;
property ContentType: string read FContentType;
property ContentCharset: string read FCharset;
property ContentFields: TDictionary<string, string> read GetContentFields;
property QueryParams: TDictionary<string, string> read GetQueryParams;
property Headers[const AHeaderName: string]: string read GetHeader;
property PathInfo: string read GetPathInfo;
property ParamsTable: TMVCRequestParamsTable read FParamsTable write FParamsTable;
property ParamNames: TArray<string> read GetParamNames;
property Params[const AParamName: string]: string read GetParams;
property ParamsMulti[const AParamName: string]: TArray<string> read GetParamsMulti;
property ParamsAsInteger[const AParamName: string]: Integer read GetParamAsInteger;
property ParamsAsInt64[const AParamName: string]: Int64 read GetParamAsInt64;
property IsAjax: Boolean read GetIsAjax;
property HTTPMethod: TMVCHTTPMethodType read GetHTTPMethod;
property HTTPMethodAsString: string read GetHTTPMethodAsString;
property Files: TAbstractWebRequestFiles read GetFiles;
end;
2013-10-30 00:48:23 +01:00
{$IFDEF WEBAPACHEHTTP}
2014-10-03 11:40:57 +02:00
TMVCApacheWebRequest = class(TMVCWebRequest)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
{$ENDIF}
TMVCISAPIWebRequest = class(TMVCWebRequest)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
2013-10-30 00:48:23 +01:00
TMVCIndyWebRequest = class(TMVCWebRequest)
private
{ private declarations }
protected
{ protected declarations }
public
// function RawHeaders: TStrings; override;
end;
2013-10-30 00:48:23 +01:00
TMVCWebResponse = class
private
FWebResponse: TWebResponse;
FFlushOnDestroy: Boolean;
function GetCustomHeaders: TStrings;
function GetReasonString: string;
function GetStatusCode: Integer;
function GetCookies: TCookieCollection;
function GetContentType: string;
function GetLocation: string;
function GetContent: string;
procedure SetReasonString(const AValue: string);
procedure SetStatusCode(const AValue: Integer);
procedure SetContentType(const AValue: string);
procedure SetLocation(const AValue: string);
procedure SetContent(const AValue: string);
public
constructor Create(const AWebResponse: TWebResponse);
destructor Destroy; override;
procedure Flush;
procedure SetCustomHeader(const AName, AValue: string);
procedure SetContentStream(const AStream: TStream; const AContentType: string);
property StatusCode: Integer read GetStatusCode write SetStatusCode;
property ReasonString: string read GetReasonString write SetReasonString;
property ContentType: string read GetContentType write SetContentType;
property CustomHeaders: TStrings read GetCustomHeaders;
property Cookies: TCookieCollection read GetCookies;
property Location: string read GetLocation write SetLocation;
property RawWebResponse: TWebResponse read FWebResponse;
property Content: string read GetContent write SetContent;
property FlushOnDestroy: Boolean read FFlushOnDestroy write FFlushOnDestroy;
end;
TUser = class
private
FUserName: string;
FRoles: TList<string>;
FLoggedSince: TDateTime;
FRealm: string;
FCustomData: TMVCCustomData;
procedure SetLoggedSince(const AValue: TDateTime);
procedure SetCustomData(const Value: TMVCCustomData);
public
constructor Create;
destructor Destroy; override;
function IsValid: Boolean;
procedure Clear;
procedure SaveToSession(const AWebSession: TWebSession);
function LoadFromSession(const AWebSession: TWebSession): Boolean;
property UserName: string read FUserName write FUserName;
property Roles: TList<string> read FRoles;
property LoggedSince: TDateTime read FLoggedSince write SetLoggedSince;
property Realm: string read FRealm write FRealm;
property CustomData: TMVCCustomData read FCustomData write SetCustomData;
end;
TWebContext = class
private
FRequest: TMVCWebRequest;
FResponse: TMVCWebResponse;
FConfig: TMVCConfig;
FSerializers: TDictionary<string, IMVCSerializer>;
FIsSessionStarted: Boolean;
FSessionMustBeClose: Boolean;
FLoggedUser: TUser;
FData: TDictionary<string, string>;
FWebSession: TWebSession;
function GetWebSession: TWebSession;
function GetLoggedUser: TUser;
function GetParamsTable: TMVCRequestParamsTable;
procedure SetParamsTable(const AValue: TMVCRequestParamsTable);
2020-08-22 15:37:08 +02:00
function GetHostingFrameworkType: TMVCHostingFrameworkType;
protected
procedure Flush; virtual;
procedure BindToSession(const ASessionId: string);
function SendSessionCookie(const AContext: TWebContext): string;
function AddSessionToTheSessionList(const ASessionType, ASessionId: string;
const ASessionTimeout: Integer): TWebSession;
public
constructor Create(const ARequest: TWebRequest; const AResponse: TWebResponse;
const AConfig: TMVCConfig; const ASerializers: TDictionary<string, IMVCSerializer>);
destructor Destroy; override;
procedure SessionStart; virtual;
procedure SessionStop(const ARaiseExceptionIfExpired: Boolean = True); virtual;
function SessionStarted: Boolean;
function SessionId: string;
function IsSessionStarted: Boolean;
function SessionMustBeClose: Boolean;
2020-08-22 15:37:08 +02:00
property HostingFrameworkType: TMVCHostingFrameworkType read GetHostingFrameworkType;
property LoggedUser: TUser read GetLoggedUser;
property Request: TMVCWebRequest read FRequest;
property Response: TMVCWebResponse read FResponse;
property Session: TWebSession read GetWebSession;
property Config: TMVCConfig read FConfig;
property Data: TDictionary<string, string> read FData;
property ParamsTable: TMVCRequestParamsTable read GetParamsTable write SetParamsTable;
end;
TMVCEngine = class;
TMVCBase = class
private
FEngine: TMVCEngine;
FApplicationSession: TWebApplicationSession;
function GetEngine: TMVCEngine;
function GetConfig: TMVCConfig;
function GetApplicationSession: TWebApplicationSession;
procedure SetApplicationSession(const AValue: TWebApplicationSession);
procedure SetEngine(const AValue: TMVCEngine);
protected
class function GetApplicationFileName: string; static;
class function GetApplicationFileNamePath: string; static;
public
property Engine: TMVCEngine read GetEngine write SetEngine;
property Config: TMVCConfig read GetConfig;
property ApplicationSession: TWebApplicationSession read GetApplicationSession
write SetApplicationSession;
end;
TMVCResponse = class;
TMVCErrorResponse = class;
IMVCRenderer = interface
['{2FF6DAC8-2F19-4C78-B9EC-A86296847D39}']
procedure Render(const AContent: string); overload;
procedure Render(const AObject: TObject); overload;
procedure Render(const AObject: TObject; const AOwns: Boolean); overload;
procedure Render(const AObject: TObject; const AOwns: Boolean;
const AType: TMVCSerializationType); overload;
procedure Render(const AStatusCode: Integer; AObject: TObject; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const ACollection: IMVCList); overload;
procedure Render(const ACollection: IMVCList; const AType: TMVCSerializationType); overload;
procedure Render(const ADataSet: TDataSet;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
2019-03-10 16:29:18 +01:00
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
2019-03-10 16:29:18 +01:00
const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ASerializationType: TMVCDatasetSerializationType;
2019-03-10 16:29:18 +01:00
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase;
2019-03-10 16:29:18 +01:00
const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ATextWriter: TTextWriter; const AOwns: Boolean = True); overload;
procedure Render(const AStream: TStream; const AOwns: Boolean = True); overload;
procedure Render(const AErrorCode: Integer; const AErrorMessage: string;
const AErrorClassName: string = ''); overload;
procedure Render(const AException: Exception; AExceptionItems: TList<string> = nil;
const AOwns: Boolean = True); overload;
procedure Render(const AResponse: TMVCResponse; const AOwns: Boolean = True); overload;
// SSE Support
procedure RenderSSE(const EventID: string; const EventData: string; EventName: string = '';
const Retry: Integer = TMVCConstants.SSE_RETRY_DEFAULT);
procedure SendStream(const AStream: TStream; const AOwns: Boolean = True;
const ARewind: Boolean = False);
procedure SendFile(const AFileName: string);
procedure RenderResponseStream;
function ResponseStream: TStringBuilder;
procedure Redirect(const AUrl: string);
procedure ResponseStatus(const AStatusCode: Integer; const AReasonString: string = '');
procedure Render201Created(const Location: string = '');
// Serializer access
function Serializer: IMVCSerializer; overload;
2019-05-09 20:53:52 +02:00
function Serializer(const AContentType: string; const ARaiseExcpIfNotExists: Boolean = True)
: IMVCSerializer; overload;
end;
IMVCAuthenticationHandler = interface
['{19B580EA-8A47-4364-A302-EEF3C6207A9F}']
procedure OnRequest(const AContext: TWebContext; const AControllerQualifiedClassName,
AActionName: string; var AAuthenticationRequired: Boolean);
procedure OnAuthentication(const AContext: TWebContext; const AUserName, APassword: string;
AUserRoles: TList<string>; var AIsValid: Boolean;
const ASessionData: TDictionary<string, string>);
procedure OnAuthorization(const AContext: TWebContext; AUserRoles: TList<string>;
const AControllerQualifiedClassName: string; const AActionName: string;
var AIsAuthorized: Boolean);
end;
TMVCRenderer = class(TMVCBase)
protected
FContext: TWebContext;
FContentCharset: string;
FResponseStream: TStringBuilder;
function ToMVCList(const AObject: TObject; AOwnsObject: Boolean = False): IMVCList;
public { this must be public because of entity processors }
function GetContentType: string;
function GetStatusCode: Integer;
procedure SetContentType(const AValue: string);
procedure SetStatusCode(const AValue: Integer);
function GetContext: TWebContext;
procedure Redirect(const AUrl: string); virtual;
procedure ResponseStatus(const AStatusCode: Integer; const AReasonString: string = ''); virtual;
/// <summary>
/// HTTP Status 201 indicates that as a result of HTTP POST request, one or more new resources have been successfully created on server.
/// The response may contain URI in Location header field in HTTP headers list, which can have reference to the newly created resource. Also, response payload also may include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate.
/// WARNING: The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.
/// </summary>
/// <remarks>
/// https://restfulapi.net/http-status-201-created/
/// </remarks>
procedure Render201Created(const Location: string = '';
const Reason: string = 'Created'); virtual;
/// <summary>
/// Allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent’s connection to the server persist until the process is completed.
/// The entity returned with this response SHOULD describe the request’s current status and point to (or embed) a status monitor that can provide the user with (or without) an estimate of when the request will be fulfilled.
/// </summary>
/// <remarks>
/// https://restfulapi.net/http-status-202-accepted/
/// </remarks>
procedure Render202Accepted(const HREF: string; const ID: string;
const Reason: string = 'Accepted'); virtual;
/// <summary>
/// HTTP Status 204 (No Content) indicates that the server has successfully fulfilled the request and that there is no content to send in the response payload body. The server might want to return updated meta information in the form of entity-headers, which if present SHOULD be applied to current document’s active view if any.
/// The 204 response MUST NOT include a message-body and thus is always terminated by the first empty line after the header fields.
/// </summary>
procedure Render204NoContent(const Location: string = '';
const Reason: string = 'No Content'); virtual;
function Serializer: IMVCSerializer; overload;
function Serializer(const AContentType: string;
const ARaiseExceptionIfNotExists: Boolean = True): IMVCSerializer; overload;
procedure SendStream(const AStream: TStream; const AOwns: Boolean = True;
const ARewind: Boolean = False); virtual;
procedure SendFile(const AFileName: string); virtual;
procedure RenderResponseStream; virtual;
function ResponseStream: TStringBuilder;
procedure Render(const AContent: string); overload;
2019-05-09 20:53:52 +02:00
// PODO renders
procedure Render(const AStatusCode: Integer; const AObject: TObject;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AObject: TObject;
const ASerializationAction: TMVCSerializationAction = nil); overload;
2019-05-09 20:53:52 +02:00
procedure Render(const AObject: TObject; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AObject: TObject; const AOwns: Boolean;
const AType: TMVCSerializationType;
2019-05-09 20:53:52 +02:00
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AStatusCode: Integer; AObject: TObject; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AObject: IInterface;
const ASerializationAction: TMVCSerializationAction = nil); overload;
procedure Render(const AStatusCode: Integer; const AObject: IInterface;
const ASerializationAction: TMVCSerializationAction = nil); overload;
2019-05-09 20:53:52 +02:00
// PODOs Collection render
procedure Render<T: class>(const ACollection: TObjectList<T>;
const ASerializationAction: TMVCSerializationAction<T> = nil); overload;
procedure Render<T: class>(const ACollection: TObjectList<T>; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction<T> = nil); overload;
procedure Render<T: class>(const AStatusCode: Integer; const ACollection: TObjectList<T>;
const AOwns: Boolean; const ASerializationAction: TMVCSerializationAction<T> = nil); overload;
procedure Render<T: class>(const ACollection: TObjectList<T>; const AOwns: Boolean;
const AType: TMVCSerializationType;
const ASerializationAction: TMVCSerializationAction<T> = nil); overload;
procedure Render(const ACollection: IMVCList); overload;
procedure Render(const ACollection: IMVCList; const AType: TMVCSerializationType); overload;
procedure Render(const ATextWriter: TTextWriter; const AOwns: Boolean = True); overload;
procedure Render(const AStream: TStream; const AOwns: Boolean = True); overload;
procedure Render(const AErrorCode: Integer; const AErrorMessage: string = '';
const AErrorClassName: string = ''; const ADataObject: TObject = nil); overload;
procedure Render(const AException: Exception; AExceptionItems: TList<string> = nil;
const AOwns: Boolean = True); overload;
procedure Render(const AResponse: TMVCResponse; const AOwns: Boolean = True); overload;
2019-05-09 20:53:52 +02:00
// Dataset support
procedure Render(const ADataSet: TDataSet;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
2019-03-10 16:29:18 +01:00
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
2019-03-10 16:29:18 +01:00
const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ASerializationType: TMVCDatasetSerializationType;
2019-03-10 16:29:18 +01:00
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
procedure Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase;
2019-03-10 16:29:18 +01:00
const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction = nil); overload;
// SSE Support
procedure RenderSSE(const EventID: string; const EventData: string; EventName: string = '';
const Retry: Integer = TMVCConstants.SSE_RETRY_DEFAULT);
end;
TMVCController = class(TMVCRenderer)
private
FViewModel: TMVCViewDataObject;
FViewDataSets: TMVCViewDataSet;
function GetSession: TWebSession;
2018-01-29 17:30:53 +01:00
function GetViewData(const aModelName: string): TObject;
function GetViewDataset(const aDataSetName: string): TDataSet;
procedure SetViewData(const aModelName: string; const Value: TObject);
procedure SetViewDataset(const aDataSetName: string; const Value: TDataSet);
protected const
CLIENTID_KEY = '__clientid';
protected
procedure MVCControllerAfterCreate; virtual;
procedure MVCControllerBeforeDestroy; virtual;
procedure OnBeforeAction(AContext: TWebContext; const AActionName: string;
var AHandled: Boolean); virtual;
procedure OnAfterAction(AContext: TWebContext; const AActionName: string); virtual;
function GetClientId: string;
function GetCurrentWebModule: TWebModule;
2017-03-20 19:08:01 +01:00
function GetViewModel: TMVCViewDataObject;
function GetViewDataSets: TMVCViewDataSet;
2017-03-20 19:08:01 +01:00
function GetRenderedView(const AViewNames: TArray<string>): string; virtual;
/// <summary>
/// Load mustache view located in TMVCConfigKey.ViewsPath
/// returns the rendered views and generates output using
/// models pushed using Push* methods
/// </summary>
function LoadView(const AViewNames: TArray<string>): string; virtual;
/// <summary>
/// Load a view fragment in the output render stream. The view fragment is appended to the
/// ResponseStream verbatim. No processing happens.
/// Useful when used with cache.
/// It is equivalent to <code>ResponseStream.Append(AViewFragment);</code>
/// </summary>
procedure LoadViewFragment(const AViewFragment: string);
function SessionAs<T: TWebSession>: T;
procedure RaiseSessionExpired; virtual;
2017-10-09 16:17:12 +02:00
// Properties
property Context: TWebContext read GetContext write FContext;
property Session: TWebSession read GetSession;
property ContentType: string read GetContentType write SetContentType;
property StatusCode: Integer read GetStatusCode write SetStatusCode;
property ViewModelList: TMVCViewDataObject read GetViewModel;
property ViewDataSetList: TMVCViewDataSet read GetViewDataSets;
2017-03-20 19:08:01 +01:00
procedure PushObjectToView(const aModelName: string; const AModel: TObject);
deprecated 'Use "ViewData"';
procedure PushDataSetToView(const aModelName: string; const ADataSet: TDataSet);
deprecated 'Use "ViewDataSet"';
2018-01-29 17:30:53 +01:00
property ViewData[const aModelName: string]: TObject read GetViewData write SetViewData;
property ViewDataset[const aDataSetName: string]: TDataSet read GetViewDataset
write SetViewDataset;
2020-08-08 01:03:37 +02:00
public
constructor Create; virtual;
destructor Destroy; override;
end;
TMVCControllerClazz = class of TMVCController;
TMVCControllerCreateAction = reference to function: TMVCController;
TMVCObjectCreatorDelegate = reference to function: TObject;
TMVCControllerDelegate = class
private
FClazz: TMVCControllerClazz;
FCreateAction: TMVCControllerCreateAction;
2017-09-24 19:40:40 +02:00
FURLSegment: string;
protected
{ protected declarations }
public
constructor Create(const AClazz: TMVCControllerClazz;
const ACreateAction: TMVCControllerCreateAction; const AURLSegment: string = '');
property Clazz: TMVCControllerClazz read FClazz;
property CreateAction: TMVCControllerCreateAction read FCreateAction;
2017-09-24 19:40:40 +02:00
property URLSegment: string read FURLSegment;
end;
TMVCStaticContents = class(TMVCController)
private
{ private declarations }
protected
{ protected declarations }
public
class procedure SendFile(const AFileName, AMediaType: string; AContext: TWebContext);
2020-09-16 15:56:14 +02:00
class function IsStaticFile(const AViewPath, AWebRequestPath: string; out ARealFileName: string;
out AIsDirectoryTraversalAttack: Boolean): Boolean;
class function IsScriptableFile(const AStaticFileName: string;
const AConfig: TMVCConfig): Boolean;
end;
/// <summary>
/// Basis Interface for DMVC Middleware.
/// </summary>
IMVCMiddleware = interface
['{3278183A-124A-4214-AB4E-94CA4C22450D}']
/// <summary>
/// Procedure is called before the MVCEngine routes the request to a specific controller/method.
/// </summary>
/// <param name="AContext">Webcontext which contains the complete request and response of the actual call.</param>
/// <param name="AHandled">If set to True the Request would finished. Response must be set by the implementor. Default value is False.</param>
procedure OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
/// <summary>
/// Procedure is called before the specific controller method is called.
/// </summary>
/// <param name="AContext">Webcontext which contains the complete request and response of the actual call.</param>
/// <param name="AControllerQualifiedClassName">Qualified classname of the matching controller.</param>
/// <param name="AActionName">Method name of the matching controller method.</param>
/// <param name="AHandled">If set to True the Request would finished. Response must be set by the implementor. Default value is False.</param>
procedure OnBeforeControllerAction(AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string;
var AHandled: Boolean);
/// <summary>
/// Procedure is called after the specific controller method was called.
/// It is still possible to cancel or to completly modifiy the request.
/// </summary>
/// <param name="AContext">Webcontext which contains the complete request and response of the actual call.</param>
/// <param name="AActionName">Method name of the matching controller method.</param>
/// <param name="AHandled">If set to True the Request would finished. Response must be set by the implementor. Default value is False.</param>
procedure OnAfterControllerAction(AContext: TWebContext; const AActionName: string;
const AHandled: Boolean);
/// <summary>
/// Procedure is called after the MVCEngine routes the request to a specific controller/method.
/// </summary>
/// <param name="AContext">Webcontext which contains the complete request and response of the actual call.</param>
/// <param name="AHandled">If set to True the Request would finished. Response must be set by the implementor. Default value is False.</param>
procedure OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
end;
TMVCExceptionHandlerProc = reference to procedure(E: Exception;
SelectedController: TMVCController; WebContext: TWebContext; var ExceptionHandled: Boolean);
TMVCRouterLogState = (rlsRouteFound, rlsRouteNotFound);
TMVCRouterLogHandlerProc = reference to procedure(const Router: TMVCCustomRouter;
const RouterLogState: TMVCRouterLogState; const WebContext: TWebContext);
2017-05-25 16:57:49 +02:00
TMVCEngine = class(TComponent)
private const
ALLOWED_TYPED_ACTION_PARAMETERS_TYPES =
'Integer, Int64, Single, Double, Extended, Boolean, TDate, TTime, TDateTime, String and TGUID';
private
FViewEngineClass: TMVCViewEngineClass;
FWebModule: TWebModule;
FConfig: TMVCConfig;
FConfigCache_MaxRequestSize: Int64;
FConfigCache_ExposeServerSignature: Boolean;
FConfigCache_ServerSignature: string;
FConfigCache_ExposeXPoweredBy: Boolean;
FConfigCache_DefaultContentType: String;
FConfigCache_DefaultContentCharset: String;
FConfigCache_PathPrefix: String;
FSerializers: TDictionary<string, IMVCSerializer>;
FMiddlewares: TList<IMVCMiddleware>;
FControllers: TObjectList<TMVCControllerDelegate>;
FApplicationSession: TWebApplicationSession;
FSavedOnBeforeDispatch: THTTPMethodEvent;
FOnException: TMVCExceptionHandlerProc;
fOnRouterLog: TMVCRouterLogHandlerProc;
procedure FillActualParamsForAction(const ASelectedController: TMVCController;
const AContext: TWebContext; const AActionFormalParams: TArray<TRttiParameter>;
const AActionName: string; var AActualParams: TArray<TValue>; out ABodyParameter: TObject);
procedure RegisterDefaultsSerializers;
function GetViewEngineClass: TMVCViewEngineClass;
procedure HandleDefaultValueForInjectedParameter(var InjectedParamValue: String;
const InjectableParamAttribute: MVCInjectableParamAttribute);
protected
function GetActualParam(const AFormalParam: TRttiParameter; const AStringValue: String): TValue;
2019-05-09 20:53:52 +02:00
function CustomExceptionHandling(const Ex: Exception; const ASelectedController: TMVCController;
const AContext: TWebContext): Boolean;
procedure ConfigDefaultValues; virtual;
procedure SaveCacheConfigValues;
procedure LoadSystemControllers; virtual;
procedure FixUpWebModule;
procedure ExecuteBeforeRoutingMiddleware(const AContext: TWebContext; var AHandled: Boolean);
procedure ExecuteBeforeControllerActionMiddleware(const AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string;
var AHandled: Boolean);
procedure ExecuteAfterControllerActionMiddleware(const AContext: TWebContext;
const AActionName: string; const AHandled: Boolean);
procedure ExecuteAfterRoutingMiddleware(const AContext: TWebContext; const AHandled: Boolean);
procedure DefineDefaultResponseHeaders(const AContext: TWebContext);
procedure OnBeforeDispatch(ASender: TObject; ARequest: TWebRequest; AResponse: TWebResponse;
var AHandled: Boolean); virtual;
procedure ResponseErrorPage(const AException: Exception; const ARequest: TWebRequest;
const AResponse: TWebResponse); virtual;
function ExecuteAction(const ASender: TObject; const ARequest: TWebRequest;
const AResponse: TWebResponse): Boolean; virtual;
public
class function GetCurrentSession(const ASessionTimeout: Integer; const ASessionId: string;
const ARaiseExceptionIfExpired: Boolean = True): TWebSession; static;
class function ExtractSessionIdFromWebRequest(const AWebRequest: TWebRequest): string; static;
class function SendSessionCookie(const AContext: TWebContext): string; overload; static;
class function SendSessionCookie(const AContext: TWebContext; const ASessionId: string): string;
overload; static;
class procedure ClearSessionCookiesAlreadySet(const ACookies: TCookieCollection); static;
public
constructor Create(const AWebModule: TWebModule; const AConfigAction: TProc<TMVCConfig> = nil;
const ACustomLogger: ILogWriter = nil); reintroduce;
destructor Destroy; override;
function GetSessionBySessionId(const ASessionId: string): TWebSession;
function AddSerializer(const AContentType: string; const ASerializer: IMVCSerializer)
: TMVCEngine;
function AddMiddleware(const AMiddleware: IMVCMiddleware): TMVCEngine;
function AddController(const AControllerClazz: TMVCControllerClazz;
const AURLSegment: string = ''): TMVCEngine; overload;
function AddController(const AControllerClazz: TMVCControllerClazz;
const ACreateAction: TMVCControllerCreateAction; const AURLSegment: string = '')
: TMVCEngine; overload;
function PublishObject(const AObjectCreatorDelegate: TMVCObjectCreatorDelegate;
const AURLSegment: string): TMVCEngine;
function SetViewEngine(const AViewEngineClass: TMVCViewEngineClass): TMVCEngine;
function SetExceptionHandler(const AExceptionHandlerProc: TMVCExceptionHandlerProc): TMVCEngine;
procedure HTTP404(const AContext: TWebContext);
procedure HTTP500(const AContext: TWebContext; const AReasonString: string = '');
procedure SendRawHTTPStatus(const AContext: TWebContext; const HTTPStatusCode: Integer;
const AReasonString: string; const AClassName: string = '');
property ViewEngineClass: TMVCViewEngineClass read GetViewEngineClass;
property WebModule: TWebModule read FWebModule;
property Config: TMVCConfig read FConfig;
property Serializers: TDictionary<string, IMVCSerializer> read FSerializers;
property Middlewares: TList<IMVCMiddleware> read FMiddlewares;
property Controllers: TObjectList<TMVCControllerDelegate> read FControllers;
property ApplicationSession: TWebApplicationSession read FApplicationSession
write FApplicationSession;
property OnRouterLog: TMVCRouterLogHandlerProc read fOnRouterLog write fOnRouterLog;
end;
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
TMVCErrorResponseItem = class
private
FMessage: string;
protected
{ protected declarations }
public
constructor Create(const AMessage: string = '');
2017-05-25 16:57:49 +02:00
property message: string read FMessage write FMessage;
end;
2017-03-29 14:49:35 +02:00
[MVCNameCase(ncLowerCase)]
TMVCResponse = class
private
FStatusCode: Integer;
FReasonString: string;
FMessage: string;
fDataObject: TObject;
protected
{ protected declarations }
public
constructor Create; overload; virtual;
constructor Create(AStatusCode: Integer; AReasonString: string; AMessage: string); overload;
property StatusCode: Integer read FStatusCode write FStatusCode;
property ReasonString: string read FReasonString write FReasonString;
2017-05-25 16:57:49 +02:00
property message: string read FMessage write FMessage;
property Data: TObject read fDataObject write fDataObject;
end;
[MVCNameCase(ncLowerCase)]
TMVCErrorResponse = class(TMVCResponse)
private
FClassname: string;
FItems: TObjectList<TMVCErrorResponseItem>;
FAppErrorCode: Integer;
FDetailedMessage: string;
procedure SetAppErrorCode(const Value: Integer);
public
constructor Create; override;
destructor Destroy; override;
property Classname: string read FClassname write FClassname;
property DetailedMessage: string read FDetailedMessage write FDetailedMessage;
property AppErrorCode: Integer read FAppErrorCode write SetAppErrorCode;
[MVCListOf(TMVCErrorResponseItem)]
property Items: TObjectList<TMVCErrorResponseItem> read FItems;
end;
2014-03-10 17:39:29 +01:00
TMVCBaseViewEngine = class(TMVCBase)
private
FViewName: string;
FWebContext: TWebContext;
FViewModel: TMVCViewDataObject;
FViewDataSets: TObjectDictionary<string, TDataSet>;
FContentType: string;
FOutput: string;
protected
function GetRealFileName(const AViewName: string): string; virtual;
function IsCompiledVersionUpToDate(const AFileName, ACompiledFileName: string): Boolean;
virtual; abstract;
public
constructor Create(const AEngine: TMVCEngine; const AWebContext: TWebContext;
const AViewModel: TMVCViewDataObject;
const AViewDataSets: TObjectDictionary<string, TDataSet>;
const AContentType: string); virtual;
destructor Destroy; override;
2018-01-29 17:30:53 +01:00
procedure Execute(const ViewName: string; const OutputStream: TStream); virtual; abstract;
property ViewName: string read FViewName;
property WebContext: TWebContext read FWebContext;
property ViewModel: TMVCViewDataObject read FViewModel;
property ViewDataSets: TObjectDictionary<string, TDataSet> read FViewDataSets;
property ContentType: string read FContentType;
property Output: string read FOutput;
end;
function IsShuttingDown: Boolean;
2013-10-30 00:48:23 +01:00
procedure EnterInShutdownState;
function CreateResponse(const StatusCode: UInt16; const ReasonString: string;
const Message: string = ''): TMVCResponse;
2013-10-30 00:48:23 +01:00
implementation
uses
IdURI,
MVCFramework.SysControllers,
2019-05-09 20:53:52 +02:00
MVCFramework.Serializer.JsonDataObjects,
MVCFramework.JSONRPC,
MVCFramework.Router,
MVCFramework.Rtti.Utils,
MVCFramework.Serializer.HTML, MVCFramework.Serializer.Abstract;
2013-10-30 01:09:09 +01:00
2013-10-30 00:48:23 +01:00
var
_IsShuttingDown: Int64 = 0;
2017-06-02 00:10:31 +02:00
_MVCGlobalActionParamsCache: TMVCStringObjectDictionary<TMVCActionParamCacheItem> = nil;
2020-08-22 15:37:08 +02:00
_HostingFramework: TMVCHostingFrameworkType = hftUnknown;
2013-10-30 00:48:23 +01:00
function IsShuttingDown: Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := TInterlocked.Read(_IsShuttingDown) = 1
end;
procedure EnterInShutdownState;
begin
TInterlocked.Add(_IsShuttingDown, 1);
2013-10-30 00:48:23 +01:00
end;
function CreateResponse(const StatusCode: UInt16; const ReasonString: string;
const Message: string = ''): TMVCResponse;
2019-05-09 20:53:52 +02:00
begin
Result := TMVCResponse.Create(StatusCode, ReasonString, message);
2019-05-09 20:53:52 +02:00
end;
{ MVCHTTPMethodsAttribute }
constructor MVCHTTPMethodsAttribute.Create(const AMVCHTTPMethods: TMVCHTTPMethods);
begin
inherited Create;
FMVCHTTPMethods := AMVCHTTPMethods;
end;
function MVCHTTPMethodsAttribute.GetMVCHTTPMethodsAsString: string;
2015-04-01 17:01:23 +02:00
var
I: TMVCHTTPMethodType;
2015-04-01 17:01:23 +02:00
begin
Result := '';
for I := low(TMVCHTTPMethodType) to high(TMVCHTTPMethodType) do
if I in FMVCHTTPMethods then
Result := Result + ',' + GetEnumName(TypeInfo(TMVCHTTPMethodType), Ord(I));
2015-04-01 17:01:23 +02:00
if Result <> EmptyStr then
Result := Result.Remove(0, 1)
else
Result := 'any';
end;
{ MVCStringAttribute }
constructor MVCStringAttribute.Create(const AValue: string);
begin
inherited Create;
FValue := AValue;
end;
2016-04-20 11:02:27 +02:00
{ MVCInjectableParamAttribute }
constructor MVCInjectableParamAttribute.Create(const AParamName: string;
const DefaultAsString: string);
begin
Create(AParamName);
FCanBeUsedADefaultValue := True;
FDefaultValueAsString := DefaultAsString;
end;
constructor MVCInjectableParamAttribute.Create(const AParamName: string);
begin
inherited Create;
FParamName := AParamName;
FCanBeUsedADefaultValue := False;
end;
{ MVCProducesAttribute }
constructor MVCProducesAttribute.Create(const AValue, ACharset: string);
begin
Create(AValue);
FCharset := ACharset;
2015-10-18 16:35:50 +02:00
end;
constructor MVCProducesAttribute.Create(const AValue: string);
begin
inherited Create(AValue);
FCharset := TMVCCharset.UTF_8;
2013-10-30 00:48:23 +01:00
end;
{ MVCPathAttribute }
constructor MVCPathAttribute.Create(const APath: string);
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FPath := APath;
2013-10-30 00:48:23 +01:00
end;
constructor MVCResponseAttribute.Create(inStatusCode: Integer; const inDescription: string;
inResponseClass: TClass);
begin
FStatusCode := inStatusCode;
FDescription := inDescription;
FResponseClass := inResponseClass;
end;
{ MVCResponseListAttribute }
constructor MVCResponseListAttribute.Create(inStatusCode: Integer; const inDescription: string;
inResponseClass: TClass);
begin
FStatusCode := inStatusCode;
FDescription := inDescription;
FResponseClass := inResponseClass;
end;
{ MVCStringEnumAttribute }
constructor MVCStringEnumAttribute.Create(const enumValue: string);
begin
fValues := enumValue;
end;
{ TMVCWebRequest }
function TMVCWebRequest.Accept: string;
begin
Result := FWebRequest.Accept;
end;
2020-09-16 15:56:14 +02:00
function TMVCWebRequest.BestAccept: string;
begin
if Accept.Contains(',') then
begin
Result := Accept.Split([','])[0];
end
else
begin
Result := Accept;
end;
end;
function TMVCWebRequest.Body: string;
2013-10-30 00:48:23 +01:00
var
lEncoding: TEncoding;
lCurrCharset: string;
{$IFNDEF BERLINORBETTER}
lBuffer: TArray<Byte>;
{$ENDIF}
begin
{ TODO -oEzequiel -cRefactoring : Refactoring the method TMVCWebRequest.Body }
if (FBody = EmptyStr) then
begin
lCurrCharset := FCharset;
if (lCurrCharset = EmptyStr) then
lCurrCharset := 'UTF-8';
lEncoding := TEncoding.GetEncoding(lCurrCharset);
try
{$IFDEF BERLINORBETTER}
FWebRequest.ReadTotalContent; // Otherwise ISAPI Raises "Empty BODY"
FBody := lEncoding.GetString(FWebRequest.RawContent);
{$ELSE}
SetLength(lBuffer, FWebRequest.ContentLength);
FWebRequest.ReadClient(lBuffer[0], FWebRequest.ContentLength);
FBody := lEncoding.GetString(lBuffer);
{$ENDIF}
finally
lEncoding.Free;
end;
end;
Result := FBody;
end;
function TMVCWebRequest.BodyAs<T>(const RootNode: string): T;
var
Obj: TObject;
lSerializer: IMVCSerializer;
begin
Result := nil;
if FSerializers.TryGetValue(ContentMediaType, lSerializer) then
begin
Obj := TMVCSerializerHelper.CreateObject(TClass(T).QualifiedClassName);
try
lSerializer.DeserializeObject(Body, Obj, TMVCSerializationType.stDefault, nil, RootNode);
Result := Obj as T;
except
on E: Exception do
begin
FreeAndNil(Obj);
raise;
end;
end;
end
else
raise EMVCDeserializationException.CreateFmt('Body ContentType "%s" not supported',
[ContentType]);
end;
function TMVCWebRequest.BodyAsListOf<T>(const RootNode: string): TObjectList<T>;
2015-04-01 17:01:23 +02:00
var
List: TObjectList<T>;
lSerializer: IMVCSerializer;
2015-04-01 17:01:23 +02:00
begin
Result := nil;
if FSerializers.TryGetValue(ContentMediaType, lSerializer) then
begin
List := TObjectList<T>.Create(True);
try
lSerializer.DeserializeCollection(Body, List, T, TMVCSerializationType.stDefault, nil,
RootNode);
Result := List;
except
FreeAndNil(List);
raise;
end;
end
else
raise EMVCException.CreateFmt('Body ContentType "%s" not supported', [ContentType]);
2015-04-01 17:01:23 +02:00
end;
procedure TMVCWebRequest.BodyFor<T>(const AObject: T);
var
lSerializer: IMVCSerializer;
begin
if Assigned(AObject) then
if FSerializers.TryGetValue(ContentMediaType, lSerializer) then
lSerializer.DeserializeObject(Body, AObject)
else
2019-05-09 20:53:52 +02:00
begin
if ContentType.Trim.IsEmpty then
begin
raise EMVCException.Create('Request ContentType header is empty, cannot deserialize body');
end
else
begin
raise EMVCException.CreateFmt('Body ContentType "%s" not supported', [ContentType]);
end;
end;
end;
// function TMVCWebRequest.HeaderNames: TArray<String>;
// var
// lHeaderList: TIdHeaderList;
// I: Integer;
// begin
// EnsureINDY;
// lHeaderList := THackIdHTTPAppRequest(TMVCIndyWebRequest(Self).RawWebRequest).FRequestInfo.RawHeaders;
// SetLength(Result, lHeaderList.Count);
// for I := 0 to Pred(lHeaderList.Count) do
// begin
// Result[I] := lHeaderList.Names[I];
// end;
// end;
procedure TMVCWebRequest.BodyForListOf<T>(const AObjectList: TObjectList<T>);
var
lSerializer: IMVCSerializer;
2015-10-18 16:35:50 +02:00
begin
if Assigned(AObjectList) then
if FSerializers.TryGetValue(ContentMediaType, lSerializer) then
lSerializer.DeserializeCollection(Body, AObjectList, T)
else
raise EMVCException.CreateFmt('Body ContentType "%s" not supported', [ContentType]);
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.ClientIp: string;
var
S: string;
begin
Result := EmptyStr;
if FWebRequest.GetFieldByName('HTTP_CLIENT_IP') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('HTTP_CLIENT_IP'));
2013-10-30 00:48:23 +01:00
for S in string(FWebRequest.GetFieldByName('HTTP_X_FORWARDED_FOR')).Split([',']) do
if not S.Trim.IsEmpty then
Exit(S.Trim);
if FWebRequest.GetFieldByName('HTTP_X_FORWARDED') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('HTTP_X_FORWARDED'));
if FWebRequest.GetFieldByName('HTTP_X_CLUSTER_CLIENT_IP') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('HTTP_X_CLUSTER_CLIENT_IP'));
if FWebRequest.GetFieldByName('HTTP_FORWARDED_FOR') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('HTTP_FORWARDED_FOR'));
2015-10-18 16:35:50 +02:00
if FWebRequest.GetFieldByName('HTTP_FORWARDED') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('HTTP_FORWARDED'));
2013-10-30 00:48:23 +01:00
if FWebRequest.GetFieldByName('REMOTE_ADDR') <> EmptyStr then
Exit(FWebRequest.GetFieldByName('REMOTE_ADDR'));
2013-10-30 00:48:23 +01:00
if FWebRequest.RemoteIP <> EmptyStr then
Exit(FWebRequest.RemoteIP);
2013-10-30 00:48:23 +01:00
if FWebRequest.RemoteAddr <> EmptyStr then
Exit(FWebRequest.RemoteAddr);
2015-10-18 16:35:50 +02:00
if FWebRequest.RemoteHost <> EmptyStr then
Exit(FWebRequest.RemoteHost);
2013-10-30 00:48:23 +01:00
if FWebRequest.RemoteAddr <> EmptyStr then
Exit(FWebRequest.RemoteAddr);
2015-10-18 16:35:50 +02:00
if FWebRequest.RemoteIP <> EmptyStr then
Exit(FWebRequest.RemoteIP);
if FWebRequest.RemoteHost <> EmptyStr then
Exit(FWebRequest.RemoteHost);
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.ClientPrefer(const AMediaType: string): Boolean;
2015-04-01 17:01:23 +02:00
begin
Result := AnsiPos(AMediaType, LowerCase(RawWebRequest.Accept)) = 1;
2015-04-01 17:01:23 +02:00
end;
2017-03-20 19:08:01 +01:00
function TMVCWebRequest.ClientPreferHTML: Boolean;
begin
Result := ClientPrefer(TMVCMediaType.TEXT_HTML);
end;
function TMVCWebRequest.ContentParam(const AName: string): string;
2015-04-01 17:01:23 +02:00
begin
Result := FWebRequest.ContentFields.Values[AName];
2015-04-01 17:01:23 +02:00
end;
function TMVCWebRequest.Cookie(const AName: string): string;
2013-10-30 00:48:23 +01:00
begin
Result := FWebRequest.CookieFields.Values[AName];
2013-10-30 00:48:23 +01:00
end;
constructor TMVCWebRequest.Create(const AWebRequest: TWebRequest;
const ASerializers: TDictionary<string, IMVCSerializer>);
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FBody := EmptyStr;
// FContentType := TMVCConstants.DEFAULT_CONTENT_TYPE;
FCharset := TMVCConstants.DEFAULT_CONTENT_CHARSET;
FWebRequest := AWebRequest;
FSerializers := ASerializers;
FParamsTable := nil;
DefineContentType;
end;
procedure TMVCWebRequest.DefineContentType;
begin
SplitContentMediaTypeAndCharset(FWebRequest.GetFieldByName('Content-Type'), FContentMediaType,
FCharset);
2017-10-09 16:17:12 +02:00
FContentType := BuildContentType(FContentMediaType, FCharset);
2013-10-30 00:48:23 +01:00
end;
destructor TMVCWebRequest.Destroy;
2013-10-30 00:48:23 +01:00
begin
2019-07-26 19:28:45 +02:00
if Assigned(FContentFields) then
begin
FContentFields.Free;
end;
if Assigned(FQueryParams) then
begin
FQueryParams.Free;
end;
2019-07-26 21:30:16 +02:00
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCWebRequest.EnsureINDY;
begin
if not(Self is TMVCIndyWebRequest) then
begin
raise EMVCException.Create(http_status.InternalServerError,
'Method available only in INDY implementation');
end;
end;
procedure TMVCWebRequest.EnsureQueryParamExists(const AName: string);
2013-10-30 00:48:23 +01:00
begin
if GetParams(AName).IsEmpty then
raise EMVCException.CreateFmt('Parameter "%s" required', [AName]);
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetContentFields: TDictionary<string, string>;
var
I: Integer;
begin
2019-07-26 21:30:16 +02:00
if not Assigned(FContentFields) then
2019-07-26 19:28:45 +02:00
begin
2019-07-26 21:30:16 +02:00
FContentFields := TDictionary<string, string>.Create;
for I := 0 to Pred(FWebRequest.ContentFields.Count) do
begin
FContentFields.AddOrSetValue(LowerCase(FWebRequest.ContentFields.Names[I]),
FWebRequest.ContentFields.ValueFromIndex[I]);
end;
end;
2019-07-26 19:28:45 +02:00
Result := FContentFields;
end;
function TMVCWebRequest.GetFiles: TAbstractWebRequestFiles;
begin
Result := FWebRequest.Files;
end;
function TMVCWebRequest.GetHeader(const AName: string): string;
2015-04-01 17:01:23 +02:00
begin
Result := FWebRequest.GetFieldByName(AName);
2015-04-01 17:01:23 +02:00
end;
function TMVCWebRequest.GetHTTPMethod: TMVCHTTPMethodType;
begin
Result := TMVCRouter.StringMethodToHTTPMetod(FWebRequest.Method);
end;
function TMVCWebRequest.GetHTTPMethodAsString: string;
begin
Result := FWebRequest.Method;
end;
function TMVCWebRequest.GetIsAjax: Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := LowerCase(FWebRequest.GetFieldByName('X-Requested-With')) = 'xmlhttprequest';
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetOverwrittenHTTPMethod: TMVCHTTPMethodType;
var
lOverriddenMethod: string;
begin
lOverriddenMethod := Headers[TMVCConstants.X_HTTP_Method_Override];
if lOverriddenMethod.IsEmpty then
begin
Exit(HTTPMethod);
end
else
begin
Result := TMVCRouter.StringMethodToHTTPMetod(FWebRequest.Method);
end;
end;
function TMVCWebRequest.GetParamAsInt64(const AParamName: string): Int64;
2013-10-30 00:48:23 +01:00
begin
Result := StrToInt64(GetParams(AParamName));
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetParamAsInteger(const AParamName: string): Integer;
2013-10-30 00:48:23 +01:00
begin
Result := StrToInt(GetParams(AParamName));
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetParamNames: TArray<string>;
var
I: Integer;
Names: TList<string>;
N: string;
2013-10-30 00:48:23 +01:00
begin
Names := TList<string>.Create;
try
if Assigned(FParamsTable) and (Length(FParamsTable.Keys.ToArray) > 0) then
begin
for N in FParamsTable.Keys.ToArray do
begin
Names.Add(N);
end;
end;
2013-10-30 00:48:23 +01:00
if (FWebRequest.QueryFields.Count > 0) then
begin
for I := 0 to FWebRequest.QueryFields.Count - 1 do
begin
Names.Add(FWebRequest.QueryFields.Names[I]);
end;
end;
2013-10-30 00:48:23 +01:00
if (FWebRequest.ContentFields.Count > 0) then
begin
for I := 0 to FWebRequest.ContentFields.Count - 1 do
begin
if Names.IndexOf(FWebRequest.ContentFields.Names[I]) = -1 then
begin
Names.Add(FWebRequest.ContentFields.Names[I]);
end;
end;
end;
if (FWebRequest.CookieFields.Count > 0) then
begin
for I := 0 to FWebRequest.CookieFields.Count - 1 do
begin
Names.Add(FWebRequest.CookieFields.Names[I]);
end;
end;
Result := Names.ToArray;
finally
Names.Free;
end;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetParams(const AParamName: string): string;
2013-10-30 00:48:23 +01:00
begin
if (not Assigned(FParamsTable)) or (not FParamsTable.TryGetValue(AParamName, Result)) then
begin
Result := '';
if FWebRequest.ContentType.StartsWith(TMVCMediaType.APPLICATION_FORM_URLENCODED, True) or
FWebRequest.ContentType.StartsWith(TMVCMediaType.MULTIPART_FORM_DATA, True) then
Result := FWebRequest.ContentFields.Values[AParamName];
if Result.IsEmpty then
Result := FWebRequest.QueryFields.Values[AParamName];
if Result.IsEmpty then
Result := FWebRequest.CookieFields.Values[AParamName];
end;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetParamsMulti(const AParamName: string): TArray<string>;
var
lList: TList<string>;
procedure AddParamsToList(const AStrings: TStrings; const AList: TList<string>);
var
I: Integer;
begin
for I := 0 to AStrings.Count - 1 do
if SameText(AStrings.Names[I], AParamName) then
AList.Add(AStrings.ValueFromIndex[I]);
end;
begin
lList := TList<string>.Create;
try
AddParamsToList(FWebRequest.ContentFields, lList);
AddParamsToList(FWebRequest.QueryFields, lList);
AddParamsToList(FWebRequest.CookieFields, lList);
Result := lList.ToArray;
finally
lList.Free;
end;
end;
function TMVCWebRequest.GetPathInfo: string;
2013-10-30 00:48:23 +01:00
begin
Result := FWebRequest.PathInfo;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.GetQueryParams: TDictionary<string, string>;
var
I: Integer;
begin
if not Assigned(FQueryParams) then
begin
FQueryParams := TDictionary<string, string>.Create;
for I := 0 to Pred(FWebRequest.QueryFields.Count) do
begin
FQueryParams.Add(LowerCase(FWebRequest.QueryFields.Names[I]),
FWebRequest.QueryFields.ValueFromIndex[I]);
end;
end;
Result := FQueryParams;
end;
function TMVCWebRequest.QueryStringParam(const AName: string): string;
2014-03-25 12:41:23 +01:00
begin
Result := FWebRequest.QueryFields.Values[AName];
2014-03-25 12:41:23 +01:00
end;
function TMVCWebRequest.QueryStringParamExists(const AName: string): Boolean;
begin
Result := QueryStringParam(AName) <> EmptyStr;
end;
function TMVCWebRequest.QueryStringParams: TStrings;
2013-10-30 00:48:23 +01:00
begin
Result := FWebRequest.QueryFields;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.SegmentParam(const AParamName: string; out AValue: string): Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := False;
if Assigned(FParamsTable) then
Result := FParamsTable.TryGetValue(AParamName, AValue);
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.SegmentParamsCount: Integer;
2013-10-30 00:48:23 +01:00
begin
Result := 0;
if Assigned(FParamsTable) then
Result := FParamsTable.Count;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebRequest.ThereIsRequestBody: Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := (FWebRequest.Content <> EmptyStr);
2013-10-30 00:48:23 +01:00
end;
{ TMVCWebResponse }
constructor TMVCWebResponse.Create(const AWebResponse: TWebResponse);
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FWebResponse := AWebResponse;
FFlushOnDestroy := True;
2013-10-30 00:48:23 +01:00
end;
destructor TMVCWebResponse.Destroy;
2013-10-30 00:48:23 +01:00
begin
if FFlushOnDestroy then
begin
try
Flush;
except
// do nothing
end;
end;
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCWebResponse.Flush;
2014-03-25 12:41:23 +01:00
begin
if not FWebResponse.Sent then
FWebResponse.SendResponse;
2014-03-25 12:41:23 +01:00
end;
function TMVCWebResponse.GetContent: string;
2013-10-30 00:48:23 +01:00
begin
Result := FWebResponse.Content;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebResponse.GetContentType: string;
2013-10-30 00:48:23 +01:00
begin
Result := FWebResponse.ContentType;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebResponse.GetCookies: TCookieCollection;
2013-10-30 00:48:23 +01:00
begin
Result := FWebResponse.Cookies;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebResponse.GetCustomHeaders: TStrings;
begin
Result := FWebResponse.CustomHeaders;
2013-10-30 00:48:23 +01:00
end;
function TMVCWebResponse.GetLocation: string;
begin
Result := CustomHeaders.Values['location'];
end;
function TMVCWebResponse.GetReasonString: string;
2013-10-30 00:48:23 +01:00
begin
Result := FWebResponse.ReasonString;
end;
function TMVCWebResponse.GetStatusCode: Integer;
begin
Result := FWebResponse.StatusCode;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCWebResponse.SetContent(const AValue: string);
begin
FWebResponse.Content := AValue;
end;
procedure TMVCWebResponse.SetContentStream(const AStream: TStream; const AContentType: string);
begin
ContentType := AContentType;
FWebResponse.ContentStream := AStream;
end;
procedure TMVCWebResponse.SetContentType(const AValue: string);
begin
FWebResponse.ContentType := '';
FWebResponse.ContentType := AValue;
end;
procedure TMVCWebResponse.SetCustomHeader(const AName, AValue: string);
begin
FWebResponse.SetCustomHeader(AName, AValue);
end;
procedure TMVCWebResponse.SetLocation(const AValue: string);
begin
CustomHeaders.Values['location'] := AValue;
end;
procedure TMVCWebResponse.SetReasonString(const AValue: string);
begin
FWebResponse.ReasonString := AValue;
end;
procedure TMVCWebResponse.SetStatusCode(const AValue: Integer);
begin
FWebResponse.StatusCode := AValue;
end;
{ TUser }
procedure TUser.Clear;
begin
FUserName := EmptyStr;
FLoggedSince := 0;
FRealm := EmptyStr;
FRoles.Clear;
end;
constructor TUser.Create;
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FRoles := TList<string>.Create;
FCustomData := nil;
2013-10-30 00:48:23 +01:00
end;
destructor TUser.Destroy;
2013-10-30 00:48:23 +01:00
begin
FRoles.Free;
FreeAndNil(FCustomData);
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
function TUser.IsValid: Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := (not UserName.IsEmpty) and (LoggedSince > 0);
2013-10-30 00:48:23 +01:00
end;
function TUser.LoadFromSession(const AWebSession: TWebSession): Boolean;
var
SerObj: string;
Pieces: TArray<string>;
I: Integer;
begin
if not Assigned(AWebSession) then
Exit(False);
SerObj := AWebSession[TMVCConstants.CURRENT_USER_SESSION_KEY];
Result := not SerObj.IsEmpty;
if Result then
begin
Clear;
Pieces := SerObj.Split(['$$'], TStringSplitOptions.None);
UserName := Pieces[0];
LoggedSince := ISOTimeStampToDateTime(Pieces[1]);
Realm := Pieces[2];
Roles.Clear;
for I := 3 to Length(Pieces) - 1 do
// https://github.com/danieleteti/delphimvcframework/issues/225
Roles.Add(Pieces[I]);
end;
end;
procedure TUser.SaveToSession(const AWebSession: TWebSession);
var
LRoles: string;
begin
if (FRoles.Count > 0) then
LRoles := string.Join('$$', FRoles.ToArray)
else
LRoles := '';
AWebSession[TMVCConstants.CURRENT_USER_SESSION_KEY] := FUserName + '$$' +
DateTimeToISOTimeStamp(FLoggedSince) + '$$' + FRealm + '$$' + LRoles;
end;
procedure TUser.SetCustomData(const Value: TMVCCustomData);
begin
FCustomData := Value;
end;
procedure TUser.SetLoggedSince(const AValue: TDateTime);
2013-10-30 00:48:23 +01:00
begin
if (FLoggedSince = 0) then
FLoggedSince := AValue
else
raise EMVCException.Create('TUser.LoggedSince already set.');
2013-10-30 00:48:23 +01:00
end;
{ TWebContext }
function TWebContext.AddSessionToTheSessionList(const ASessionType, ASessionId: string;
const ASessionTimeout: Integer): TWebSession;
var
Session: TWebSession;
begin
if (Trim(ASessionType) = EmptyStr) then
raise EMVCException.Create('Empty Session Type');
TMonitor.Enter(GlobalSessionList);
try
Session := TMVCSessionFactory.GetInstance.CreateNewByType(ASessionType, ASessionId,
ASessionTimeout);
GlobalSessionList.Add(ASessionId, Session);
Result := Session;
Session.MarkAsUsed;
finally
TMonitor.Exit(GlobalSessionList);
end;
end;
2013-10-30 00:48:23 +01:00
procedure TWebContext.BindToSession(const ASessionId: string);
2013-10-30 00:48:23 +01:00
begin
if not Assigned(FWebSession) then
begin
FWebSession := TMVCEngine.GetCurrentSession(StrToInt64(FConfig[TMVCConfigKey.SessionTimeout]),
ASessionId, False);
if not Assigned(FWebSession) then
raise EMVCException.Create('Invalid SessionID');
FWebSession.MarkAsUsed;
TMVCEngine.SendSessionCookie(Self, ASessionId);
end
else
raise EMVCException.Create('Session already bounded for this request');
2013-10-30 00:48:23 +01:00
end;
constructor TWebContext.Create(const ARequest: TWebRequest; const AResponse: TWebResponse;
const AConfig: TMVCConfig; const ASerializers: TDictionary<string, IMVCSerializer>);
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FIsSessionStarted := False;
FSessionMustBeClose := False;
FWebSession := nil;
2019-05-19 14:19:58 +02:00
FRequest := nil;
2013-10-30 00:48:23 +01:00
2019-05-19 14:19:58 +02:00
if not IsLibrary then
begin
FRequest := TMVCIndyWebRequest.Create(ARequest, ASerializers);
end
else
begin
{$IFDEF WEBAPACHEHTTP}
2019-05-19 14:19:58 +02:00
if ARequest.ClassType = TApacheRequest then
begin
FRequest := TMVCApacheWebRequest.Create(ARequest, ASerializers)
end
else
{$IFNDEF LINUX}
if ARequest.ClassType = TISAPIRequest then
begin
FRequest := TMVCISAPIWebRequest.Create(ARequest, ASerializers)
end
else
{$ENDIF}
2019-05-19 14:19:58 +02:00
{$ENDIF}
begin
FRequest := TMVCIndyWebRequest.Create(ARequest, ASerializers);
end;
2019-05-19 14:19:58 +02:00
end;
FResponse := TMVCWebResponse.Create(AResponse);
FConfig := AConfig;
FSerializers := ASerializers;
FData := TDictionary<string, string>.Create;
FLoggedUser := nil;
2013-11-05 14:57:50 +01:00
end;
destructor TWebContext.Destroy;
2013-10-30 00:48:23 +01:00
begin
try
FResponse.Free;
except
end;
try
FRequest.Free;
except
end;
try
FData.Free;
except
end;
try
if Assigned(FLoggedUser) then
FLoggedUser.Free;
except
end;
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
procedure TWebContext.Flush;
2013-11-10 01:04:17 +01:00
begin
FResponse.Flush;
2013-11-10 01:04:17 +01:00
end;
2020-08-22 15:37:08 +02:00
function TWebContext.GetHostingFrameworkType: TMVCHostingFrameworkType;
begin
{$IFDEF WEBAPACHEHTTP}
if FRequest.ClassType = TApacheRequest then
begin
Exit(hftApache);
end
else
begin
{$IFNDEF LINUX}
if FRequest.ClassType = TISAPIRequest then
begin
Exit(hftISAPI);
end
else
{$ENDIF}
{$ENDIF}
begin
Exit(hftIndy);
end;
end;
end;
{ MVCFromBodyAttribute }
constructor MVCFromBodyAttribute.Create(const RootNode: string; const DataType: TMVCDataType);
begin
inherited Create;
fRootNode := '';
fDataType := DataType;
end;
function MVCFromBodyAttribute.DataType: TMVCDataType;
begin
Result := fDataType;
end;
function MVCFromBodyAttribute.RootNode: String;
begin
Result := fRootNode;
end;
{ TMVCErrorResponseItem }
constructor TMVCErrorResponseItem.Create(const AMessage: string);
begin
inherited Create;
FMessage := AMessage;
end;
2020-08-22 15:37:08 +02:00
{ TMVCIndyWebRequest }
// function TMVCIndyWebRequest.RawHeaders: TStrings;
// begin
// Result := TMVCHackHTTPAppRequest(FWebRequest).GetHeaders;
// end;
2020-08-22 15:37:08 +02:00
function TWebContext.GetLoggedUser: TUser;
2013-10-30 00:48:23 +01:00
begin
if not Assigned(FLoggedUser) then
FLoggedUser := TUser.Create;
Result := FLoggedUser;
2013-10-30 00:48:23 +01:00
end;
function TWebContext.GetParamsTable: TMVCRequestParamsTable;
begin
Result := FRequest.ParamsTable;
end;
function TWebContext.GetWebSession: TWebSession;
2016-02-27 09:58:54 +01:00
begin
if not Assigned(FWebSession) then
begin
FWebSession := TMVCEngine.GetCurrentSession(StrToInt64(FConfig[TMVCConfigKey.SessionTimeout]),
TMVCEngine.ExtractSessionIdFromWebRequest(FRequest.RawWebRequest), False);
if not Assigned(FWebSession) then
SessionStart
else
TMVCEngine.SendSessionCookie(Self, FWebSession.SessionId);
2016-02-27 09:58:54 +01:00
end;
Result := FWebSession;
Result.MarkAsUsed;
2016-02-27 09:58:54 +01:00
end;
function TWebContext.IsSessionStarted: Boolean;
2017-02-08 11:42:05 +01:00
begin
Result := FIsSessionStarted;
2017-02-08 11:42:05 +01:00
end;
function TWebContext.SendSessionCookie(const AContext: TWebContext): string;
2013-10-30 00:48:23 +01:00
begin
Result := TMVCEngine.SendSessionCookie(Self);
2015-10-18 16:35:50 +02:00
end;
function TWebContext.SessionId: string;
begin
if Assigned(FWebSession) then
Exit(FWebSession.SessionId);
Result := FRequest.Cookie(TMVCConstants.SESSION_TOKEN_NAME);
2013-10-30 00:48:23 +01:00
end;
function TWebContext.SessionMustBeClose: Boolean;
begin
Result := FSessionMustBeClose;
end;
procedure TWebContext.SessionStart;
var
ID: string;
2013-10-30 00:48:23 +01:00
begin
if not Assigned(FWebSession) then
begin
ID := TMVCEngine.SendSessionCookie(Self);
FWebSession := AddSessionToTheSessionList(Config[TMVCConfigKey.SessionType], ID,
StrToInt64(Config[TMVCConfigKey.SessionTimeout]));
FIsSessionStarted := True;
FSessionMustBeClose := False;
end;
2013-10-30 00:48:23 +01:00
end;
function TWebContext.SessionStarted: Boolean;
var
SId: string;
2013-10-30 00:48:23 +01:00
begin
SId := SessionId;
if SId.IsEmpty then
Exit(False);
TMonitor.Enter(GlobalSessionList);
try
Result := GlobalSessionList.ContainsKey(SId);
finally
TMonitor.Exit(GlobalSessionList);
end;
2013-10-30 00:48:23 +01:00
end;
procedure TWebContext.SessionStop(const ARaiseExceptionIfExpired: Boolean);
var
Cookie: TCookie;
SId: string;
2013-10-30 00:48:23 +01:00
begin
FResponse.Cookies.Clear;
2013-10-30 00:48:23 +01:00
Cookie := FResponse.Cookies.Add;
Cookie.name := TMVCConstants.SESSION_TOKEN_NAME;
2013-10-30 00:48:23 +01:00
Cookie.Value := GUIDToString(TGUID.NewGuid) + 'invalid' + GUIDToString(TGUID.NewGuid);
Cookie.Expires := EncodeDate(1970, 1, 1);
Cookie.Path := '/';
TMonitor.Enter(GlobalSessionList);
try
SId := TMVCEngine.ExtractSessionIdFromWebRequest(FRequest.RawWebRequest);
GlobalSessionList.Remove(SId);
finally
TMonitor.Exit(GlobalSessionList);
end;
FIsSessionStarted := False;
FSessionMustBeClose := True;
2013-10-30 00:48:23 +01:00
end;
procedure TWebContext.SetParamsTable(const AValue: TMVCRequestParamsTable);
2013-10-30 00:48:23 +01:00
begin
FRequest.ParamsTable := AValue;
2013-10-30 00:48:23 +01:00
end;
{ TMVCEngine }
function TMVCEngine.AddController(const AControllerClazz: TMVCControllerClazz;
const AURLSegment: string): TMVCEngine;
2013-10-30 00:48:23 +01:00
begin
2017-09-24 19:40:40 +02:00
Result := AddController(AControllerClazz, nil, AURLSegment);
2013-10-30 00:48:23 +01:00
end;
function TMVCEngine.AddController(const AControllerClazz: TMVCControllerClazz;
const ACreateAction: TMVCControllerCreateAction; const AURLSegment: string): TMVCEngine;
begin
2017-09-24 19:40:40 +02:00
FControllers.Add(TMVCControllerDelegate.Create(AControllerClazz, ACreateAction, AURLSegment));
Result := Self;
end;
function TMVCEngine.AddMiddleware(const AMiddleware: IMVCMiddleware): TMVCEngine;
begin
FMiddlewares.Add(AMiddleware);
Result := Self;
2015-10-18 16:35:50 +02:00
end;
function TMVCEngine.AddSerializer(const AContentType: string; const ASerializer: IMVCSerializer)
: TMVCEngine;
begin
FSerializers.AddOrSetValue(AContentType, ASerializer);
Result := Self;
2013-10-30 00:48:23 +01:00
end;
class procedure TMVCEngine.ClearSessionCookiesAlreadySet(const ACookies: TCookieCollection);
var
I: Integer;
SessionCookieName: string;
Cookie: TCookie;
2016-09-25 16:17:37 +02:00
begin
SessionCookieName := TMVCConstants.SESSION_TOKEN_NAME.ToLower;
I := 0;
while True do
begin
if I = ACookies.Count then
Break;
Cookie := ACookies[I];
if LowerCase(Cookie.name) = SessionCookieName then
ACookies.Delete(I)
else
Inc(I);
end;
2016-09-25 16:17:37 +02:00
end;
procedure TMVCEngine.ConfigDefaultValues;
2013-12-04 13:06:18 +01:00
begin
Log.Info('ENTER: Config default values', LOGGERPRO_TAG);
2013-12-04 13:06:18 +01:00
Config[TMVCConfigKey.SessionTimeout] := '30' { 30 minutes };
Config[TMVCConfigKey.DefaultContentType] := TMVCConstants.DEFAULT_CONTENT_TYPE;
Config[TMVCConfigKey.DefaultContentCharset] := TMVCConstants.DEFAULT_CONTENT_CHARSET;
Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
Config[TMVCConfigKey.ViewPath] := 'templates';
2017-06-22 16:18:58 +02:00
Config[TMVCConfigKey.PathPrefix] := '';
Config[TMVCConfigKey.AllowUnhandledAction] := 'false';
Config[TMVCConfigKey.ServerName] := 'DelphiMVCFramework';
Config[TMVCConfigKey.ExposeServerSignature] := 'true';
Config[TMVCConfigKey.ExposeXPoweredBy] := 'true';
Config[TMVCConfigKey.SessionType] := 'memory';
Config[TMVCConfigKey.MaxEntitiesRecordCount] := '20';
Config[TMVCConfigKey.MaxRequestSize] := IntToStr(TMVCConstants.DEFAULT_MAX_REQUEST_SIZE);
Config[TMVCConfigKey.HATEOSPropertyName] := '_links';
Config[TMVCConfigKey.LoadSystemControllers] := 'true';
2017-02-09 11:24:18 +01:00
Log.Info('EXIT: Config default values', LOGGERPRO_TAG);
fOnRouterLog :=
procedure(const Sender: TMVCCustomRouter; const RouterLogState: TMVCRouterLogState;
const Context: TWebContext)
begin
case RouterLogState of
rlsRouteFound:
begin
Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' +
Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> ' +
Sender.GetQualifiedActionName + ' - ' + IntToStr(Context.Response.StatusCode) + ' ' +
Context.Response.ReasonString);
end;
rlsRouteNotFound:
begin
Log(TLogLevel.levNormal, Context.Request.HTTPMethodAsString + ':' +
Context.Request.PathInfo + ' [' + Context.Request.ClientIp + '] -> {NOT FOUND} - ' +
IntToStr(Context.Response.StatusCode) + ' ' + Context.Response.ReasonString);
end;
else
raise EMVCException.Create('Invalid RouterLogState');
end;
end;
2013-10-30 00:48:23 +01:00
end;
constructor TMVCEngine.Create(const AWebModule: TWebModule; const AConfigAction: TProc<TMVCConfig>;
const ACustomLogger: ILogWriter);
2013-10-30 00:48:23 +01:00
begin
2017-05-25 16:57:49 +02:00
inherited Create(AWebModule);
FWebModule := AWebModule;
2017-10-16 22:57:27 +02:00
FixUpWebModule;
FConfig := TMVCConfig.Create;
FSerializers := TDictionary<string, IMVCSerializer>.Create;
FMiddlewares := TList<IMVCMiddleware>.Create;
FControllers := TObjectList<TMVCControllerDelegate>.Create(True);
FApplicationSession := nil;
FSavedOnBeforeDispatch := nil;
2013-10-30 00:48:23 +01:00
WebRequestHandler.CacheConnections := True;
WebRequestHandler.MaxConnections := 4096;
2013-10-30 00:48:23 +01:00
MVCFramework.Logger.SetDefaultLogger(ACustomLogger);
ConfigDefaultValues;
if Assigned(AConfigAction) then
begin
LogEnterMethod('Custom configuration method');
AConfigAction(FConfig);
LogExitMethod('Custom configuration method');
end;
FConfig.Freeze;
SaveCacheConfigValues;
RegisterDefaultsSerializers;
LoadSystemControllers;
end;
function TMVCEngine.CustomExceptionHandling(const Ex: Exception;
const ASelectedController: TMVCController; const AContext: TWebContext): Boolean;
begin
Result := False;
if Assigned(FOnException) then
begin
FOnException(Ex, ASelectedController, AContext, Result);
end;
end;
procedure TMVCEngine.DefineDefaultResponseHeaders(const AContext: TWebContext);
begin
if FConfigCache_ExposeServerSignature and (not IsLibrary) then
AContext.Response.CustomHeaders.Values['Server'] := FConfigCache_ServerSignature;
if FConfigCache_ExposeXPoweredBy then
AContext.Response.CustomHeaders.Values['X-Powered-By'] := 'DMVCFramework ' +
DMVCFRAMEWORK_VERSION;
AContext.Response.RawWebResponse.Date := Now;
end;
destructor TMVCEngine.Destroy;
2013-10-30 00:48:23 +01:00
begin
FConfig.Free;
FSerializers.Free;
FMiddlewares.Free;
FControllers.Free;
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
function TMVCEngine.ExecuteAction(const ASender: TObject; const ARequest: TWebRequest;
const AResponse: TWebResponse): Boolean;
var
lParamsTable: TMVCRequestParamsTable;
lContext: TWebContext;
lRouter: TMVCRouter;
lHandled: Boolean;
lResponseContentMediaType: string;
lResponseContentCharset: string;
lSelectedController: TMVCController;
lActionFormalParams: TArray<TRttiParameter>;
lActualParams: TArray<TValue>;
lBodyParameter: TObject;
begin
Result := False;
2019-03-19 12:06:13 +01:00
if ARequest.ContentLength > FConfigCache_MaxRequestSize then
begin
raise EMVCException.CreateFmt(http_status.RequestEntityTooLarge,
'Request size exceeded the max allowed size [%d KiB] (1)',
2019-03-19 12:06:13 +01:00
[(FConfigCache_MaxRequestSize div 1024)]);
end;
{$IFDEF BERLINORBETTER}
ARequest.ReadTotalContent;
2019-03-19 12:06:13 +01:00
2019-05-09 20:53:52 +02:00
// Double check for malicious content-length header
if ARequest.ContentLength > FConfigCache_MaxRequestSize then
begin
raise EMVCException.CreateFmt(http_status.RequestEntityTooLarge,
'Request size exceeded the max allowed size [%d KiB] (2)',
[(FConfigCache_MaxRequestSize div 1024)]);
end;
2019-03-19 12:06:13 +01:00
{$ENDIF}
lParamsTable := TMVCRequestParamsTable.Create;
try
lContext := TWebContext.Create(ARequest, AResponse, FConfig, FSerializers);
try
DefineDefaultResponseHeaders(lContext);
lHandled := False;
lRouter := TMVCRouter.Create(FConfig, _MVCGlobalActionParamsCache);
try // finally
lSelectedController := nil;
try // only for lSelectedController
try // global exception handler
ExecuteBeforeRoutingMiddleware(lContext, lHandled);
if not lHandled then
begin
if lRouter.ExecuteRouting(ARequest.PathInfo,
lContext.Request.GetOverwrittenHTTPMethod { lContext.Request.HTTPMethod } ,
ARequest.ContentType, ARequest.Accept, FControllers,
FConfigCache_DefaultContentType, FConfigCache_DefaultContentCharset,
FConfigCache_PathPrefix, lParamsTable, lResponseContentMediaType,
lResponseContentCharset) then
begin
try
if Assigned(lRouter.ControllerCreateAction) then
lSelectedController := lRouter.ControllerCreateAction()
else
lSelectedController := lRouter.ControllerClazz.Create;
except
on Ex: Exception do
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")',
[Ex.Classname, Ex.Message, 'Cannot create controller'], LOGGERPRO_TAG);
raise EMVCException.Create(http_status.InternalServerError,
'Cannot create controller');
end;
end;
lSelectedController.Engine := Self;
lSelectedController.Context := lContext;
lSelectedController.ApplicationSession := FApplicationSession;
lContext.ParamsTable := lParamsTable;
ExecuteBeforeControllerActionMiddleware(lContext,
lRouter.ControllerClazz.QualifiedClassName, lRouter.MethodToCall.name, lHandled);
if lHandled then
Exit(True);
lBodyParameter := nil;
lSelectedController.MVCControllerAfterCreate;
try
lHandled := False;
lSelectedController.ContentType := BuildContentType(lResponseContentMediaType,
lResponseContentCharset);
lActionFormalParams := lRouter.MethodToCall.GetParameters;
if (Length(lActionFormalParams) = 0) then
SetLength(lActualParams, 0)
else if (Length(lActionFormalParams) = 1) and
(SameText(lActionFormalParams[0].ParamType.QualifiedName,
'MVCFramework.TWebContext')) then
2019-05-19 14:19:58 +02:00
begin
SetLength(lActualParams, 1);
lActualParams[0] := lContext;
2019-05-19 14:19:58 +02:00
end
else
begin
FillActualParamsForAction(lSelectedController, lContext, lActionFormalParams,
lRouter.MethodToCall.name, lActualParams, lBodyParameter);
end;
lSelectedController.OnBeforeAction(lContext, lRouter.MethodToCall.name, lHandled);
if not lHandled then
begin
try
lRouter.MethodToCall.Invoke(lSelectedController, lActualParams);
finally
lSelectedController.OnAfterAction(lContext, lRouter.MethodToCall.name);
end;
end;
finally
try
lBodyParameter.Free;
except
on E: Exception do
begin
LogE(Format('Cannot free Body object: [CLS: %s][MSG: %s]',
[E.Classname, E.Message]));
end;
end;
lSelectedController.MVCControllerBeforeDestroy;
end;
ExecuteAfterControllerActionMiddleware(lContext, lRouter.MethodToCall.name,
lHandled);
lContext.Response.ContentType := lSelectedController.ContentType;
fOnRouterLog(lRouter, rlsRouteFound, lContext);
end
else // execute-routing
begin
if Config[TMVCConfigKey.AllowUnhandledAction] = 'false' then
begin
lContext.Response.StatusCode := http_status.NotFound;
lContext.Response.ReasonString := 'Not Found';
fOnRouterLog(lRouter, rlsRouteNotFound, lContext);
raise EMVCException.Create(lContext.Response.ReasonString,
lContext.Request.HTTPMethodAsString + ' ' + lContext.Request.PathInfo, 0,
http_status.NotFound);
end
else
begin
lContext.Response.FlushOnDestroy := False;
end;
end; // end-execute-routing
end; // if not handled by beforerouting
except
on ESess: EMVCSessionExpiredException do
begin
if not CustomExceptionHandling(ESess, lSelectedController, lContext) then
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")', [ESess.Classname, ESess.Message,
ESess.DetailedMessage], LOGGERPRO_TAG);
lContext.SessionStop(False);
lSelectedController.ResponseStatus(ESess.HTTPErrorCode);
lSelectedController.Render(ESess);
end;
end;
on E: EMVCException do
begin
if not CustomExceptionHandling(E, lSelectedController, lContext) then
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")',
[E.Classname, E.Message, E.DetailedMessage], LOGGERPRO_TAG);
if Assigned(lSelectedController) then
begin
lSelectedController.ResponseStatus(E.HTTPErrorCode);
lSelectedController.Render(E);
end
else
begin
SendRawHTTPStatus(lContext, E.HTTPErrorCode,
Format('[%s] %s', [E.Classname, E.Message]), E.Classname);
end;
end;
end;
on EIO: EInvalidOp do
begin
if not CustomExceptionHandling(EIO, lSelectedController, lContext) then
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")',
[EIO.Classname, EIO.Message, 'Invalid Op'], LOGGERPRO_TAG);
if Assigned(lSelectedController) then
begin
lSelectedController.ResponseStatus(http_status.InternalServerError);
lSelectedController.Render(EIO);
end
else
begin
SendRawHTTPStatus(lContext, http_status.InternalServerError,
Format('[%s] %s', [EIO.Classname, EIO.Message]), EIO.Classname);
end;
end;
end;
on Ex: Exception do
begin
if not CustomExceptionHandling(Ex, lSelectedController, lContext) then
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")',
[Ex.Classname, Ex.Message, 'Global Action Exception Handler'], LOGGERPRO_TAG);
if Assigned(lSelectedController) then
begin
lSelectedController.ResponseStatus(http_status.InternalServerError);
lSelectedController.Render(Ex);
end
else
begin
SendRawHTTPStatus(lContext, http_status.InternalServerError,
Format('[%s] %s', [Ex.Classname, Ex.Message]), Ex.Classname);
end;
end;
end;
end;
try
ExecuteAfterRoutingMiddleware(lContext, lHandled);
except
on Ex: Exception do
begin
if not CustomExceptionHandling(Ex, lSelectedController, lContext) then
begin
Log.ErrorFmt('[%s] %s (Custom message: "%s")',
[Ex.Classname, Ex.Message, 'After Routing Exception Handler'], LOGGERPRO_TAG);
if Assigned(lSelectedController) then
begin
{ middlewares *must* not raise unhandled exceptions }
lSelectedController.ResponseStatus(http_status.InternalServerError);
lSelectedController.Render(Ex);
end
else
begin
SendRawHTTPStatus(lContext, http_status.InternalServerError,
2019-05-09 20:53:52 +02:00
Format('[%s] %s', [Ex.Classname, Ex.Message]), Ex.Classname);
end;
end;
end;
end;
finally
FreeAndNil(lSelectedController);
end;
finally
lRouter.Free;
end;
finally
lContext.Free;
end;
finally
lParamsTable.Free;
end;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCEngine.ExecuteAfterControllerActionMiddleware(const AContext: TWebContext;
const AActionName: string; const AHandled: Boolean);
var
I: Integer;
2013-11-05 14:57:50 +01:00
begin
for I := 0 to FMiddlewares.Count - 1 do
begin
FMiddlewares[I].OnAfterControllerAction(AContext, AActionName, AHandled);
end;
2013-11-05 14:57:50 +01:00
end;
procedure TMVCEngine.ExecuteAfterRoutingMiddleware(const AContext: TWebContext;
const AHandled: Boolean);
var
I: Integer;
begin
for I := 0 to FMiddlewares.Count - 1 do
begin
FMiddlewares[I].OnAfterRouting(AContext, AHandled);
end;
end;
procedure TMVCEngine.ExecuteBeforeControllerActionMiddleware(const AContext: TWebContext;
const AControllerQualifiedClassName: string; const AActionName: string; var AHandled: Boolean);
var
Middleware: IMVCMiddleware;
2013-10-30 00:48:23 +01:00
begin
if not AHandled then
begin
for Middleware in FMiddlewares do
begin
Middleware.OnBeforeControllerAction(AContext, AControllerQualifiedClassName, AActionName,
AHandled);
if AHandled then
begin
Break;
end;
end;
end;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCEngine.ExecuteBeforeRoutingMiddleware(const AContext: TWebContext;
var AHandled: Boolean);
var
Middleware: IMVCMiddleware;
2013-10-30 00:48:23 +01:00
begin
if not AHandled then
begin
for Middleware in FMiddlewares do
begin
Middleware.OnBeforeRouting(AContext, AHandled);
if AHandled then
begin
Break;
end;
end;
end;
2013-10-30 00:48:23 +01:00
end;
class function TMVCEngine.ExtractSessionIdFromWebRequest(const AWebRequest: TWebRequest): string;
2013-10-30 00:48:23 +01:00
begin
Result := AWebRequest.CookieFields.Values[TMVCConstants.SESSION_TOKEN_NAME];
if not Result.IsEmpty then
Result := TIdURI.URLDecode(Result);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCEngine.FillActualParamsForAction(const ASelectedController: TMVCController;
const AContext: TWebContext; const AActionFormalParams: TArray<TRttiParameter>;
const AActionName: string; var AActualParams: TArray<TValue>; out ABodyParameter: TObject);
var
lParamName: string;
I: Integer;
lStrValue: string;
lFromBodyAttribute: MVCFromBodyAttribute;
lFromQueryStringAttribute: MVCFromQueryStringAttribute;
lFromHeaderAttribute: MVCFromHeaderAttribute;
lFromCookieAttribute: MVCFromCookieAttribute;
lAttributeInjectedParamCount: Integer;
lInjectedParamValue: string;
lList: IMVCList;
lItemClass: TClass;
begin
ABodyParameter := nil;
lAttributeInjectedParamCount := 0;
SetLength(AActualParams, Length(AActionFormalParams));
for I := 0 to Length(AActionFormalParams) - 1 do
begin
lParamName := AActionFormalParams[I].name;
if Length(AActionFormalParams[I].GetAttributes) > 0 then
begin
// Let's check how to inject this parameter
if TRttiUtils.HasAttribute<MVCFromBodyAttribute>(AActionFormalParams[I], lFromBodyAttribute)
then
begin
Inc(lAttributeInjectedParamCount, 1);
if AActionFormalParams[I].ParamType.QualifiedName <> 'System.string' then
begin
ABodyParameter := TRttiUtils.CreateObject(AActionFormalParams[I].ParamType.QualifiedName);
if TDuckTypedList.CanBeWrappedAsList(ABodyParameter, lList) then
begin
lItemClass := TMVCAbstractSerializer(ASelectedController.Serializer).GetObjectTypeOfGenericList(ABodyParameter.ClassInfo);
ASelectedController.Serializer.DeserializeCollection(ASelectedController.Context.Request.Body,
ABodyParameter, lItemClass, stDefault, [], lFromBodyAttribute.RootNode);
end
else
begin
ASelectedController.Serializer.DeserializeObject(ASelectedController.Context.Request.Body,
ABodyParameter, stDefault, [], lFromBodyAttribute.RootNode);
end;
AActualParams[I] := ABodyParameter;
end
else
begin
AActualParams[I] := ASelectedController.Context.Request.Body;
Continue;
end;
end
else if TRttiUtils.HasAttribute<MVCFromQueryStringAttribute>(AActionFormalParams[I],
lFromQueryStringAttribute) then
begin
Inc(lAttributeInjectedParamCount, 1);
lInjectedParamValue := AContext.Request.QueryStringParam
(lFromQueryStringAttribute.ParamName);
HandleDefaultValueForInjectedParameter(lInjectedParamValue, lFromQueryStringAttribute);
AActualParams[I] := GetActualParam(AActionFormalParams[I], lInjectedParamValue);
end
else if TRttiUtils.HasAttribute<MVCFromHeaderAttribute>(AActionFormalParams[I],
lFromHeaderAttribute) then
begin
Inc(lAttributeInjectedParamCount, 1);
lInjectedParamValue := AContext.Request.GetHeader(lFromHeaderAttribute.ParamName);
HandleDefaultValueForInjectedParameter(lInjectedParamValue, lFromHeaderAttribute);
AActualParams[I] := GetActualParam(AActionFormalParams[I], lInjectedParamValue);
end
else if TRttiUtils.HasAttribute<MVCFromCookieAttribute>(AActionFormalParams[I],
lFromCookieAttribute) then
begin
Inc(lAttributeInjectedParamCount, 1);
lInjectedParamValue := AContext.Request.Cookie(lFromCookieAttribute.ParamName);
HandleDefaultValueForInjectedParameter(lInjectedParamValue, lFromCookieAttribute);
AActualParams[I] := GetActualParam(AActionFormalParams[I], lInjectedParamValue);
end
else
begin
raise EMVCException.Create(http_status.InternalServerError,
'Unknown custom attribute on action parameter: ' + AActionFormalParams[I].name +
'. [HINT: Allowed attributes are MVCFromBody, MVCFromQueryString, MVCFromHeader, MVCFromCookie]');
end;
Continue;
end;
// From now on we'll check for url mapped parameters
if not AContext.Request.SegmentParam(lParamName, lStrValue) then
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid parameter %s for action %s (Hint: Here parameters names are case-sensitive)',
[lParamName, AActionName]);
AActualParams[I] := GetActualParam(AActionFormalParams[I], lStrValue);
end;
if (AContext.Request.SegmentParamsCount + lAttributeInjectedParamCount) <>
Length(AActionFormalParams) then
raise EMVCException.CreateFmt(http_status.BadRequest,
'Parameters count mismatch (expected %d actual %d) for action "%s"',
[Length(AActionFormalParams), AContext.Request.SegmentParamsCount, AActionName]);
end;
procedure TMVCEngine.FixUpWebModule;
begin
FSavedOnBeforeDispatch := FWebModule.BeforeDispatch;
FWebModule.BeforeDispatch := OnBeforeDispatch;
end;
function TMVCEngine.GetActualParam(const AFormalParam: TRttiParameter;
const AStringValue: String): TValue;
var lWasDateTime: Boolean; lQualifiedName: String;
lFormatSettings: TFormatSettings;
begin
case AFormalParam.ParamType.TypeKind of
tkInteger:
try
Result := StrToInt(AStringValue);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid Integer value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
end;
end;
tkInt64:
try
Result := StrToInt64(AStringValue);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid Int64 value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
end;
end;
tkUString:
begin
Result := AStringValue;
end;
tkFloat:
begin
lWasDateTime := False;
lQualifiedName := AFormalParam.ParamType.QualifiedName;
if lQualifiedName = 'System.TDate' then
begin
try
lWasDateTime := True;
Result := ISODateToDate(AStringValue);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid TDate value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
2019-05-19 14:19:58 +02:00
end;
end;
end
else if lQualifiedName = 'System.TDateTime' then
begin
try
lWasDateTime := True;
Result := ISOTimeStampToDateTime(AStringValue);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid TDateTime value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
2019-05-19 14:19:58 +02:00
end;
end;
end
else if lQualifiedName = 'System.TTime' then
begin
try
lWasDateTime := True;
Result := ISOTimeToTime(AStringValue);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid TTime value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
end;
end;
end;
if not lWasDateTime then
try
lFormatSettings.DecimalSeparator := '.';
Result := StrToFloat(AStringValue, lFormatSettings);
except
on E: Exception do
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid Float value for param [%s] - [CLASS: %s][MSG: %s]',
[AFormalParam.name, E.Classname, E.Message]);
end;
end;
end;
tkEnumeration:
begin
if AFormalParam.ParamType.QualifiedName = 'System.Boolean' then
begin
if SameText(AStringValue, 'true') or SameText(AStringValue, '1') then
Result := True
else if SameText(AStringValue, 'false') or SameText(AStringValue, '0') then
Result := False
else
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid boolean value for parameter %s. Boolean parameters accepts only "true"/"false" or "1"/"0".',
[AFormalParam.name]);
end;
end
else
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid type for parameter %s. Allowed types are ' +
ALLOWED_TYPED_ACTION_PARAMETERS_TYPES, [AFormalParam.name]);
end;
end;
tkRecord:
begin
if AFormalParam.ParamType.QualifiedName = 'System.TGUID' then
begin
try
Result := TValue.From<TGUID>(TMVCGuidHelper.GuidFromString(AStringValue));
except
raise EMVCException.CreateFmt('Invalid Guid value for param [%s]', [AFormalParam.name]);
end;
end
else
raise EMVCException.CreateFmt('Invalid type for parameter %s. Allowed types are ' +
ALLOWED_TYPED_ACTION_PARAMETERS_TYPES, [AFormalParam.name]);
end
else
begin
raise EMVCException.CreateFmt(http_status.BadRequest,
'Invalid type for parameter %s. Allowed types are ' + ALLOWED_TYPED_ACTION_PARAMETERS_TYPES,
[AFormalParam.name]);
end;
end;
2013-10-30 00:48:23 +01:00
end;
class function TMVCEngine.GetCurrentSession(const ASessionTimeout: Integer;
const ASessionId: string; const ARaiseExceptionIfExpired: Boolean): TWebSession;
var lSessionList: TObjectDictionary<string, TWebSession>;
2013-10-30 00:48:23 +01:00
begin
Result := nil;
lSessionList := GlobalSessionList;
TMonitor.Enter(lSessionList);
try
if not ASessionId.IsEmpty then
begin
if lSessionList.TryGetValue(ASessionId, Result) then
begin
{ https://github.com/danieleteti/delphimvcframework/issues/355 }
if Result.IsExpired then
begin
lSessionList.Remove(ASessionId);
if ARaiseExceptionIfExpired then
begin
raise EMVCSessionExpiredException.Create('Session expired.')
end
else
begin
Result := nil;
end;
end
else
begin
Result.MarkAsUsed;
end;
end;
end;
finally
TMonitor.Exit(lSessionList);
end;
2013-10-30 00:48:23 +01:00
end;
function TMVCEngine.GetSessionBySessionId(const ASessionId: string): TWebSession;
begin
Result := TMVCEngine.GetCurrentSession(StrToInt64(Config[TMVCConfigKey.SessionTimeout]),
ASessionId, False);
if Assigned(Result) then
Result.MarkAsUsed;
end;
function TMVCEngine.GetViewEngineClass: TMVCViewEngineClass;
begin
if FViewEngineClass = nil then
raise EMVCConfigException.Create
('No View Engine configured. [HINT: Use TMVCEngine.SetViewEngine() to set a valid view engine]');
Result := FViewEngineClass;
end;
procedure TMVCEngine.HandleDefaultValueForInjectedParameter(var InjectedParamValue: String;
const InjectableParamAttribute: MVCInjectableParamAttribute);
begin
if InjectedParamValue.IsEmpty then
begin
if InjectableParamAttribute.CanBeUsedADefaultValue then
begin
InjectedParamValue := InjectableParamAttribute.DefaultValueAsString;
end
else
begin
raise EMVCException.CreateFmt
('Required parameter "%s" injected using "%s" has not provided and cannot be used a default value for it',
[InjectableParamAttribute.ParamName, InjectableParamAttribute.Classname]);
end;
end;
end;
procedure TMVCEngine.HTTP404(const AContext: TWebContext);
2013-11-08 23:10:25 +01:00
begin
AContext.Response.SetStatusCode(http_status.NotFound);
AContext.Response.SetContentType(BuildContentType(TMVCMediaType.TEXT_PLAIN,
AContext.Config[TMVCConfigKey.DefaultContentCharset]));
AContext.Response.SetReasonString('Not Found');
AContext.Response.SetContent('Not Found' + sLineBreak + FConfigCache_ServerSignature);
2013-11-08 23:10:25 +01:00
end;
procedure TMVCEngine.HTTP500(const AContext: TWebContext; const AReasonString: string);
2013-10-30 00:48:23 +01:00
begin
AContext.Response.SetStatusCode(http_status.InternalServerError);
AContext.Response.SetContentType(BuildContentType(TMVCMediaType.TEXT_PLAIN,
AContext.Config[TMVCConfigKey.DefaultContentCharset]));
AContext.Response.SetReasonString('Internal server error');
AContext.Response.SetContent('Internal server error' + sLineBreak + FConfigCache_ServerSignature +
': ' + AReasonString);
end;
procedure TMVCEngine.SendRawHTTPStatus(const AContext: TWebContext; const HTTPStatusCode: Integer;
const AReasonString: string; const AClassName: string);
var lSer: IMVCSerializer; lError: TMVCErrorResponse;
begin
2020-09-16 15:56:14 +02:00
lError := TMVCErrorResponse.Create;
try
lError.Classname := AClassName;
lError.StatusCode := HTTPStatusCode;
lError.Message := AReasonString;
if AContext.Request.ClientPreferHTML then
begin
if not Serializers.TryGetValue(TMVCMediaType.TEXT_HTML, lSer) then
begin
raise EMVCConfigException.Create('Cannot find HTML serializer');
end;
AContext.Response.SetContent(lSer.SerializeObject(lError));
2020-09-16 15:56:14 +02:00
AContext.Response.SetContentType(BuildContentType(TMVCMediaType.TEXT_HTML,
AContext.Config[TMVCConfigKey.DefaultContentCharset]));
end
else if AContext.Request.ClientPrefer(AContext.Config[TMVCConfigKey.DefaultContentType]) and
Serializers.TryGetValue(AContext.Config[TMVCConfigKey.DefaultContentType], lSer) then
2020-09-16 15:56:14 +02:00
begin
AContext.Response.SetContent(lSer.SerializeObject(lError));
AContext.Response.SetContentType
(BuildContentType(AContext.Config[TMVCConfigKey.DefaultContentType],
2020-09-16 15:56:14 +02:00
AContext.Config[TMVCConfigKey.DefaultContentCharset]));
end
else
begin
AContext.Response.SetContentType(BuildContentType(TMVCMediaType.TEXT_PLAIN,
AContext.Config[TMVCConfigKey.DefaultContentCharset]));
AContext.Response.SetContent(FConfigCache_ServerSignature + sLineBreak + 'HTTP ' +
HTTPStatusCode.ToString + ': ' + AReasonString);
end;
2020-09-16 15:56:14 +02:00
AContext.Response.SetStatusCode(HTTPStatusCode);
AContext.Response.SetReasonString(AReasonString);
finally
lError.Free;
end;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCEngine.LoadSystemControllers;
2014-04-10 13:56:23 +02:00
begin
if FConfig[TMVCConfigKey.LoadSystemControllers] = 'true' then
begin
Log(TLogLevel.levNormal, 'ENTER: LoadSystemControllers');
AddController(TMVCSystemController);
Log(TLogLevel.levNormal, 'EXIT: LoadSystemControllers');
end;
2014-04-10 13:56:23 +02:00
end;
procedure TMVCEngine.OnBeforeDispatch(ASender: TObject; ARequest: TWebRequest;
AResponse: TWebResponse; var AHandled: Boolean);
2013-10-30 00:48:23 +01:00
begin
AHandled := False;
2017-10-16 22:57:27 +02:00
{ there is a bug in WebBroker Linux on 10.2.1 tokyo }
// if Assigned(FSavedOnBeforeDispatch) then
// begin
// FSavedOnBeforeDispatch(ASender, ARequest, AResponse, AHandled);
// end;
if IsShuttingDown then
begin
AResponse.StatusCode := http_status.ServiceUnavailable;
AResponse.ContentType := TMVCMediaType.TEXT_PLAIN;
AResponse.Content := 'Server is shutting down';
AHandled := True;
end;
if not AHandled then
begin
try
AHandled := ExecuteAction(ASender, ARequest, AResponse);
if not AHandled then
begin
AResponse.ContentStream := nil;
end;
except
on E: Exception do
begin
Log.ErrorFmt('[%s] %s', [E.Classname, E.Message], LOGGERPRO_TAG);
AResponse.StatusCode := http_status.InternalServerError; // default is Internal Server Error
if E is EMVCException then
begin
AResponse.StatusCode := (E as EMVCException).HTTPErrorCode;
end;
AResponse.Content := E.Message;
AResponse.SendResponse;
AHandled := True;
end;
end;
end;
2013-10-30 00:48:23 +01:00
end;
function TMVCEngine.PublishObject(const AObjectCreatorDelegate: TMVCObjectCreatorDelegate;
const AURLSegment: string): TMVCEngine;
begin
Result := AddController(TMVCJSONRPCPublisher,
function: TMVCController
begin
Result := TMVCJSONRPCPublisher.Create(AObjectCreatorDelegate(), True);
end, AURLSegment);
end;
procedure TMVCEngine.RegisterDefaultsSerializers;
var lDefaultSerializerContentType: string;
2013-10-30 00:48:23 +01:00
begin
// lDefaultSerializerContentType := BuildContentType(TMVCMediaType.APPLICATION_JSON, TMVCCharset.UTF_8);
// if not FSerializers.ContainsKey(lDefaultSerializerContentType) then
// begin
// FSerializers.Add(lDefaultSerializerContentType, TMVCJSONDataObjectsSerializer.Create);
// end;
2017-09-22 09:33:21 +02:00
// register the same serializer without the charset in the contenttype
2017-10-09 16:17:12 +02:00
lDefaultSerializerContentType := BuildContentType(TMVCMediaType.APPLICATION_JSON, '');
2017-09-22 09:33:21 +02:00
if not FSerializers.ContainsKey(lDefaultSerializerContentType) then
begin
2020-09-16 15:56:14 +02:00
FSerializers.Add(lDefaultSerializerContentType, TMVCJSONDataObjectsSerializer.Create(Config));
end;
// this is used only for exceptions (dt 2020-09-16)
lDefaultSerializerContentType := BuildContentType(TMVCMediaType.TEXT_HTML, '');
if not FSerializers.ContainsKey(lDefaultSerializerContentType) then
begin
FSerializers.Add(lDefaultSerializerContentType, TMVCHTMLSerializer.Create(Config));
end;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCEngine.ResponseErrorPage(const AException: Exception; const ARequest: TWebRequest;
const AResponse: TWebResponse);
2013-10-30 00:48:23 +01:00
begin
AResponse.SetCustomHeader('x-mvc-error', AException.Classname + ': ' + AException.Message);
AResponse.StatusCode := http_status.OK;
2019-05-19 14:19:58 +02:00
begin
AResponse.ContentType := TMVCMediaType.TEXT_PLAIN;
AResponse.Content := Config[TMVCConfigKey.ServerName] + ' ERROR:' + sLineBreak +
'Exception raised of class: ' + AException.Classname + sLineBreak +
'***********************************************' + sLineBreak + AException.Message +
sLineBreak + '***********************************************';
end;
2013-10-30 00:48:23 +01:00
end;
class function TMVCEngine.SendSessionCookie(const AContext: TWebContext): string;
var SId: string;
begin
SId := StringReplace(StringReplace(StringReplace('DT' + GUIDToString(TGUID.NewGuid), '}', '', []),
'{', '', []), '-', '', [rfReplaceAll]);
Result := SendSessionCookie(AContext, SId);
end;
procedure TMVCEngine.SaveCacheConfigValues;
begin
FConfigCache_MaxRequestSize := StrToInt64Def(Config[TMVCConfigKey.MaxRequestSize],
TMVCConstants.DEFAULT_MAX_REQUEST_SIZE);
FConfigCache_ExposeServerSignature := Config[TMVCConfigKey.ExposeServerSignature] = 'true';
FConfigCache_ServerSignature := Config[TMVCConfigKey.ServerName];
FConfigCache_ExposeXPoweredBy := Config[TMVCConfigKey.ExposeXPoweredBy] = 'true';
FConfigCache_DefaultContentType := Config[TMVCConfigKey.DefaultContentType];
FConfigCache_DefaultContentCharset := Config[TMVCConfigKey.DefaultContentCharset];
FConfigCache_PathPrefix := Config[TMVCConfigKey.PathPrefix];
end;
class function TMVCEngine.SendSessionCookie(const AContext: TWebContext;
const ASessionId: string): string;
var Cookie: TCookie; SessionTimeout: Integer;
begin
ClearSessionCookiesAlreadySet(AContext.Response.Cookies);
Cookie := AContext.Response.Cookies.Add;
Cookie.name := TMVCConstants.SESSION_TOKEN_NAME;
Cookie.Value := ASessionId;
2017-06-22 16:18:58 +02:00
if not TryStrToInt(AContext.Config[TMVCConfigKey.SessionTimeout], SessionTimeout) then
raise EMVCException.Create('[Config::Session Timeout] is not a valid integer');
if SessionTimeout = 0 then
2017-06-22 16:18:58 +02:00
Cookie.Expires := 0 // session cookie
else
Cookie.Expires := Now + OneMinute * SessionTimeout;
2017-06-22 16:18:58 +02:00
Cookie.Path := '/';
Result := ASessionId;
end;
function TMVCEngine.SetExceptionHandler(const AExceptionHandlerProc: TMVCExceptionHandlerProc)
: TMVCEngine;
begin
FOnException := AExceptionHandlerProc;
Result := Self;
end;
function TMVCEngine.SetViewEngine(const AViewEngineClass: TMVCViewEngineClass): TMVCEngine;
begin
FViewEngineClass := AViewEngineClass;
Result := Self;
end;
{ TMVCBase }
class function TMVCBase.GetApplicationFileName: string;
// var
// Name: PChar;
// Size: Integer;
begin
Result := GetModuleName(HInstance);
// Result := EmptyStr;
// Name := GetMemory(2048);
// try
// GetModuleName()
// Size := GetModuleFileName(0, Name, 2048);
// if Size > 0 then
// Result := Name;
// finally
// FreeMem(Name, 2048);
// end;
end;
class function TMVCBase.GetApplicationFileNamePath: string;
2013-10-30 00:48:23 +01:00
begin
Result := IncludeTrailingPathDelimiter(ExtractFilePath(GetApplicationFileName));
2013-10-30 00:48:23 +01:00
end;
function TMVCBase.GetApplicationSession: TWebApplicationSession;
2013-11-12 01:23:50 +01:00
begin
if not Assigned(FApplicationSession) then
raise EMVCException.CreateFmt('ApplicationSession not assigned to this %s instance.',
[Classname]);
Result := FApplicationSession;
end;
2013-11-12 01:23:50 +01:00
function TMVCBase.GetConfig: TMVCConfig;
begin
Result := Engine.Config;
2013-11-12 01:23:50 +01:00
end;
function TMVCBase.GetEngine: TMVCEngine;
2013-10-30 00:48:23 +01:00
begin
if not Assigned(FEngine) then
raise EMVCException.CreateFmt('MVCEngine not assigned to this %s instance.', [Classname]);
Result := FEngine;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCBase.SetApplicationSession(const AValue: TWebApplicationSession);
2013-10-30 00:48:23 +01:00
begin
FApplicationSession := AValue;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCBase.SetEngine(const AValue: TMVCEngine);
2013-10-30 00:48:23 +01:00
begin
FEngine := AValue;
2013-10-30 00:48:23 +01:00
end;
{ TMVCControllerDelegate }
2013-10-30 00:48:23 +01:00
constructor TMVCControllerDelegate.Create(const AClazz: TMVCControllerClazz;
const ACreateAction: TMVCControllerCreateAction; const AURLSegment: string = '');
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FClazz := AClazz;
FCreateAction := ACreateAction;
2017-09-24 19:40:40 +02:00
FURLSegment := AURLSegment;
2013-10-30 00:48:23 +01:00
end;
{ TMVCStaticContents }
class function TMVCStaticContents.IsScriptableFile(const AStaticFileName: string;
const AConfig: TMVCConfig): Boolean;
2013-10-30 00:48:23 +01:00
begin
Result := TPath.GetExtension(AStaticFileName).ToLower = '.' +
AConfig[TMVCConfigKey.DefaultViewFileExtension].ToLower;
end;
2013-10-30 00:48:23 +01:00
class function TMVCStaticContents.IsStaticFile(const AViewPath, AWebRequestPath: string;
2020-09-14 15:52:50 +02:00
out ARealFileName: string; out AIsDirectoryTraversalAttack: Boolean): Boolean;
var lFileName: string; lWebRoot: string;
begin
2020-09-14 15:52:50 +02:00
AIsDirectoryTraversalAttack := False;
if TDirectory.Exists(AViewPath) then
begin
lWebRoot := TPath.GetFullPath(AViewPath);
end
else
begin
lWebRoot := TPath.GetFullPath(GetApplicationFileNamePath + AViewPath);
end;
2020-09-16 15:56:14 +02:00
lFileName := TPath.Combine(lWebRoot, AWebRequestPath.Replace('/', TPath.DirectorySeparatorChar));
if not TPath.HasValidPathChars(lFileName, True) then
begin
Exit(False);
end;
lFileName := TPath.GetFullPath(lFileName);
if not lFileName.StartsWith(lWebRoot) then
// AVOID PATH TRAVERSAL
begin
2020-09-14 15:52:50 +02:00
AIsDirectoryTraversalAttack := True;
Exit(False);
end;
ARealFileName := lFileName;
Result := TFile.Exists(ARealFileName);
2013-10-30 00:48:23 +01:00
end;
class procedure TMVCStaticContents.SendFile(const AFileName, AMediaType: string;
AContext: TWebContext);
var FileDate: TDateTime; ReqDate: TDateTime; S: TFileStream;
begin
2020-09-11 18:14:28 +02:00
if not FileExists(AFileName) then
begin
AContext.Response.StatusCode := 404;
end
else
begin
2020-09-11 18:14:28 +02:00
FileDate := IndyFileAge(AFileName);
ReqDate := GMTToLocalDateTime(AContext.Request.Headers['If-Modified-Since']);
if (ReqDate <> 0) and (abs(ReqDate - FileDate) < 2 * (1 / (24 * 60 * 60))) then
begin
AContext.Response.ContentType := AMediaType;
AContext.Response.StatusCode := 304;
end
else
begin
S := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
AContext.Response.SetCustomHeader('Last-Modified', LocalDateTimeToHttpStr(FileDate));
AContext.Response.SetContentStream(S, AMediaType);
end;
end;
2013-10-30 00:48:23 +01:00
end;
{ TMVCController }
constructor TMVCController.Create;
2013-10-30 00:48:23 +01:00
begin
inherited Create;
FContext := nil;
FContentCharset := TMVCConstants.DEFAULT_CONTENT_CHARSET;
FResponseStream := nil;
2017-03-20 19:08:01 +01:00
FViewModel := nil;
FViewDataSets := nil;
2013-10-30 00:48:23 +01:00
end;
destructor TMVCController.Destroy;
2013-10-30 00:48:23 +01:00
begin
if Assigned(FResponseStream) then
FResponseStream.Free;
2017-03-20 19:08:01 +01:00
if Assigned(FViewModel) then
FViewModel.Free;
if Assigned(FViewDataSets) then
FViewDataSets.Free;
inherited Destroy;
2013-10-30 00:48:23 +01:00
end;
function TMVCController.GetClientId: string;
2013-10-30 00:48:23 +01:00
begin
Result := Session[CLIENTID_KEY];
if Result.IsEmpty then
raise EMVCException.Create('Invalid ClientID' + sLineBreak +
'Hint: Messaging extensions require a valid clientid. Did you call /messages/clients/YOUR_CLIENT_ID ?');
2013-10-30 00:48:23 +01:00
end;
function TMVCRenderer.GetContentType: string;
2013-10-30 00:48:23 +01:00
begin
Result := GetContext.Response.ContentType.Trim;
if Result.IsEmpty then
begin
GetContext.Response.ContentType := FContext.FConfig
[MVCFramework.Commons.TMVCConfigKey.DefaultContentType];
Result := GetContentType;
end;
2013-10-30 00:48:23 +01:00
end;
function TMVCRenderer.GetContext: TWebContext;
2013-10-30 00:48:23 +01:00
begin
if not Assigned(FContext) then
raise EMVCException.CreateFmt('Context already set on %s.', [Classname]);
Result := FContext;
2013-10-30 00:48:23 +01:00
end;
function TMVCController.GetCurrentWebModule: TWebModule;
2013-10-30 00:48:23 +01:00
begin
Result := Engine.WebModule;
2013-10-30 00:48:23 +01:00
end;
function TMVCController.GetSession: TWebSession;
2013-10-30 00:48:23 +01:00
begin
Result := GetContext.Session;
2013-10-30 00:48:23 +01:00
end;
function TMVCRenderer.GetStatusCode: Integer;
2013-10-30 00:48:23 +01:00
begin
Result := GetContext.Response.StatusCode;
2013-10-30 00:48:23 +01:00
end;
2018-01-29 17:30:53 +01:00
function TMVCController.GetViewData(const aModelName: string): TObject;
begin
if not FViewModel.TryGetValue(aModelName, Result) then
Result := nil;
end;
2018-01-29 17:30:53 +01:00
function TMVCController.GetViewDataset(const aDataSetName: string): TDataSet;
begin
if not FViewDataSets.TryGetValue(aDataSetName, Result) then
Result := nil;
end;
function TMVCController.GetViewDataSets: TMVCViewDataSet;
2017-03-20 19:08:01 +01:00
begin
if not Assigned(FViewDataSets) then
FViewDataSets := TMVCViewDataSet.Create;
2017-03-20 19:08:01 +01:00
Result := FViewDataSets;
end;
function TMVCController.GetViewModel: TMVCViewDataObject;
begin
if not Assigned(FViewModel) then
FViewModel := TMVCViewDataObject.Create;
Result := FViewModel;
end;
function TMVCController.LoadView(const AViewNames: TArray<string>): string;
begin
try
Result := GetRenderedView(AViewNames);
ResponseStream.Append(Result);
except
on E: Exception do
begin
Log.ErrorFmt('[%s] %s', [E.Classname, E.Message], LOGGERPRO_TAG);
2020-09-16 15:56:14 +02:00
raise;
2017-03-20 19:08:01 +01:00
end;
end;
end;
procedure TMVCController.LoadViewFragment(const AViewFragment: string);
begin
ResponseStream.Append(AViewFragment);
end;
procedure TMVCController.MVCControllerAfterCreate;
begin
{ Implement if need be. }
end;
procedure TMVCController.MVCControllerBeforeDestroy;
begin
{ Implement if need be. }
end;
procedure TMVCController.OnAfterAction(AContext: TWebContext; const AActionName: string);
begin
{ Implement if need be. }
end;
2013-10-30 00:48:23 +01:00
procedure TMVCController.OnBeforeAction(AContext: TWebContext; const AActionName: string;
var AHandled: Boolean);
2013-10-30 00:48:23 +01:00
begin
AHandled := False;
if ContentType.IsEmpty then
ContentType := Config[TMVCConfigKey.DefaultContentType];
{ Implement if need be. }
2013-10-30 00:48:23 +01:00
end;
procedure TMVCController.PushDataSetToView(const aModelName: string; const ADataSet: TDataSet);
2017-03-20 19:08:01 +01:00
begin
GetViewDataSets.Add(aModelName, ADataSet);
2017-03-20 19:08:01 +01:00
end;
procedure TMVCController.PushObjectToView(const aModelName: string; const AModel: TObject);
2017-03-20 19:08:01 +01:00
begin
GetViewModel.Add(aModelName, AModel);
2017-03-20 19:08:01 +01:00
end;
procedure TMVCController.RaiseSessionExpired;
begin
raise EMVCSessionExpiredException.Create('Session expired.');
end;
procedure TMVCRenderer.Redirect(const AUrl: string);
2013-10-30 00:48:23 +01:00
begin
GetContext.Response.RawWebResponse.SendRedirect(AUrl);
2013-10-30 00:48:23 +01:00
end;
2019-05-09 20:53:52 +02:00
procedure TMVCRenderer.Render(const AObject: TObject; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction = nil);
begin
2019-03-10 16:29:18 +01:00
Render(AObject, AOwns, stDefault, ASerializationAction);
end;
procedure TMVCRenderer.Render(const AContent: string);
var lContentType: string;
lOutEncoding: TEncoding; lCharset: string;
begin
SplitContentMediaTypeAndCharset(GetContentType, lContentType, lCharset);
if lCharset.IsEmpty then
lCharset := TMVCConstants.DEFAULT_CONTENT_CHARSET;
if lContentType.IsEmpty then
lContentType := TMVCConstants.DEFAULT_CONTENT_TYPE;
lContentType := BuildContentType(lContentType, lCharset);
lOutEncoding := TEncoding.GetEncoding(lCharset);
try
if SameText('UTF-8', UpperCase(lCharset)) then
GetContext.Response.SetContentStream(TStringStream.Create(AContent, TEncoding.UTF8),
lContentType)
else
begin
GetContext.Response.SetContentStream(TBytesStream.Create(TEncoding.Convert(TEncoding.Default,
lOutEncoding, TEncoding.Default.GetBytes(AContent))), lContentType);
end;
finally
lOutEncoding.Free;
end;
2015-10-18 16:35:50 +02:00
end;
procedure TMVCRenderer.Render<T>(const ACollection: TObjectList<T>; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction<T>);
begin
Self.Render<T>(ACollection, AOwns, stDefault, ASerializationAction);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.Render202Accepted(const HREF: string; const ID: string;
const Reason: string);
begin
if HREF.IsEmpty then
begin
raise EMVCException.Create('Cannot send 202 without provide an HREF');
end;
ResponseStatus(http_status.Accepted, Reason);
Render(TMVCAcceptedResponse.Create(HREF, ID));
end;
procedure TMVCRenderer.Render201Created(const Location, Reason: string);
2019-08-28 00:18:30 +02:00
begin
if not Location.IsEmpty then
begin
FContext.Response.CustomHeaders.AddPair('location', Location);
end;
ResponseStatus(http_status.Created, Reason);
{$IF CompilerVersion >= 34}
Render(''); // in 10.4 INDY requires something on the content
{$ENDIF}
end;
procedure TMVCRenderer.Render204NoContent(const Location, Reason: string);
begin
if not Location.IsEmpty then
begin
FContext.Response.CustomHeaders.AddPair('location', Location);
end;
ResponseStatus(http_status.NoContent, Reason);
2019-08-28 00:18:30 +02:00
end;
procedure TMVCRenderer.ResponseStatus(const AStatusCode: Integer; const AReasonString: string);
begin
SetStatusCode(AStatusCode);
GetContext.Response.ReasonString := AReasonString;
end;
function TMVCRenderer.ResponseStream: TStringBuilder;
begin
if not Assigned(FResponseStream) then
FResponseStream := TStringBuilder.Create;
Result := FResponseStream;
2015-10-18 16:35:50 +02:00
end;
function TMVCRenderer.Serializer: IMVCSerializer;
2015-10-18 16:35:50 +02:00
begin
Result := Serializer(GetContentType);
2015-10-18 16:35:50 +02:00
end;
procedure TMVCRenderer.SendFile(const AFileName: string);
begin
TMVCStaticContents.SendFile(AFileName, GetContentType, GetContext);
end;
procedure TMVCRenderer.SendStream(const AStream: TStream; const AOwns: Boolean;
const ARewind: Boolean);
var lTemp: TStream;
2013-10-30 00:48:23 +01:00
begin
if ARewind then
begin
AStream.Position := 0;
end;
lTemp := TMemoryStream.Create;
try
lTemp.CopyFrom(AStream, 0);
lTemp.Position := 0;
except
lTemp.Free;
raise;
end;
if AOwns then
begin
AStream.Free;
end;
GetContext.Response.RawWebResponse.Content := EmptyStr;
GetContext.Response.RawWebResponse.ContentType := GetContentType;
GetContext.Response.RawWebResponse.ContentStream := lTemp;
GetContext.Response.RawWebResponse.FreeContentStream := True;
2013-10-30 00:48:23 +01:00
end;
function TMVCRenderer.Serializer(const AContentType: string;
const ARaiseExceptionIfNotExists: Boolean): IMVCSerializer;
var lContentMediaType: string;
2019-03-06 12:00:56 +01:00
lContentCharSet: string;
2014-04-15 17:03:47 +02:00
begin
2019-03-06 12:00:56 +01:00
SplitContentMediaTypeAndCharset(AContentType.ToLower, lContentMediaType, lContentCharSet);
if Engine.Serializers.ContainsKey(lContentMediaType) then
begin
Result := Engine.Serializers.Items[lContentMediaType];
end
else
begin
if ARaiseExceptionIfNotExists then
begin
raise EMVCException.CreateFmt('The serializer for %s could not be found.',
[lContentMediaType]);
end
else
begin
Result := nil;
end;
end;
2014-04-15 17:03:47 +02:00
end;
function TMVCController.SessionAs<T>: T;
2016-04-22 09:46:21 +02:00
begin
Result := Session as T;
2016-04-22 09:46:21 +02:00
end;
procedure TMVCRenderer.SetContentType(const AValue: string);
2013-10-30 00:48:23 +01:00
begin
GetContext.Response.ContentType := AValue;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.SetStatusCode(const AValue: Integer);
begin
GetContext.Response.StatusCode := AValue;
end;
function TMVCRenderer.ToMVCList(const AObject: TObject; AOwnsObject: Boolean): IMVCList;
begin
Result := MVCFramework.DuckTyping.WrapAsList(AObject, AOwnsObject);
end;
2018-01-29 17:30:53 +01:00
procedure TMVCController.SetViewData(const aModelName: string; const Value: TObject);
begin
GetViewModel.Add(aModelName, Value);
end;
procedure TMVCController.SetViewDataset(const aDataSetName: string; const Value: TDataSet);
begin
GetViewDataSets.Add(aDataSetName, Value);
end;
procedure TMVCRenderer.Render(const AObject: TObject; const AOwns: Boolean;
const AType: TMVCSerializationType; const ASerializationAction: TMVCSerializationAction = nil);
begin
try
2019-03-10 16:29:18 +01:00
Render(Serializer(GetContentType).SerializeObject(AObject, AType, [], ASerializationAction));
finally
if AOwns then
AObject.Free;
end;
end;
procedure TMVCRenderer.Render(const AStream: TStream; const AOwns: Boolean);
2013-10-30 00:48:23 +01:00
begin
SendStream(AStream, AOwns);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.Render(const AErrorCode: Integer;
const AErrorMessage, AErrorClassName: string; const ADataObject: TObject);
var R: TMVCErrorResponse;
2013-10-30 00:48:23 +01:00
begin
ResponseStatus(AErrorCode, AErrorMessage);
R := TMVCErrorResponse.Create;
try
R.StatusCode := AErrorCode;
if ((R.StatusCode div 100) = 2) then
R.ReasonString := 'ok'
else
R.ReasonString := 'error';
R.Message := AErrorMessage;
R.Classname := AErrorClassName;
R.Data := ADataObject;
Render(R, False, stProperties);
finally
R.Free;
end;
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase;
const ASerializationType: TMVCDatasetSerializationType;
2019-05-09 20:53:52 +02:00
const ASerializationAction: TMVCDatasetSerializationAction);
2017-03-29 14:49:35 +02:00
begin
if Assigned(ADataSet) then
begin
2017-03-30 17:00:04 +02:00
try
2019-05-09 20:53:52 +02:00
case ASerializationType of
dstSingleRecord:
begin
Render(Serializer(GetContentType).SerializeDataSetRecord(ADataSet, AIgnoredFields,
ANameCase, ASerializationAction))
2019-05-09 20:53:52 +02:00
end;
dstAllRecords:
begin
Render(Serializer(GetContentType).SerializeDataSet(ADataSet, AIgnoredFields, ANameCase,
ASerializationAction))
end
else
2019-05-09 20:53:52 +02:00
begin
raise EMVCSerializationException.Create('Invalid dataset serialization type');
end;
end;
// if ASerializationType = dstSingleRecord then
// Render(Serializer(GetContentType).SerializeDataSetRecord(ADataSet, AIgnoredFields, ANameCase,
// ASerializationAction))
// else
// Render(Serializer(GetContentType).SerializeDataSet(ADataSet, AIgnoredFields, ANameCase, ASerializationAction))
2017-03-30 17:00:04 +02:00
finally
if AOwns then
ADataSet.Free;
end;
2017-03-29 14:49:35 +02:00
end
else
raise EMVCException.Create('Can not render an empty dataset.');
end;
procedure TMVCRenderer.Render(const AStatusCode: Integer; const AObject: IInterface;
const ASerializationAction: TMVCSerializationAction);
2020-04-24 14:48:30 +02:00
begin
SetStatusCode(AStatusCode);
Render(AObject, ASerializationAction);
end;
procedure TMVCRenderer.Render(const AObject: IInterface;
const ASerializationAction: TMVCSerializationAction);
begin
Render(TObject(AObject), False, ASerializationAction);
end;
procedure TMVCRenderer.Render(const AStatusCode: Integer; AObject: TObject; const AOwns: Boolean;
const ASerializationAction: TMVCSerializationAction);
begin
ResponseStatus(AStatusCode);
Render(AObject, AOwns, ASerializationAction);
end;
procedure TMVCRenderer.Render(const AStatusCode: Integer; const AObject: TObject;
const ASerializationAction: TMVCSerializationAction);
begin
ResponseStatus(AStatusCode);
Render(AObject, True, ASerializationAction);
end;
procedure TMVCRenderer.Render<T>(const ACollection: TObjectList<T>; const AOwns: Boolean;
const AType: TMVCSerializationType; const ASerializationAction: TMVCSerializationAction<T>);
var lSerializationAction: TMVCSerializationAction;
2013-10-30 00:48:23 +01:00
begin
if Assigned(ACollection) then
begin
try
if Assigned(ASerializationAction) then
begin
2019-05-09 20:53:52 +02:00
lSerializationAction := procedure(const AObject: TObject; const Dict: IMVCLinks)
begin
ASerializationAction(T(AObject), Dict);
end;
end
else
begin
lSerializationAction := nil;
end;
Render(Serializer(GetContentType).SerializeCollection(ACollection, AType, [],
lSerializationAction));
finally
if AOwns then
ACollection.Free;
end;
end
else
raise EMVCException.Create('Can not render an empty collection.');
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.Render<T>(const AStatusCode: Integer; const ACollection: TObjectList<T>;
const AOwns: Boolean; const ASerializationAction: TMVCSerializationAction<T>);
begin
SetStatusCode(AStatusCode);
Render<T>(ACollection, AOwns, ASerializationAction);
end;
2017-03-20 19:08:01 +01:00
function TMVCController.GetRenderedView(const AViewNames: TArray<string>): string;
var lView: TMVCBaseViewEngine; lViewName: string; lStrStream: TStringStream;
2017-03-20 19:08:01 +01:00
begin
2018-01-29 17:30:53 +01:00
lStrStream := TStringStream.Create('', TEncoding.UTF8);
2017-03-20 19:08:01 +01:00
try
lView := FEngine.ViewEngineClass.Create(Engine, Context, ViewModelList, ViewDataSetList,
ContentType);
2017-03-20 19:08:01 +01:00
try
2018-01-29 17:30:53 +01:00
for lViewName in AViewNames do
2017-03-20 19:08:01 +01:00
begin
2018-01-29 17:30:53 +01:00
lView.Execute(lViewName, lStrStream);
2017-03-20 19:08:01 +01:00
end;
2018-01-29 17:30:53 +01:00
finally
lView.Free;
2017-03-20 19:08:01 +01:00
end;
2018-01-29 17:30:53 +01:00
lStrStream.Position := 0;
Result := lStrStream.DataString;
2017-03-20 19:08:01 +01:00
finally
2018-01-29 17:30:53 +01:00
lStrStream.Free;
2017-03-20 19:08:01 +01:00
end;
end;
procedure TMVCRenderer.Render<T>(const ACollection: TObjectList<T>;
const ASerializationAction: TMVCSerializationAction<T>);
2013-10-30 00:48:23 +01:00
begin
Self.Render<T>(ACollection, True, ASerializationAction);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.RenderResponseStream;
2013-10-30 00:48:23 +01:00
begin
Render(ResponseStream.ToString);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.RenderSSE(const EventID, EventData: string; EventName: string;
const Retry: Integer);
2017-10-09 16:17:12 +02:00
begin
// setting up the correct SSE headers
SetContentType('text/event-stream');
GetContext.Response.SetCustomHeader('Cache-Control', 'no-cache');
GetContext.Response.StatusCode := http_status.OK;
2017-10-09 16:17:12 +02:00
// render the response using SSE compliant data format
// current event id (the client will resend this number at the next request)
ResponseStream.Append(Format('id: %s'#13, [EventID]));
// The browser attempts to reconnect to the source roughly 3 seconds after
// each connection is closed. You can change that timeout by including a line
// beginning with "retry:", followed by the number of milliseconds to wait
// before trying to reconnect.
if Retry > -1 then
begin
ResponseStream.Append(Format('retry: %d'#13, [Retry]));
end;
if not EventName.IsEmpty then
begin
ResponseStream.Append(Format('event: %s'#13, [EventName]));
end;
// actual message
ResponseStream.Append('data: ' + EventData + #13#13);
// render all the stuff
RenderResponseStream;
end;
procedure TMVCRenderer.Render(const ACollection: IMVCList);
2013-10-30 00:48:23 +01:00
begin
Render(ACollection, stDefault);
2013-10-30 00:48:23 +01:00
end;
procedure TMVCRenderer.Render(const ACollection: IMVCList; const AType: TMVCSerializationType);
2013-11-08 23:10:25 +01:00
begin
if Assigned(ACollection) then
Render(Serializer(GetContentType).SerializeCollection(TObject(ACollection), AType))
else
raise EMVCException.Create('Can not render an empty collection.');
2013-11-08 23:10:25 +01:00
end;
procedure TMVCRenderer.Render(const ATextWriter: TTextWriter; const AOwns: Boolean);
2013-11-08 23:10:25 +01:00
begin
if Assigned(ATextWriter) then
begin
try
Render(ATextWriter.ToString);
finally
if AOwns then
ATextWriter.Free;
end;
end
else
raise EMVCException.Create('Can not render an empty textwriter.');
2013-11-08 23:10:25 +01:00
end;
procedure TMVCRenderer.Render(const AException: Exception; AExceptionItems: TList<string>;
const AOwns: Boolean);
var S: string; R: TMVCErrorResponse; I: TMVCErrorResponseItem;
2013-11-08 23:10:25 +01:00
begin
try
if AException is EMVCException then
ResponseStatus(EMVCException(AException).HTTPErrorCode, AException.Message + ' [' +
AException.Classname + ']');
2013-11-08 23:10:25 +01:00
if (GetContext.Response.StatusCode = http_status.OK) then
ResponseStatus(http_status.InternalServerError, AException.Message + ' [' +
AException.Classname + ']');
// if (not GetContext.Request.IsAjax) and (GetContext.Request.ClientPrefer(TMVCMediaType.TEXT_HTML)) then
// begin
// SetContentType(TMVCMediaType.TEXT_HTML);
// Render(AException, False);
// exit;
// ResponseStream.Clear;
// ResponseStream.Append
// ('<html><head><style>pre { padding: 15px; color: #000000; background-color: #e0e0e0; }</style></head><body>')
// .Append('<h1>' + Config[TMVCConfigKey.ServerName] + ': Error Raised</h1>')
// .AppendFormat('<pre>HTTP Return Code: %d' + sLineBreak,
// [GetContext.Response.StatusCode]).AppendFormat('HTTP Reason Text: "%s"</pre>',
// [GetContext.Response.ReasonString])
// .Append('<h3><pre>').AppendFormat('Exception Class Name : %s' + sLineBreak, [AException.Classname])
// .AppendFormat('Exception Message : %s' + sLineBreak, [AException.Message]).Append('</pre></h3>');
// if Assigned(AExceptionItems) and (AExceptionItems.Count > 0) then
// begin
// ResponseStream.Append('<h2><pre>');
// for S in AExceptionItems do
// ResponseStream.AppendLine('- ' + S);
// ResponseStream.Append('</pre><h2>');
// end
// else
// begin
// ResponseStream.AppendLine('<pre>No other information available</pre>');
// end;
// ResponseStream.Append('</body></html>');
// RenderResponseStream;
// end
// else
begin
R := TMVCErrorResponse.Create;
try
R.StatusCode := GetContext.Response.StatusCode;
R.ReasonString := 'error';
R.Message := AException.Message;
R.Classname := AException.Classname;
if AException is EMVCException then
begin
R.AppErrorCode := EMVCException(AException).ApplicationErrorCode;
R.DetailedMessage := EMVCException(AException).DetailedMessage;
for S in EMVCException(AException).ErrorItems do
begin
R.Items.Add(TMVCErrorResponseItem.Create(S));
end;
end;
if Assigned(AExceptionItems) and (AExceptionItems.Count > 0) then
begin
for S in AExceptionItems do
begin
I := TMVCErrorResponseItem.Create;
I.Message := S;
R.Items.Add(I);
end;
end;
2020-09-16 15:56:14 +02:00
if Serializer(GetContentType, False) = nil then
begin
2020-09-16 15:56:14 +02:00
if Serializer(FContext.Request.BestAccept, False) <> nil then
GetContext.Response.ContentType := FContext.Request.BestAccept
else
GetContext.Response.ContentType := GetConfig[TMVCConfigKey.DefaultContentType];
end;
Render(R, False);
finally
R.Free;
end;
end;
finally
if AOwns then
AExceptionItems.Free;
end;
2015-04-01 17:01:23 +02:00
end;
procedure TMVCRenderer.Render(const AResponse: TMVCResponse; const AOwns: Boolean);
2015-04-01 17:01:23 +02:00
begin
if Assigned(AResponse) then
begin
try
GetContext.Response.StatusCode := AResponse.StatusCode;
Render(AResponse, False, stProperties);
finally
if AOwns then
AResponse.Free;
end;
end
else
raise EMVCException.Create('Cannot render an empty response object.');
2015-04-01 17:01:23 +02:00
end;
procedure TMVCRenderer.Render(const ADataSet: TDataSet;
const ASerializationAction: TMVCDatasetSerializationAction);
begin
2019-03-10 16:29:18 +01:00
Render(ADataSet, True, ASerializationAction);
end;
2019-05-09 20:53:52 +02:00
procedure TMVCRenderer.Render(const ADataSet: TDataSet; const AOwns: Boolean;
const ASerializationAction: TMVCDatasetSerializationAction);
2015-04-01 17:01:23 +02:00
begin
2019-03-10 16:29:18 +01:00
Render(ADataSet, AOwns, dstAllRecords, ASerializationAction);
2015-04-01 17:01:23 +02:00
end;
procedure TMVCRenderer.Render(const AObject: TObject;
const ASerializationAction: TMVCSerializationAction = nil);
2015-04-01 17:01:23 +02:00
begin
2019-03-10 16:29:18 +01:00
Render(AObject, True, ASerializationAction);
2015-04-01 17:01:23 +02:00
end;
procedure TMVCRenderer.Render(const ADataSet: TDataSet; const AOwns: Boolean;
const AIgnoredFields: TMVCIgnoredList; const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction);
begin
2019-03-10 16:29:18 +01:00
Render(ADataSet, AOwns, AIgnoredFields, ncLowerCase, ASerializationType, ASerializationAction);
2015-04-01 17:01:23 +02:00
end;
procedure TMVCRenderer.Render(const ADataSet: TDataSet; const AOwns: Boolean;
2019-05-09 20:53:52 +02:00
const ASerializationType: TMVCDatasetSerializationType;
const ASerializationAction: TMVCDatasetSerializationAction);
2015-04-01 17:01:23 +02:00
begin
2019-03-10 16:29:18 +01:00
Render(ADataSet, AOwns, [], ASerializationType, ASerializationAction);
2015-04-01 17:01:23 +02:00
end;
constructor TMVCResponse.Create;
begin
inherited Create;
end;
2015-04-01 17:01:23 +02:00
constructor TMVCErrorResponse.Create;
2015-04-01 17:01:23 +02:00
begin
inherited Create;
FItems := TObjectList<TMVCErrorResponseItem>.Create(True);
2015-04-01 17:01:23 +02:00
end;
constructor TMVCResponse.Create(AStatusCode: Integer; AReasonString, AMessage: string);
2017-05-09 23:13:51 +02:00
begin
Create;
StatusCode := AStatusCode;
ReasonString := AReasonString;
2017-05-25 16:57:49 +02:00
message := AMessage;
2017-05-09 23:13:51 +02:00
end;
destructor TMVCErrorResponse.Destroy;
begin
FItems.Free;
inherited Destroy;
end;
procedure TMVCErrorResponse.SetAppErrorCode(const Value: Integer);
begin
FAppErrorCode := Value;
end;
{ TMVCBaseView }
constructor TMVCBaseViewEngine.Create(const AEngine: TMVCEngine; const AWebContext: TWebContext;
const AViewModel: TMVCViewDataObject; const AViewDataSets: TObjectDictionary<string, TDataSet>;
const AContentType: string);
begin
inherited Create;
Engine := AEngine;
FWebContext := AWebContext;
FViewModel := AViewModel;
FViewDataSets := AViewDataSets;
FContentType := AContentType;
FOutput := EmptyStr;
end;
destructor TMVCBaseViewEngine.Destroy;
begin
inherited Destroy;
end;
function TMVCBaseViewEngine.GetRealFileName(const AViewName: string): string;
var lFileName: string; lDefaultViewFileExtension: string;
begin
lDefaultViewFileExtension := Config[TMVCConfigKey.DefaultViewFileExtension];
lFileName := StringReplace(AViewName, '/', '\', [rfReplaceAll]);
if (lFileName = '\') then
begin
lFileName := '\index.' + lDefaultViewFileExtension
end
else
begin
lFileName := lFileName + '.' + lDefaultViewFileExtension;
end;
if DirectoryExists(Config[TMVCConfigKey.ViewPath]) then
begin
lFileName := ExpandFileName(IncludeTrailingPathDelimiter(Config.Value[TMVCConfigKey.ViewPath]) +
lFileName)
end
else
begin
lFileName := ExpandFileName(IncludeTrailingPathDelimiter(GetApplicationFileNamePath +
Config.Value[TMVCConfigKey.ViewPath]) + lFileName);
end;
if FileExists(lFileName) then
Result := lFileName
else
Result := EmptyStr;
end;
{ MVCIntegerAttribute }
constructor MVCIntegerAttribute.Create(const AValue: Int64);
begin
FValue := AValue;
end;
{ MVCPathParamAttribute }
constructor MVCPathParamAttribute.Create(AType: TSwagTypeParameter; APattern, AFormat: string);
begin
FType := AType;
FValue := APattern;
FFormat := AFormat;
end;
{ MVCParamAttribute }
constructor MVCParamAttribute.Create(name: string; Location: TSwagRequestParameterInLocation;
AType: TSwagTypeParameter; APattern, AFormat: string);
begin
FName := name;
FLocation := Location;
FType := AType;
FPattern := APattern;
FFormat := AFormat;
end;
constructor MVCParamAttribute.Create(name: string; Location: TSwagRequestParameterInLocation;
AType: TClass; APattern, AFormat: string);
begin
FName := name;
FLocation := Location;
FClassType := AType;
FPattern := APattern;
FFormat := AFormat;
end;
2020-08-22 15:37:08 +02:00
{ TMVCHackHTTPAppRequest }
// function TMVCHackHTTPAppRequest.GetHeaders: TStringList;
// begin
// Result := FRequestInfo.RawHeaders;
// end;
2020-08-22 15:37:08 +02:00
2013-10-30 00:48:23 +01:00
initialization
_IsShuttingDown := 0;
2017-06-02 00:10:31 +02:00
_MVCGlobalActionParamsCache := TMVCStringObjectDictionary<TMVCActionParamCacheItem>.Create;
finalization
FreeAndNil(_MVCGlobalActionParamsCache);
2013-10-30 00:48:23 +01:00
end.