2016-06-22 17:49:16 +02:00
// ***************************************************************************
//
// Delphi MVC Framework
//
2021-02-25 10:50:57 +01:00
// Copyright (c) 2010-2021 Daniele Teti and the DMVCFramework Team
2016-06-22 17:49:16 +02:00
//
// https://github.com/danieleteti/delphimvcframework
//
2017-03-20 13:55:46 +01:00
// Collaborators on this file: Ezequiel Juliano M<> ller (ezequieljuliano@gmail.com)
//
2016-06-22 17:49:16 +02:00
// ***************************************************************************
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// *************************************************************************** }
2015-12-22 12:38:17 +01:00
2013-10-30 00:48:23 +01:00
unit MVCFramework. Commons;
2017-01-18 21:53:53 +01:00
{$I dmvcframework.inc}
2017-03-23 18:51:25 +01:00
interface
2013-10-30 00:48:23 +01:00
uses
2019-03-08 14:26:20 +01:00
System. Classes,
2017-03-13 20:52:11 +01:00
System. SysUtils,
System. SyncObjs,
2017-03-20 13:55:46 +01:00
System. IOUtils,
2017-11-23 17:29:51 +01:00
Data. DB,
2017-03-20 13:55:46 +01:00
IdGlobal,
2019-08-28 00:18:30 +02:00
IdCoderMIME,
IdContext,
2020-04-18 23:32:24 +02:00
System. Generics. Collections,
2020-09-22 15:18:22 +02:00
MVCFramework. DuckTyping,
2019-08-28 00:18:30 +02:00
JsonDataObjects;
2013-10-30 00:48:23 +01:00
2017-01-29 16:11:04 +01:00
{$I dmvcframeworkbuildconsts.inc}
2019-03-16 17:20:28 +01:00
2013-10-30 00:48:23 +01:00
type
2017-03-13 20:52:11 +01:00
2019-03-16 17:20:28 +01:00
TMVCHTTPMethodType = ( httpGET, httpPOST, httpPUT, httpDELETE, httpHEAD, httpOPTIONS, httpPATCH,
httpTRACE) ;
2017-03-13 20:52:11 +01:00
2017-01-18 21:53:53 +01:00
TMVCHTTPMethods = set of TMVCHTTPMethodType;
2015-12-22 12:16:30 +01:00
2017-03-13 20:52:11 +01:00
TMVCMediaType = record
2015-12-22 12:16:30 +01:00
public const
APPLICATION_ATOM_XML = 'application/atom+xml' ;
APPLICATION_JSON = 'application/json' ;
APPLICATION_OCTET_STREAM = 'application/octet-stream' ;
APPLICATION_SVG_XML = 'application/svg+xml' ;
APPLICATION_XHTML_XML = 'application/xhtml+xml' ;
APPLICATION_XML = 'application/xml' ;
2017-03-13 20:52:11 +01:00
APPLICATION_OCTETSTREAM = 'application/octet-stream' ;
2015-12-22 12:16:30 +01:00
MEDIA_TYPE_WILDCARD = '*' ;
MULTIPART_FORM_DATA = 'multipart/form-data' ;
2019-03-16 17:20:28 +01:00
APPLICATION_FORM_URLENCODED = 'application/x-www-form-urlencoded' ;
2015-12-22 12:16:30 +01:00
TEXT_HTML = 'text/html' ;
TEXT_PLAIN = 'text/plain' ;
TEXT_XML = 'text/xml' ;
TEXT_CSS = 'text/css' ;
TEXT_JAVASCRIPT = 'text/javascript' ;
TEXT_CACHEMANIFEST = 'text/cache-manifest' ;
TEXT_EVENTSTREAM = 'text/event-stream' ;
2017-03-13 20:52:11 +01:00
TEXT_CSV = 'text/csv' ;
2015-12-22 12:16:30 +01:00
IMAGE_JPEG = 'image/jpeg' ;
2019-03-05 20:55:37 +01:00
IMAGE_X_PNG = 'image/x-png' ;
2020-04-02 15:28:06 +02:00
IMAGE_X_ICON = 'image/x-icon' ;
2019-03-05 20:55:37 +01:00
IMAGE_PNG = 'image/png' ;
2020-04-25 15:24:08 +02:00
IMAGE_SVG_XML = 'image/svg+xml' ;
IMAGE_GIF = 'image/gif' ;
2019-03-08 07:15:23 +01:00
APPLICATION_PDF = 'application/pdf' ;
APPLICATION_X_PDF = 'application/x-pdf' ;
2015-12-22 12:16:30 +01:00
WILDCARD = '*/*' ;
end ;
2017-03-13 20:52:11 +01:00
TMVCCharSet = record
2015-12-22 12:16:30 +01:00
public const
US_ASCII = 'US-ASCII' ;
WINDOWS_1250 = 'windows-1250' ;
WINDOWS_1251 = 'windows-1251' ;
WINDOWS_1252 = 'windows-1252' ;
WINDOWS_1253 = 'windows-1253' ;
WINDOWS_1254 = 'windows-1254' ;
WINDOWS_1257 = 'windows-1257' ;
ISO88591 = 'ISO-8859-1' ;
ISO88592 = 'ISO-8859-2' ;
ISO88593 = 'ISO-8859-3' ;
ISO88594 = 'ISO-8859-4' ;
ISO88595 = 'ISO-8859-5' ;
ISO88596 = 'ISO-8859-6' ;
ISO88597 = 'ISO-8859-7' ;
ISO88598 = 'ISO-8859-8' ;
ISO885915 = 'ISO-8859-15' ;
UTF_8 = 'UTF-8' ;
UTF_16 = 'UTF-16' ;
UTF_16BE = 'UTF-16BE' ;
UTF_16LE = 'UTF-16LE' ;
2015-12-16 16:41:06 +01:00
end ;
2017-03-13 20:52:11 +01:00
TMVCConstants = record
2015-12-16 16:41:06 +01:00
public const
SESSION_TOKEN_NAME = 'dtsessionid' ;
DEFAULT_CONTENT_CHARSET = 'UTF-8' ;
2016-01-01 23:00:22 +01:00
DEFAULT_CONTENT_TYPE = TMVCMediaType. APPLICATION_JSON;
2015-12-16 16:41:06 +01:00
CURRENT_USER_SESSION_KEY = '__DMVC_CURRENT_USER__' ;
LAST_AUTHORIZATION_HEADER_VALUE = '__DMVC_LAST_AUTHORIZATION_HEADER_VALUE_' ;
2017-10-09 16:17:12 +02:00
SSE_RETRY_DEFAULT = 1 0 0 ;
SSE_LAST_EVENT_ID = 'Last-Event-ID' ;
2020-03-08 17:54:10 +01:00
URL_MAPPED_PARAMS_ALLOWED_CHARS = ' <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ' '"@\[\]\{\}\(\)\=;&#\.:!\_,%\w\d\x2D\x3A' ;
2018-12-17 00:39:29 +01:00
OneMiB = 1 0 4 8 5 7 6 ;
OneKiB = 1 0 2 4 ;
2019-01-08 12:48:27 +01:00
DEFAULT_MAX_REQUEST_SIZE = OneMiB * 5 ; // 5 MiB
2020-05-02 16:39:32 +02:00
HATEOAS_PROP_NAME = 'links' ;
2019-05-10 00:46:03 +02:00
X_HTTP_Method_Override = 'X-HTTP-Method-Override' ;
2020-09-30 11:16:10 +02:00
MAX_RECORD_COUNT = 2 0 ;
2019-05-08 09:10:42 +02:00
end ;
HATEOAS = record
public const
/// <summary>
/// Target URI: It indicates the target resource URI. This is represented by the href attribute.
/// </summary>
HREF = 'href' ;
/// <summary>
/// Link relation: The link relation type describes how the current context is related to the target resource. This is represented by the rel attribute.
/// </summary>
REL = 'rel' ;
/// <summary>
/// Type: This indicates the expected resource media type. This is represented by the type attribute.
/// </summary>
_TYPE = 'type' ;
2015-12-16 16:41:06 +01:00
end ;
2017-03-13 20:52:11 +01:00
TMVCConfigKey = record
public const
SessionTimeout = 'sessiontimeout' ;
ViewPath = 'view_path' ;
DefaultContentType = 'default_content_type' ;
DefaultContentCharset = 'default_content_charset' ;
DefaultViewFileExtension = 'default_view_file_extension' ;
2017-06-22 16:18:58 +02:00
PathPrefix = 'pathprefix' ;
2017-03-13 20:52:11 +01:00
AllowUnhandledAction = 'allow_unhandled_action' ;
ServerName = 'server_name' ;
ExposeServerSignature = 'server_signature' ;
2020-06-29 19:11:41 +02:00
ExposeXPoweredBy = 'xpoweredby' ;
2017-03-13 20:52:11 +01:00
SessionType = 'session_type' ;
2018-10-23 16:18:34 +02:00
MaxEntitiesRecordCount = 'max_entities_record_count' ;
2019-01-08 12:48:27 +01:00
MaxRequestSize = 'max_request_size' ; // bytes
2019-03-08 09:33:41 +01:00
HATEOSPropertyName = 'hateos' ;
2020-04-29 17:53:29 +02:00
LoadSystemControllers = 'load_system_controllers' ;
2017-03-13 20:52:11 +01:00
end ;
2020-08-22 15:37:08 +02:00
TMVCHostingFrameworkType = ( hftUnknown, hftIndy, hftApache, hftISAPI) ;
2015-12-16 16:41:06 +01:00
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
HTTP_STATUS = record
const
// Informational 1xx
Continue = 1 0 0 ;
SwitchingProtocols = 1 0 1 ;
// Successful 2xx
/// <summary>
/// 200 OK
/// The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:
/// GET an entity corresponding to the requested resource is sent in the response;
/// HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;
/// POST an entity describing or containing the result of the action;
/// TRACE an entity containing the request message as received by the end server.
/// </summary>
OK = 2 0 0 ;
/// <summary>
/// 201 Created
/// The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD 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. The entity format is specified by the media type given in the Content-Type header field. 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.
/// A 201 response MAY contain an ETag response header field indicating the current value of the entity tag for the requested variant just created
/// </summary>
Created = 2 0 1 ;
/// <summary>
/// 202 Accepted
/// The request has been accepted for processing, but the processing has not been completed. The request might or might not eventually be acted upon, as it might be disallowed when processing actually takes place. There is no facility for re-sending a status code from an asynchronous operation such as this.
/// The 202 response is intentionally non-committal. Its purpose is to 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 include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.
/// </summary>
Accepted = 2 0 2 ;
2017-03-20 13:55:46 +01:00
///
2015-12-16 16:41:06 +01:00
NonAuthoritativeInformation = 2 0 3 ;
/// <summary>
/// 204 No Content
/// The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.
/// If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
/// The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.
/// </summary>
NoContent = 2 0 4 ;
ResetContent = 2 0 5 ;
PartialContent = 2 0 6 ;
// Redirection 3xx
MultipleChoices = 3 0 0 ;
/// <summary>
/// 301 Moved Permanently
/// The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.
/// The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
/// If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
/// Note: When automatically redirecting a POST request after
/// receiving a 301 status code, some existing HTTP/1.0 user agents
/// will erroneously change it into a GET request.
/// </summary>
MovedPermanently = 3 0 1 ;
/// <summary>
/// 302 Found
/// The requested resource resides temporarily under a different URI. Since the redirection might be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field.
/// The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
/// If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
/// </summary>
Found = 3 0 2 ;
/// <summary>
/// 303 See Other
/// The response to the request can be found under a different URI and SHOULD be retrieved using a GET method on that resource. This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource. The new URI is not a substitute reference for the originally requested resource. The 303 response MUST NOT be cached, but the response to the second (redirected) request might be cacheable.
/// The different URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).
/// </summary>
SeeOther = 3 0 3 ;
/// <summary>
/// 304 Not Modified
/// If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.
/// The response MUST include the following header fields:
/// - Date, unless its omission is required by section 14.18.1
/// If a clockless origin server obeys these rules, and proxies and clients add their own Date to any response received without one (as already specified by [RFC 2068], section 14.19), caches will operate correctly.
/// - ETag and/or Content-Location, if the header would have been sent in a 200 response to the same request
/// - Expires, Cache-Control, and/or Vary, if the field-value might differ from that sent in any previous response for the same variant
/// If the conditional GET used a strong cache validator (see section 13.3.3), the response SHOULD NOT include other entity-headers. Otherwise (i.e., the conditional GET used a weak validator), the response MUST NOT include other entity-headers; this prevents inconsistencies between cached entity-bodies and updated headers.
/// If a 304 response indicates an entity not currently cached, then the cache MUST disregard the response and repeat the request without the conditional.
/// If a cache uses a received 304 response to update a cache entry, the cache MUST update the entry to reflect any new field values given in the response.
/// </summary>
NotModified = 3 0 4 ;
UseProxy = 3 0 5 ;
TemporaryRedirect = 3 0 7 ;
// Client Error 4xx
/// <summary>
/// The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
/// </summary>
BadRequest = 4 0 0 ;
/// <summary>
/// 401 Unauthorized
/// The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication".
/// </summary>
Unauthorized = 4 0 1 ;
PaymentRequired = 4 0 2 ;
/// <summary>
/// 403 Forbidden
/// The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish to make this information available to the client, the status code 404 (Not Found) can be used instead.
/// </summary>
Forbidden = 4 0 3 ;
/// <summary>
/// 404 Not Found
/// The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.
/// </summary>
NotFound = 4 0 4 ;
/// <summary>
/// 405 Method Not Allowed
/// The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
/// </summary>
MethodNotAllowed = 4 0 5 ;
NotAcceptable = 4 0 6 ;
ProxyAuthenticationRequired = 4 0 7 ;
RequestTimeout = 4 0 8 ;
Conflict = 4 0 9 ;
Gone = 4 1 0 ;
LengthRequired = 4 1 1 ;
2019-07-19 17:22:33 +02:00
/// <summary>
/// 412 Precondition Failed
/// Any request can contain a conditional header defined in HTTP (If-
/// Match, If-Modified-Since, etc.) or the "If" or "Overwrite"
/// conditional headers defined in this specification. If the server
/// evaluates a conditional header, and if that condition fails to hold,
/// then this error code MUST be returned. On the other hand, if the
/// client did not include a conditional header in the request, then the
/// server MUST NOT use this status code.
/// </summary>
2015-12-16 16:41:06 +01:00
PreconditionFailed = 4 1 2 ;
RequestEntityTooLarge = 4 1 3 ;
RequestURITooLong = 4 1 4 ;
UnsupportedMediaType = 4 1 5 ;
RequestedRangeNotSatisfiable = 4 1 6 ;
ExpectationFailed = 4 1 7 ;
2019-07-19 17:22:33 +02:00
/// <summary>
2020-04-08 13:00:32 +02:00
/// The 422 (Unprocessable Entity) status code means the server
/// understands the content type of the request entity (hence a
/// 415(Unsupported Media Type) status code is inappropriate), and the
/// syntax of the request entity is correct (thus a 400 (Bad Request)
/// status code is inappropriate) but was unable to process the contained
/// instructions. For example, this error condition may occur if an XML
/// request body contains well-formed (i.e., syntactically correct), but
/// semantically erroneous, XML instructions.
2019-07-19 17:22:33 +02:00
/// </summary>
UnprocessableEntity = 4 2 2 ;
/// <summary>
2020-04-08 13:00:32 +02:00
/// The 423 (Locked) status code means the source or destination resource
/// of a method is locked. This response SHOULD contain an appropriate
/// precondition or postcondition code, such as 'lock-token-submitted' or 'no-conflicting-lock
2019-07-19 17:22:33 +02:00
/// </summary>
Locked = 4 2 3 ;
/// <summary>
/// The 424 (Failed Dependency) status code means that the method could
/// not be performed on the resource because the requested action
/// depended on another action and that action failed. For example, if a
/// command in a PROPPATCH method fails, then, at minimum, the rest of
/// the commands will also fail with 424 (Failed Dependency).
/// </summary>
FailedDependency = 4 2 4 ;
2015-12-16 16:41:06 +01:00
// Server Error 5xx
/// <summary>
/// 500 Internal Server Error
/// The server encountered an unexpected condition which prevented it from fulfilling the request.
/// </summary>
InternalServerError = 5 0 0 ;
/// <summary>
/// 501 Not Implemented
/// The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
/// </summary>
NotImplemented = 5 0 1 ;
BadGateway = 5 0 2 ;
/// <summary>
/// 503 Service Unavailable
/// The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.
/// Note: The existence of the 503 status code does not imply that a
/// server must use it when becoming overloaded. Some servers may wish
/// to simply refuse the connection.
/// </summary>
ServiceUnavailable = 5 0 3 ;
GatewayTimeout = 5 0 4 ;
HTTPVersionNotSupported = 5 0 5 ;
2019-07-19 17:22:33 +02:00
/// <summary>
/// The 507 (Insufficient Storage) status code means the method could not
/// be performed on the resource because the server is unable to store
/// the representation needed to successfully complete the request.
/// This condition is considered to be temporary. If the request that
/// received this status code was the result of a user action, the
/// request MUST NOT be repeated until it is requested by a separate user action.
/// </summary>
InsufficientStorage = 5 0 7 ;
2015-12-16 16:41:06 +01:00
end ;
2013-10-30 00:48:23 +01:00
2017-03-20 13:55:46 +01:00
EMVCException = class( Exception)
2019-12-17 14:52:11 +01:00
protected
2017-03-20 13:55:46 +01:00
FHttpErrorCode: UInt16 ;
FAppErrorCode: UInt16 ;
FDetailedMessage: string ;
2020-09-08 15:25:44 +02:00
FErrorItems: TArray< String > ;
2019-07-08 19:25:56 +02:00
procedure CheckHTTPErrorCode( const AHTTPErrorCode: UInt16 ) ;
2017-03-20 13:55:46 +01:00
public
constructor Create( const AMsg: string ) ; overload ; virtual ;
2019-03-16 17:20:28 +01:00
constructor Create( const AMsg: string ; const ADetailedMessage: string ;
2019-07-08 19:25:56 +02:00
const AAppErrorCode: UInt16 = 0 ;
2020-09-08 15:25:44 +02:00
const AHTTPErrorCode: UInt16 = HTTP_STATUS. InternalServerError;
const AErrorItems: TArray< String > = nil ) ; overload ; virtual ;
2019-07-08 19:25:56 +02:00
constructor Create( const AHTTPErrorCode: UInt16 ; const AMsg: string ) ; overload ; virtual ;
constructor Create( const AHTTPErrorCode: UInt16 ; const AAppErrorCode: Integer ; const AMsg: string ) ;
overload ; virtual ;
2020-04-08 13:00:32 +02:00
constructor CreateFmt( const AMsg: string ; const AArgs: array of const ) ; reintroduce ; overload ;
constructor CreateFmt( const AHTTPErrorCode: UInt16 ; const AMsg: string ; const AArgs: array of const ) ; overload ;
2017-03-20 13:55:46 +01:00
property HttpErrorCode: UInt16 read FHttpErrorCode;
property DetailedMessage: string read FDetailedMessage write FDetailedMessage;
property ApplicationErrorCode: UInt16 read FAppErrorCode write FAppErrorCode;
2020-09-08 15:25:44 +02:00
property ErrorItems: TArray< String > read FErrorItems;
2017-03-20 13:55:46 +01:00
end ;
2013-11-08 23:10:25 +01:00
2017-03-20 13:55:46 +01:00
EMVCSessionExpiredException = class( EMVCException)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end ;
2016-05-18 18:21:43 +02:00
2017-03-20 13:55:46 +01:00
EMVCConfigException = class( EMVCException)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end ;
2013-10-30 00:48:23 +01:00
2017-03-20 13:55:46 +01:00
EMVCFrameworkViewException = class( EMVCException)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end ;
EMVCJWTException = class( EMVCException)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end ;
2013-10-30 00:48:23 +01:00
2017-10-09 16:17:12 +02:00
EMVCViewError = class( EMVCException)
2019-07-26 18:59:07 +02:00
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
2017-10-09 16:17:12 +02:00
end ;
2020-04-20 17:56:17 +02:00
TMVCStringDictionaryList = class ;
2017-03-20 13:55:46 +01:00
TMVCRequestParamsTable = class( TDictionary< string , string > )
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end ;
2016-05-18 19:09:57 +02:00
2019-05-09 20:53:52 +02:00
IMVCLinkItem = interface
[ '{8BC70061-0DD0-4D0A-B135-F83A5C86629B}' ]
2020-04-08 13:00:32 +02:00
function Add( const PropName: string ; const PropValue: string ) : IMVCLinkItem;
2019-05-09 20:53:52 +02:00
end ;
IMVCLinks = interface
[ '{8A116BED-9A10-4885-AD4B-DF38A7F0D7DF}' ]
function AddRefLink: IMVCLinkItem;
function Clear: IMVCLinks;
function LinksData: TMVCStringDictionaryList;
end ;
TMVCLinks = class( TInterfacedObject, IMVCLinks)
private
fData: TMVCStringDictionaryList;
protected
function AddRefLink: IMVCLinkItem;
function Clear: IMVCLinks;
public
constructor Create; virtual ;
destructor Destroy; override ;
function LinksData: TMVCStringDictionaryList;
end ;
2020-05-11 23:39:43 +02:00
// IMVCStringDictionary = interface
// ['{164117AD-8DDD-47F7-877C-453979707D10}']
// function GetItems(const Key: string): string;
// procedure SetItems(const Key, Value: string);
// procedure Clear;
/// / function Add(const Name, Value: string): IMVCStringDictionary;
// function TryGetValue(const Name: string; out Value: string): Boolean; overload;
// function TryGetValue(const Name: string; out Value: Integer): Boolean; overload;
// function Count: Integer;
// function GetEnumerator: TDictionary<string, string>.TPairEnumerator;
// function ContainsKey(const Key: string): Boolean;
// function Keys: TArray<string>;
// property Items[const Key: string]: string read GetItems; default;
// end;
TMVCStringDictionary = class // (TInterfacedObject, IMVCStringDictionary)
2020-04-20 17:56:17 +02:00
strict private
function GetItems( const Key: string ) : string ;
procedure SetItems( const Key, Value: string ) ;
protected
fDict: TDictionary< string , string > ;
public
constructor Create; overload ; virtual ;
constructor Create( const aKey, aValue: string ) ; overload ; virtual ;
destructor Destroy; override ;
procedure Clear;
function Add( const Name , Value: string ) : TMVCStringDictionary;
2020-06-20 19:41:12 +02:00
function AddStrings( const Strings: TStrings) : TMVCStringDictionary;
2020-04-20 17:56:17 +02:00
function TryGetValue( const Name : string ; out Value: string ) : Boolean ; overload ;
function TryGetValue( const Name : string ; out Value: Integer ) : Boolean ; overload ;
function Count: Integer ;
function GetEnumerator: TDictionary< string , string > . TPairEnumerator;
function ContainsKey( const Key: string ) : Boolean ;
function Keys: TArray< string > ;
2020-06-20 19:41:12 +02:00
function ToString: String ; override ;
2020-04-20 17:56:17 +02:00
property Items[ const Key: string ] : string read GetItems write SetItems; default ;
end ;
2019-05-09 20:53:52 +02:00
TMVCDecoratorObject = class( TInterfacedObject, IMVCLinkItem)
private
fData: TMVCStringDictionary;
public
constructor Create( const aData: TMVCStringDictionary) ;
2020-04-08 13:00:32 +02:00
function Add( const PropName: string ; const PropValue: string ) : IMVCLinkItem;
2019-05-09 20:53:52 +02:00
end ;
2020-04-20 17:56:17 +02:00
TMVCStringDictionaryList = class( TObjectList< TMVCStringDictionary> )
public
constructor Create;
end ;
2017-06-02 00:10:31 +02:00
{ This type is thread safe }
TMVCStringObjectDictionary< T: class > = class
private
FMREWS: TMultiReadExclusiveWriteSynchronizer;
protected
2020-04-18 23:32:24 +02:00
fDict: TObjectDictionary< string , T> ;
2017-06-02 00:10:31 +02:00
public
constructor Create; virtual ;
destructor Destroy; override ;
function TryGetValue( const Name : string ; out Value: T) : Boolean ;
procedure Add( const Name : string ; Value: T) ;
2017-04-29 23:56:56 +02:00
end ;
2017-05-25 16:57:49 +02:00
TMVCViewDataObject = class( TObjectDictionary< string , TObject> )
2017-03-20 13:55:46 +01:00
private
{ private declarations }
protected
{ protected declarations }
public
constructor Create;
end ;
2016-05-18 18:21:43 +02:00
2020-04-20 17:56:17 +02:00
TMVCViewDataSet = class( TObjectDictionary< string , TDataset> )
2017-11-23 17:29:51 +01:00
private
{ private declarations }
protected
{ protected declarations }
public
constructor Create;
end ;
2017-10-09 16:17:12 +02:00
TMVCCriticalSectionHelper = class helper for TCriticalSection
2017-03-20 13:55:46 +01:00
public
2017-07-16 19:36:44 +02:00
procedure DoWithLock( const AAction: TProc) ;
function DoWithLockTimeout( const AAction: TProc; const ATimeOut: UInt32 ) : TWaitResult;
2017-03-20 13:55:46 +01:00
end ;
2014-04-01 00:02:31 +02:00
2018-09-25 15:36:53 +02:00
TMultiReadExclusiveWriteSynchronizerHelper = class helper for TMultiReadExclusiveWriteSynchronizer
2017-10-09 16:17:12 +02:00
public
procedure DoWithWriteLock( const AAction: TProc) ;
procedure DoWithReadLock( const AAction: TProc) ;
end ;
2017-03-20 13:55:46 +01:00
TMVCConfig = class sealed
private
2019-03-08 07:15:23 +01:00
FConfig: TMVCStringDictionary;
2020-05-11 23:39:43 +02:00
FFreezed: Boolean ;
2017-03-20 13:55:46 +01:00
function GetValue( const AIndex: string ) : string ;
function GetValueAsInt64( const AIndex: string ) : Int64 ;
2019-03-16 17:20:28 +01:00
procedure SetValue( const AIndex: string ; const aValue: string ) ;
2020-05-11 23:39:43 +02:00
procedure CheckNotFreezed; inline ;
2017-03-20 13:55:46 +01:00
protected
{ protected declarations }
public
constructor Create;
destructor Destroy; override ;
2020-05-11 23:39:43 +02:00
procedure Freeze;
2017-03-20 13:55:46 +01:00
function Keys: TArray< string > ;
function ToString: string ; override ;
procedure SaveToFile( const AFileName: string ) ;
procedure LoadFromFile( const AFileName: string ) ;
2015-04-01 17:01:23 +02:00
2017-03-20 13:55:46 +01:00
property Value[ const AIndex: string ] : string read GetValue write SetValue; default ;
property AsInt64[ const AIndex: string ] : Int64 read GetValueAsInt64;
end ;
2016-06-22 17:49:16 +02:00
2019-03-08 14:26:20 +01:00
TMVCStreamHelper = class helper for TStream
public
procedure WriteUTF8( const AString: string ) ;
end ;
2018-11-02 21:43:09 +01:00
TMVCFieldMap = record
InstanceFieldName: string ;
DatabaseFieldName: string ;
end ;
2017-03-13 20:52:11 +01:00
2020-02-26 13:10:41 +01:00
TMVCCustomRouter = class abstract
public
2020-04-08 13:00:32 +02:00
function GetQualifiedActionName( ) : string ; virtual ; abstract ;
2020-02-26 13:10:41 +01:00
end ;
2020-04-09 16:04:45 +02:00
TMVCGuidHelper = record
public
class function GuidFromString( const AGuidStr: string ) : TGUID; static ;
end ;
2018-11-02 21:43:09 +01:00
TMVCFieldsMapping = TArray< TMVCFieldMap> ;
{$SCOPEDENUMS ON}
2018-10-23 16:18:34 +02:00
TMVCCompressionType = ( ctNone, ctDeflate, ctGZIP) ;
2021-02-15 18:17:39 +01:00
{ GENERIC TYPE ALIASES }
TMVCListOfString = TList< string > ;
TMVCListOfInteger = TList< Integer > ;
TMVCListOfBoolean = TList< Boolean > ;
TMVCListOfDouble = TList< Double > ;
{ GENERIC TYPE ALIASES // END}
2017-03-20 13:55:46 +01:00
function AppPath: string ;
2017-09-21 23:11:19 +02:00
function IsReservedOrPrivateIP( const AIP: string ) : Boolean ; inline ;
function IP2Long( const AIP: string ) : UInt32 ; inline ;
2017-03-13 20:52:11 +01:00
2019-03-16 17:20:28 +01:00
function B64Encode( const aValue: string ) : string ; overload ;
function B64Encode( const aValue: TBytes) : string ; overload ;
function B64Decode( const aValue: string ) : string ;
2017-03-13 20:52:11 +01:00
2020-04-08 13:00:32 +02:00
function URLSafeB64encode( const Value: string ; IncludePadding: Boolean ; AByteEncoding: IIdTextEncoding = nil )
: string ; overload ;
2017-09-07 00:10:21 +02:00
function URLSafeB64encode( const Value: TBytes; IncludePadding: Boolean ) : string ; overload ;
2019-07-19 19:12:31 +02:00
function URLSafeB64Decode( const Value: string ; AByteEncoding: IIdTextEncoding = nil ) : string ;
2017-07-05 00:17:46 +02:00
2021-02-25 10:50:57 +01:00
function URLEncode( const Value: string ) : string ; overload ;
function URLDecode( const Value: string ) : string ;
2017-03-20 13:55:46 +01:00
function ByteToHex( AInByte: Byte ) : string ;
function BytesToHex( ABytes: TBytes) : string ;
2020-04-08 13:00:32 +02:00
procedure Base64StringToFile( const aBase64String, AFileName: string ; const aOverwrite: Boolean = False ) ;
function FileToBase64String( const FileName: string ) : string ;
2017-03-20 13:55:46 +01:00
2019-01-08 12:48:27 +01:00
procedure SplitContentMediaTypeAndCharset( const aContentType: string ; var aContentMediaType: string ;
var aContentCharSet: string ) ;
2017-10-09 16:17:12 +02:00
function BuildContentType( const aContentMediaType: string ; const aContentCharSet: string ) : string ;
2017-09-08 16:59:02 +02:00
2020-06-24 00:00:47 +02:00
function StrToJSONObject( const aString: String ) : TJsonObject;
function StrToJSONArray( const aString: String ) : TJsonArray;
2020-09-22 15:18:22 +02:00
function WrapAsList( const AObject: TObject; AOwnsObject: Boolean = False ) : IMVCList;
2020-04-20 17:56:17 +02:00
{ changing case }
function CamelCase( const Value: string ; const MakeFirstUpperToo: Boolean = False ) : string ;
2020-09-29 19:57:43 +02:00
function SnakeCase( const Value: string ) : string ;
2019-03-16 17:20:28 +01:00
2017-09-08 16:59:02 +02:00
const
MVC_HTTP_METHODS_WITHOUT_CONTENT: TMVCHTTPMethods = [ httpGET, httpDELETE, httpHEAD, httpOPTIONS] ;
MVC_HTTP_METHODS_WITH_CONTENT: TMVCHTTPMethods = [ httpPOST, httpPUT, httpPATCH] ;
2018-10-23 16:18:34 +02:00
const
2020-04-30 22:53:53 +02:00
MVC_COMPRESSION_TYPE_AS_STRING: array [ TMVCCompressionType] of string = ( 'none' , 'deflate' , 'gzip' ) ;
2018-10-31 01:07:23 +01:00
MVC_COMPRESSION_ZLIB_WINDOW_BITS: array [ TMVCCompressionType] of Integer = ( 0 , - 1 5 , 3 1 ) ;
// WindowBits: http://zlib.net/manual.html#Advanced
2018-10-23 16:18:34 +02:00
2017-03-20 13:55:46 +01:00
var
2019-05-08 20:20:14 +02:00
gLock: TObject;
2017-03-20 13:55:46 +01:00
2013-10-30 00:48:23 +01:00
const
2019-01-08 12:48:27 +01:00
RESERVED_IPS: array [ 1 .. 1 1 ] of array [ 1 .. 2 ] of string = ( ( '0.0.0.0' , '0.255.255.255' ) ,
2019-03-16 17:20:28 +01:00
( '10.0.0.0' , '10.255.255.255' ) , ( '127.0.0.0' , '127.255.255.255' ) ,
( '169.254.0.0' , '169.254.255.255' ) ,
2019-01-08 12:48:27 +01:00
( '172.16.0.0' , '172.31.255.255' ) , ( '192.0.2.0' , '192.0.2.255' ) , ( '192.88.99.0' , '192.88.99.255' ) ,
2019-03-16 17:20:28 +01:00
( '192.168.0.0' , '192.168.255.255' ) , ( '198.18.0.0' , '198.19.255.255' ) ,
( '224.0.0.0' , '239.255.255.255' ) ,
2019-01-08 12:48:27 +01:00
( '240.0.0.0' , '255.255.255.255' ) ) ;
2013-10-30 00:48:23 +01:00
2019-07-24 20:48:22 +02:00
type
TMVCParseAuthentication = class
public
2020-04-08 13:00:32 +02:00
class procedure OnParseAuthentication( AContext: TIdContext; const AAuthType, AAuthData: string ; var VUsername,
VPassword: string ; var VHandled: Boolean ) ;
2019-07-24 20:48:22 +02:00
end ;
2020-06-24 00:00:47 +02:00
2017-09-21 23:11:19 +02:00
implementation
uses
2019-03-16 17:20:28 +01:00
IdCoder3to4,
2019-05-16 00:16:55 +02:00
System. NetEncoding,
2020-06-18 14:49:06 +02:00
System. Character,
2020-10-16 02:37:55 +02:00
MVCFramework. Serializer. JsonDataObjects, MVCFramework. Serializer. Commons,
System. RegularExpressions;
2017-09-21 23:11:19 +02:00
2016-09-25 16:17:37 +02:00
var
2017-03-20 13:55:46 +01:00
GlobalAppName, GlobalAppPath, GlobalAppExe: string ;
2016-09-25 16:17:37 +02:00
2021-02-25 10:50:57 +01:00
function URLEncode( const Value: string ) : string ; overload ;
begin
Result : = TNetEncoding. URL. Encode( Value) ;
end ;
function URLDecode( const Value: string ) : string ;
begin
Result : = TNetEncoding. URL. Decode( Value) ;
end ;
2017-03-20 13:55:46 +01:00
function AppPath: string ;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
Result : = GlobalAppPath;
2013-10-30 00:48:23 +01:00
end ;
2017-09-21 23:11:19 +02:00
function IP2Long( const AIP: string ) : Cardinal ;
var
lPieces: TArray< string > ;
2013-10-30 00:48:23 +01:00
begin
2017-09-21 23:11:19 +02:00
if AIP. IsEmpty then
Exit( 0 ) ;
lPieces : = AIP. Split( [ '.' ] ) ;
2019-03-16 17:20:28 +01:00
Result : = ( StrToInt( lPieces[ 0 ] ) * 1 6 7 7 7 2 1 6 ) + ( StrToInt( lPieces[ 1 ] ) * 6 5 5 3 6 ) +
( StrToInt( lPieces[ 2 ] ) * 2 5 6 ) +
2019-01-08 12:48:27 +01:00
StrToInt( lPieces[ 3 ] ) ;
2013-10-30 00:48:23 +01:00
end ;
2019-08-06 02:54:45 +02:00
function IsReservedOrPrivateIP( const AIP: string ) : Boolean ;
var
I: Integer ;
IntIP: Cardinal ;
begin
Result : = False ;
IntIP : = IP2Long( AIP) ;
for I : = low( RESERVED_IPS) to high( RESERVED_IPS) do
if ( IntIP > = IP2Long( RESERVED_IPS[ I] [ 1 ] ) ) and ( IntIP < = IP2Long( RESERVED_IPS[ I] [ 2 ] ) ) then
Exit( True ) ;
end ;
2017-09-21 23:11:19 +02:00
// function IP2Long(const AIP: string): UInt32;
// begin
// Result := IdGlobal.IPv4ToUInt32(AIP);
// end;
2019-03-16 17:20:28 +01:00
function B64Encode( const aValue: string ) : string ; overload ;
2013-10-30 00:48:23 +01:00
begin
2017-09-07 00:10:21 +02:00
// Do not use TNetEncoding
2019-03-16 17:20:28 +01:00
Result : = TIdEncoderMIME. EncodeString( aValue) ;
2013-10-30 00:48:23 +01:00
end ;
2019-03-16 17:20:28 +01:00
function B64Encode( const aValue: TBytes) : string ; overload ;
2013-10-30 00:48:23 +01:00
begin
2017-09-07 00:10:21 +02:00
// Do not use TNetEncoding
2019-03-16 17:20:28 +01:00
Result : = TIdEncoderMIME. EncodeBytes( TIdBytes( aValue) ) ;
2013-10-30 00:48:23 +01:00
end ;
2019-03-16 17:20:28 +01:00
function B64Decode( const aValue: string ) : string ;
2013-10-30 00:48:23 +01:00
begin
2017-09-07 00:10:21 +02:00
// Do not use TNetEncoding
2019-03-16 17:20:28 +01:00
Result : = TIdDecoderMIME. DecodeString( aValue) ;
2013-10-30 00:48:23 +01:00
end ;
2017-03-20 13:55:46 +01:00
function ByteToHex( AInByte: Byte ) : string ;
const
DIGITS: array [ 0 .. 1 5 ] of Char = '0123456789abcdef' ;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
Result : = DIGITS[ AInByte shr 4 ] + DIGITS[ AInByte and $0F ] ;
2013-10-30 00:48:23 +01:00
end ;
2017-03-20 13:55:46 +01:00
function BytesToHex( ABytes: TBytes) : string ;
var
B: Byte ;
2015-04-01 17:01:23 +02:00
begin
2017-03-20 13:55:46 +01:00
Result : = EmptyStr;
for B in ABytes do
Result : = Result + ByteToHex( B) ;
2017-09-08 16:59:02 +02:00
end ;
2017-10-09 16:17:12 +02:00
function BuildContentType( const aContentMediaType: string ; const aContentCharSet: string ) : string ;
2019-03-05 20:55:37 +01:00
var
2020-04-08 13:00:32 +02:00
lContentMediaType: string ;
2017-09-08 16:59:02 +02:00
begin
2019-03-05 20:55:37 +01:00
lContentMediaType : = aContentMediaType. ToLower. Trim. Replace( ' ' , '' , [ rfReplaceAll] ) ;
if lContentMediaType = '' then
2017-09-08 16:59:02 +02:00
begin
2019-03-05 20:55:37 +01:00
Result : = '' ;
2017-09-08 16:59:02 +02:00
end
else
begin
2019-03-05 20:55:37 +01:00
if aContentCharSet. IsEmpty then
begin
Result : = lContentMediaType;
end
else
2019-05-08 09:10:42 +02:00
if lContentMediaType. StartsWith( 'text/' ) or lContentMediaType. StartsWith( 'application/' )
2019-05-16 00:16:55 +02:00
then
begin
Result : = lContentMediaType + ';charset=' + aContentCharSet. ToLower;
end
else
begin
Result : = lContentMediaType;
end ;
2017-09-08 16:59:02 +02:00
end ;
end ;
2019-01-08 12:48:27 +01:00
procedure SplitContentMediaTypeAndCharset( const aContentType: string ; var aContentMediaType: string ;
var aContentCharSet: string ) ;
2017-09-08 16:59:02 +02:00
var
lContentTypeValues: TArray< string > ;
begin
if not aContentType. IsEmpty then
begin
lContentTypeValues : = aContentType. Split( [ ';' ] ) ;
aContentMediaType : = Trim( lContentTypeValues[ 0 ] ) ;
2019-03-16 17:20:28 +01:00
if ( Length( lContentTypeValues) > 1 ) and ( lContentTypeValues[ 1 ] . Trim. StartsWith( 'charset' , True ) )
then
2017-09-08 16:59:02 +02:00
begin
aContentCharSet : = lContentTypeValues[ 1 ] . Trim. Split( [ '=' ] ) [ 1 ] . Trim;
end
else
begin
aContentCharSet : = '' ;
end ;
end
else
begin
aContentMediaType : = '' ;
aContentCharSet : = '' ;
end ;
2015-04-01 17:01:23 +02:00
end ;
2017-03-20 13:55:46 +01:00
{ EMVCException }
2015-04-01 17:01:23 +02:00
2017-03-20 13:55:46 +01:00
constructor EMVCException. Create( const AMsg: string ) ;
2013-11-08 23:10:25 +01:00
begin
2017-03-20 13:55:46 +01:00
inherited Create( AMsg) ;
FHttpErrorCode : = HTTP_STATUS. InternalServerError;
FDetailedMessage : = EmptyStr;
FAppErrorCode : = 0 ;
2020-09-08 15:25:44 +02:00
SetLength( FErrorItems, 0 ) ;
2013-11-08 23:10:25 +01:00
end ;
2019-03-16 17:20:28 +01:00
constructor EMVCException. Create( const AMsg, ADetailedMessage: string ;
2020-09-08 15:25:44 +02:00
const AAppErrorCode, AHTTPErrorCode: UInt16 ; const AErrorItems: TArray< String > ) ;
2013-11-08 23:10:25 +01:00
begin
2017-03-20 13:55:46 +01:00
Create( AMsg) ;
2019-07-08 19:25:56 +02:00
CheckHTTPErrorCode( AHTTPErrorCode) ;
FHttpErrorCode : = AHTTPErrorCode;
2017-03-20 13:55:46 +01:00
FAppErrorCode : = AAppErrorCode;
FDetailedMessage : = ADetailedMessage;
2020-09-08 15:25:44 +02:00
if AErrorItems < > nil then
begin
FErrorItems : = AErrorItems;
end ;
2013-11-08 23:10:25 +01:00
end ;
2019-07-08 19:25:56 +02:00
constructor EMVCException. Create( const AHTTPErrorCode: UInt16 ; const AMsg: string ) ;
begin
CheckHTTPErrorCode( AHTTPErrorCode) ;
Create( AMsg) ;
FHttpErrorCode : = AHTTPErrorCode;
end ;
procedure EMVCException. CheckHTTPErrorCode( const AHTTPErrorCode: UInt16 ) ;
2013-10-30 00:48:23 +01:00
begin
2019-07-08 19:25:56 +02:00
if ( AHTTPErrorCode div 1 0 0 = 0 ) or ( AHTTPErrorCode div 1 0 0 > 5 ) then
2018-09-25 15:36:53 +02:00
begin
2019-07-08 19:25:56 +02:00
raise EMVCException. CreateFmt( 'Invalid HTTP_STATUS [%d]' , [ AHTTPErrorCode] ) ;
2018-09-25 15:36:53 +02:00
end ;
2019-07-08 19:25:56 +02:00
end ;
constructor EMVCException. Create( const AHTTPErrorCode: UInt16 ;
const AAppErrorCode: Integer ; const AMsg: string ) ;
begin
CheckHTTPErrorCode( AHTTPErrorCode) ;
2017-03-20 13:55:46 +01:00
Create( AMsg) ;
2019-07-08 19:25:56 +02:00
FHttpErrorCode : = AHTTPErrorCode;
FAppErrorCode : = AAppErrorCode;
2013-10-30 00:48:23 +01:00
end ;
2020-04-08 13:00:32 +02:00
constructor EMVCException. CreateFmt( const AHTTPErrorCode: UInt16 ;
const AMsg: string ; const AArgs: array of const ) ;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
inherited CreateFmt( AMsg, AArgs) ;
2020-04-08 13:00:32 +02:00
FHttpErrorCode : = AHTTPErrorCode;
2017-03-20 13:55:46 +01:00
FDetailedMessage : = EmptyStr;
FAppErrorCode : = 0 ;
2013-10-30 00:48:23 +01:00
end ;
2020-04-08 13:00:32 +02:00
constructor EMVCException. CreateFmt( const AMsg: string ; const AArgs: array of const ) ;
begin
CreateFmt( HTTP_STATUS. InternalServerError, AMsg, AArgs) ;
end ;
2017-03-20 19:08:01 +01:00
{ TMVCViewDataObject }
2013-10-30 00:48:23 +01:00
2017-03-20 19:08:01 +01:00
constructor TMVCViewDataObject. Create;
2013-10-30 00:48:23 +01:00
begin
2017-11-23 17:29:51 +01:00
inherited Create( [ ] ) ;
2013-10-30 00:48:23 +01:00
end ;
2017-03-20 13:55:46 +01:00
{ TMVCCriticalSectionHelper }
2015-12-16 16:41:06 +01:00
2017-07-16 19:36:44 +02:00
procedure TMVCCriticalSectionHelper. DoWithLock( const AAction: TProc) ;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
Self. Enter;
try
AAction( ) ;
finally
Self. Leave;
end ;
2013-10-30 00:48:23 +01:00
end ;
2019-03-16 17:20:28 +01:00
function TMVCCriticalSectionHelper. DoWithLockTimeout( const AAction: TProc; const ATimeOut: UInt32 )
: TWaitResult;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
Result : = Self. WaitFor( ATimeOut) ;
if ( Result = TWaitResult. wrSignaled) then
try
AAction( ) ;
finally
Self. Leave;
end ;
2013-10-30 00:48:23 +01:00
end ;
2017-03-20 13:55:46 +01:00
{ TMVCConfig }
2020-05-11 23:39:43 +02:00
procedure TMVCConfig. CheckNotFreezed;
begin
if FFreezed then
begin
raise EMVCException. Create( 'Configuration in freezed - no more changes allowed' ) at ReturnAddress;
end ;
end ;
2017-03-20 13:55:46 +01:00
constructor TMVCConfig. Create;
2013-10-30 00:48:23 +01:00
begin
2017-03-20 13:55:46 +01:00
inherited Create;
2019-03-08 07:15:23 +01:00
FConfig : = TMVCStringDictionary. Create;
2020-05-11 23:39:43 +02:00
FFreezed : = False ;
2013-10-30 00:48:23 +01:00
end ;
2017-03-20 13:55:46 +01:00
destructor TMVCConfig. Destroy;
2016-05-18 18:21:43 +02:00
begin
2017-03-20 13:55:46 +01:00
FConfig. Free;
inherited Destroy;
2016-05-18 18:21:43 +02:00
end ;
2020-05-11 23:39:43 +02:00
procedure TMVCConfig. Freeze;
begin
FFreezed : = True ;
end ;
2017-03-20 13:55:46 +01:00
function TMVCConfig. GetValue( const AIndex: string ) : string ;
2016-05-18 18:21:43 +02:00
begin
2017-03-20 13:55:46 +01:00
if FConfig. ContainsKey( AIndex) then
Result : = FConfig. Items[ AIndex]
else
raise EMVCConfigException. CreateFmt( 'Invalid config key [%s]' , [ AIndex] ) ;
2016-05-18 18:21:43 +02:00
end ;
2017-03-20 13:55:46 +01:00
function TMVCConfig. GetValueAsInt64( const AIndex: string ) : Int64 ;
2016-05-18 18:21:43 +02:00
begin
2017-03-20 13:55:46 +01:00
Result : = StrToInt64( Value[ AIndex] ) ;
2016-05-18 18:21:43 +02:00
end ;
2017-03-20 13:55:46 +01:00
function TMVCConfig. Keys: TArray< string > ;
2016-05-18 18:21:43 +02:00
begin
2019-03-08 07:15:23 +01:00
Result : = FConfig. Keys;
2016-05-18 18:21:43 +02:00
end ;
2017-03-20 13:55:46 +01:00
procedure TMVCConfig. LoadFromFile( const AFileName: string ) ;
2016-05-18 18:21:43 +02:00
var
2019-03-08 07:15:23 +01:00
lConfigString: string ;
lStreamReader: TStreamReader;
lSer: TMVCJsonDataObjectsSerializer;
2016-05-18 18:21:43 +02:00
begin
2019-03-16 17:20:28 +01:00
lStreamReader : = TStreamReader. Create( TFileStream. Create( AFileName,
fmOpenRead or fmShareDenyWrite) , TEncoding. ASCII) ;
2017-12-12 20:04:01 +01:00
try
2019-03-08 07:15:23 +01:00
lStreamReader. OwnStream;
lConfigString : = lStreamReader. ReadToEnd;
finally
lStreamReader. Free;
end ;
lSer : = TMVCJsonDataObjectsSerializer. Create;
try
FConfig. Clear;
lSer. DeserializeObject( lConfigString, FConfig) ;
2017-12-12 20:04:01 +01:00
finally
2019-03-08 07:15:23 +01:00
lSer. Free;
2017-12-12 20:04:01 +01:00
end ;
2017-02-07 16:06:07 +01:00
end ;
2017-03-20 13:55:46 +01:00
procedure TMVCConfig. SaveToFile( const AFileName: string ) ;
2017-02-07 16:06:07 +01:00
begin
2017-03-20 13:55:46 +01:00
TFile. WriteAllText( AFileName, ToString, TEncoding. ASCII) ;
2017-02-07 16:06:07 +01:00
end ;
2019-03-16 17:20:28 +01:00
procedure TMVCConfig. SetValue( const AIndex, aValue: string ) ;
2017-02-09 19:33:59 +01:00
begin
2020-05-11 23:39:43 +02:00
CheckNotFreezed;
2019-03-16 17:20:28 +01:00
FConfig. Add( AIndex, aValue) ;
2017-02-09 19:33:59 +01:00
end ;
2017-03-20 13:55:46 +01:00
function TMVCConfig. ToString: string ;
var
2019-03-08 07:15:23 +01:00
lSer: TMVCJsonDataObjectsSerializer;
2017-02-10 14:19:55 +01:00
begin
2019-03-08 07:15:23 +01:00
lSer : = TMVCJsonDataObjectsSerializer. Create;
2017-03-20 13:55:46 +01:00
try
2019-03-08 07:15:23 +01:00
Result : = lSer. SerializeObject( FConfig) ;
2017-03-20 13:55:46 +01:00
finally
2019-03-08 07:15:23 +01:00
lSer. Free;
2017-03-20 13:55:46 +01:00
end ;
2017-02-10 14:19:55 +01:00
end ;
2017-04-29 23:56:56 +02:00
{ TMVCStringDictionary }
2020-06-20 19:41:12 +02:00
function TMVCStringDictionary. AddStrings( const Strings: TStrings) : TMVCStringDictionary;
var
I: Integer ;
lName: string ;
begin
for I : = 0 to Strings. Count- 1 do
begin
lName : = Strings. Names[ I] ;
Add( lName, Strings. Values[ lName] ) ;
end ;
Result : = Self;
end ;
2019-03-08 09:33:41 +01:00
function TMVCStringDictionary. Add( const Name , Value: string ) : TMVCStringDictionary;
2017-04-29 23:56:56 +02:00
begin
2020-04-18 23:32:24 +02:00
fDict. AddOrSetValue( name , Value) ;
2017-04-29 23:56:56 +02:00
Result : = Self;
end ;
procedure TMVCStringDictionary. Clear;
begin
2020-04-18 23:32:24 +02:00
fDict. Clear;
2017-04-29 23:56:56 +02:00
end ;
2018-10-31 01:07:23 +01:00
function TMVCStringDictionary. ContainsKey( const Key: string ) : Boolean ;
begin
2020-04-18 23:32:24 +02:00
Result : = fDict. ContainsKey( Key) ;
2018-10-31 01:07:23 +01:00
end ;
2017-04-29 23:56:56 +02:00
function TMVCStringDictionary. Count: Integer ;
begin
2020-04-18 23:32:24 +02:00
Result : = fDict. Count;
2017-04-29 23:56:56 +02:00
end ;
2020-04-08 13:00:32 +02:00
constructor TMVCStringDictionary. Create( const aKey, aValue: string ) ;
2019-03-16 17:20:28 +01:00
begin
Create;
Add( aKey, aValue) ;
end ;
2017-04-29 23:56:56 +02:00
constructor TMVCStringDictionary. Create;
begin
inherited ;
2020-04-18 23:32:24 +02:00
fDict : = TDictionary< string , string > . Create;
2017-04-29 23:56:56 +02:00
end ;
destructor TMVCStringDictionary. Destroy;
begin
2020-04-18 23:32:24 +02:00
fDict. Free;
2017-04-29 23:56:56 +02:00
inherited ;
end ;
2017-06-02 00:10:31 +02:00
function TMVCStringDictionary. GetEnumerator: TDictionary< string , string > . TPairEnumerator;
2017-04-29 23:56:56 +02:00
begin
2020-04-18 23:32:24 +02:00
Result : = fDict. GetEnumerator;
2017-04-29 23:56:56 +02:00
end ;
2018-10-31 01:07:23 +01:00
function TMVCStringDictionary. GetItems( const Key: string ) : string ;
begin
Result : = '' ;
2020-04-18 23:32:24 +02:00
fDict. TryGetValue( Key, Result ) ;
2018-10-31 01:07:23 +01:00
end ;
2020-06-20 19:41:12 +02:00
function TMVCStringDictionary. ToString: String ;
var
I: Integer ;
lValues: TArray< String > ;
lKey: string ;
begin
SetLength( lValues, Length( Keys) ) ;
for I : = 0 to Count - 1 do
begin
lKey : = Keys[ I] ;
lValues[ I] : = lKey + '=' + Items[ lKey] ;
end ;
Result : = String . Join( ';' , lValues) ;
end ;
2020-04-08 13:00:32 +02:00
function TMVCStringDictionary. Keys: TArray< string > ;
2019-03-08 07:15:23 +01:00
begin
2020-04-18 23:32:24 +02:00
Result : = fDict. Keys. ToArray;
2019-03-08 07:15:23 +01:00
end ;
2018-10-31 01:07:23 +01:00
procedure TMVCStringDictionary. SetItems( const Key, Value: string ) ;
begin
2020-04-18 23:32:24 +02:00
fDict. AddOrSetValue( Key, Value) ;
2018-10-31 01:07:23 +01:00
end ;
2019-01-08 12:48:27 +01:00
function TMVCStringDictionary. TryGetValue( const Name : string ; out Value: Integer ) : Boolean ;
var
2020-04-08 13:00:32 +02:00
lTmp: string ;
2019-01-08 12:48:27 +01:00
begin
2020-04-08 13:00:32 +02:00
Result : = TryGetValue( name , lTmp) and TryStrToInt( lTmp, Value) ;
2019-01-08 12:48:27 +01:00
end ;
2018-09-25 15:36:53 +02:00
function TMVCStringDictionary. TryGetValue( const Name : string ; out Value: string ) : Boolean ;
2017-06-02 00:10:31 +02:00
begin
2020-04-18 23:32:24 +02:00
Result : = fDict. TryGetValue( name , Value) ;
2017-06-02 00:10:31 +02:00
end ;
{ TMVCStringObjectDictionary }
procedure TMVCStringObjectDictionary< T> . Add( const Name : string ; Value: T) ;
begin
FMREWS. BeginWrite;
try
2020-04-18 23:32:24 +02:00
if not fDict. ContainsKey( name ) then
fDict. Add( name , Value) ;
2017-06-02 00:10:31 +02:00
finally
FMREWS. EndWrite;
end ;
end ;
constructor TMVCStringObjectDictionary< T> . Create;
begin
inherited ;
2020-04-18 23:32:24 +02:00
fDict : = TObjectDictionary< string , T> . Create( [ doOwnsValues] ) ;
2017-06-02 00:10:31 +02:00
FMREWS : = TMultiReadExclusiveWriteSynchronizer. Create;
end ;
destructor TMVCStringObjectDictionary< T> . Destroy;
begin
2020-04-18 23:32:24 +02:00
fDict. Free;
2017-06-02 00:10:31 +02:00
FMREWS. Free;
inherited ;
end ;
function TMVCStringObjectDictionary< T> . TryGetValue( const Name : string ; out Value: T) : Boolean ;
begin
FMREWS. BeginRead;
try
2020-04-18 23:32:24 +02:00
Result : = fDict. TryGetValue( name , Value) ;
2017-06-02 00:10:31 +02:00
finally
FMREWS. EndRead;
end ;
end ;
2017-07-05 00:17:46 +02:00
type
TURLSafeEncode = class( TIdEncoder3to4)
protected
procedure InitComponent; override ;
public
end ;
2017-09-07 00:10:21 +02:00
2017-07-05 00:17:46 +02:00
TURLSafeDecode = class( TIdDecoder4to3)
protected
class var GSafeBaseBase64DecodeTable: TIdDecodeTable;
procedure InitComponent; override ;
public
end ;
const
2019-03-16 17:20:28 +01:00
GURLSafeBase64CodeTable
: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_' ;
2019-01-08 12:48:27 +01:00
{ Do not Localize }
2017-07-05 00:17:46 +02:00
procedure TURLSafeEncode. InitComponent;
begin
inherited ;
FCodingTable : = ToBytes( GURLSafeBase64CodeTable) ;
2017-09-07 00:10:21 +02:00
FFillChar : = '=' ; { Do not Localize }
2017-07-05 00:17:46 +02:00
end ;
procedure TURLSafeDecode. InitComponent;
begin
inherited ;
FDecodeTable : = GSafeBaseBase64DecodeTable;
FCodingTable : = ToBytes( GURLSafeBase64CodeTable) ;
2017-09-07 00:10:21 +02:00
FFillChar : = '=' ; { Do not Localize }
2017-07-05 00:17:46 +02:00
end ;
2020-04-08 13:00:32 +02:00
function URLSafeB64encode( const Value: string ; IncludePadding: Boolean ; AByteEncoding: IIdTextEncoding = nil )
: string ; overload ;
2017-07-05 00:17:46 +02:00
begin
if IncludePadding then
2019-07-02 20:43:37 +02:00
Result : = TURLSafeEncode. EncodeString( Value, AByteEncoding)
2017-07-05 00:17:46 +02:00
else
2019-07-02 20:43:37 +02:00
Result : = TURLSafeEncode. EncodeString( Value, AByteEncoding) . Replace( '=' , '' , [ rfReplaceAll] ) ;
2017-07-05 00:17:46 +02:00
end ;
/// <summary>
2017-09-07 00:10:21 +02:00
/// Remove "trimmed" character from the end of the string passed as parameter
2017-07-05 00:17:46 +02:00
/// </summary>
/// <param name="Value">Original string</param>
/// <param name="TrimmedChar">Character to remove</param>
/// <returns>Resulting string</returns>
2018-09-25 15:36:53 +02:00
function RTrim( const Value: string ; TrimmedChar: Char ) : string ;
2017-07-05 00:17:46 +02:00
var
Strlen: Integer ;
begin
Strlen : = Length( Value) ;
2017-09-07 00:10:21 +02:00
while ( Strlen > 0 ) and ( Value[ Strlen] = TrimmedChar) do
2018-09-25 15:36:53 +02:00
dec( Strlen) ;
Result : = copy( Value, 1 , Strlen)
2017-07-05 00:17:46 +02:00
end ;
2017-09-07 00:10:21 +02:00
function URLSafeB64encode( const Value: TBytes; IncludePadding: Boolean ) : string ; overload ;
2017-07-05 00:17:46 +02:00
begin
if IncludePadding then
Result : = TURLSafeEncode. EncodeBytes( TIdBytes( Value) )
else
Result : = RTrim( TURLSafeEncode. EncodeBytes( TIdBytes( Value) ) , '=' ) ;
end ;
2019-07-19 19:12:31 +02:00
function URLSafeB64Decode( const Value: string ; AByteEncoding: IIdTextEncoding = nil ) : string ;
2017-07-05 00:17:46 +02:00
begin
// SGR 2017-07-03 : b64url might not include padding. Need to add it before decoding
2018-09-25 15:36:53 +02:00
case Length( Value) mod 4 of
2017-07-05 00:17:46 +02:00
0 :
2017-09-07 00:10:21 +02:00
begin
2019-07-19 19:12:31 +02:00
Result : = TURLSafeDecode. DecodeString( Value, AByteEncoding) ;
2017-09-07 00:10:21 +02:00
end ;
2017-07-05 00:17:46 +02:00
2 :
2019-07-19 19:12:31 +02:00
Result : = TURLSafeDecode. DecodeString( Value + '==' , AByteEncoding) ;
2017-07-05 00:17:46 +02:00
3 :
2019-07-19 19:12:31 +02:00
Result : = TURLSafeDecode. DecodeString( Value + '=' , AByteEncoding) ;
2017-07-05 00:17:46 +02:00
else
raise EExternalException. Create( 'Illegal base64url length' ) ;
end ;
end ;
2017-10-09 16:17:12 +02:00
{ TMultiReadExclusiveWriteSynchronizerHelper }
2018-09-25 15:36:53 +02:00
procedure TMultiReadExclusiveWriteSynchronizerHelper. DoWithReadLock( const AAction: TProc) ;
2017-10-09 16:17:12 +02:00
begin
Self. BeginRead;
try
AAction( ) ;
finally
Self. EndRead;
end ;
end ;
2018-09-25 15:36:53 +02:00
procedure TMultiReadExclusiveWriteSynchronizerHelper. DoWithWriteLock( const AAction: TProc) ;
2017-10-09 16:17:12 +02:00
begin
Self. BeginWrite;
try
AAction( ) ;
finally
Self. EndWrite;
end ;
end ;
2017-11-23 17:29:51 +01:00
{ TMVCViewDataSet }
constructor TMVCViewDataSet. Create;
begin
inherited Create( [ ] ) ;
end ;
2019-03-08 14:26:20 +01:00
{ TMVCStreamHelper }
procedure TMVCStreamHelper. WriteUTF8( const AString: string ) ;
var
UFTStr: UTF8String ;
begin
UFTStr : = UTF8String( AString) ;
2020-05-28 22:42:16 +02:00
if UFTStr < > '' then
begin
Self. WriteBuffer( UFTStr[ low( UFTStr) ] , Length( UFTStr) ) ;
end ;
2019-03-08 14:26:20 +01:00
end ;
2019-05-09 20:53:52 +02:00
{ TMVCDecorator }
function TMVCLinks. AddRefLink: IMVCLinkItem;
begin
if not Assigned( fData) then
begin
fData : = TMVCStringDictionaryList. Create;
end ;
Result : = TMVCDecoratorObject. Create( fData[ fData. Add( TMVCStringDictionary. Create) ] ) ;
end ;
function TMVCLinks. Clear: IMVCLinks;
begin
if Assigned( fData) then
begin
fData. Clear;
end ;
Result : = Self;
end ;
constructor TMVCLinks. Create;
begin
inherited Create;
fData : = nil ;
end ;
function TMVCLinks. LinksData: TMVCStringDictionaryList;
begin
Result : = fData;
end ;
destructor TMVCLinks. Destroy;
begin
FreeAndNil( fData) ;
inherited ;
end ;
{ TMVCDecoratorObject }
function TMVCDecoratorObject. Add( const PropName,
2020-04-08 13:00:32 +02:00
PropValue: string ) : IMVCLinkItem;
2019-05-09 20:53:52 +02:00
begin
fData. Items[ PropName] : = PropValue;
Result : = Self;
end ;
constructor TMVCDecoratorObject. Create( const aData: TMVCStringDictionary) ;
begin
inherited Create;
fData : = aData;
end ;
{ TMVCNamedPairList }
constructor TMVCStringDictionaryList. Create;
begin
inherited Create( True ) ;
end ;
2020-04-08 13:00:32 +02:00
procedure Base64StringToFile( const aBase64String, AFileName: string ; const aOverwrite: Boolean = False ) ;
2019-05-16 00:16:55 +02:00
var
lSS: TStringStream;
lFile: TFileStream;
begin
lSS : = TStringStream. Create;
try
lSS. WriteString( aBase64String) ;
lSS. Position : = 0 ;
if aOverwrite then
begin
if TFile. Exists( AFileName) then
begin
TFile. Delete( AFileName) ;
end ;
end ;
lFile : = TFileStream. Create( AFileName, fmCreate) ;
try
2019-06-24 20:59:33 +02:00
TMVCSerializerHelper. DecodeStream( lSS, lFile) ;
2019-05-16 00:16:55 +02:00
finally
lFile. Free;
end ;
finally
lSS. Free;
end ;
end ;
2020-04-08 13:00:32 +02:00
function FileToBase64String( const FileName: string ) : string ;
2019-05-16 00:16:55 +02:00
var
lTemplateFileB64: TStringStream;
lTemplateFile: TFileStream;
begin
lTemplateFileB64 : = TStringStream. Create;
try
lTemplateFile : = TFileStream. Create( FileName, fmOpenRead) ;
try
2019-06-24 20:59:33 +02:00
TMVCSerializerHelper. EncodeStream( lTemplateFile, lTemplateFileB64) ;
2019-05-16 00:16:55 +02:00
finally
lTemplateFile. Free;
end ;
Result : = lTemplateFileB64. DataString;
finally
lTemplateFileB64. Free;
end ;
end ;
2020-04-08 13:00:32 +02:00
class procedure TMVCParseAuthentication. OnParseAuthentication( AContext: TIdContext; const AAuthType, AAuthData: string ;
var VUsername,
VPassword: string ; var VHandled: Boolean ) ;
2019-07-24 20:48:22 +02:00
begin
VHandled : = SameText( LowerCase( AAuthType) , 'bearer' ) ;
end ;
2020-04-09 16:04:45 +02:00
{ TMVCGuidHelper }
class function TMVCGuidHelper. GuidFromString( const AGuidStr: string ) : TGUID;
var
2020-04-18 23:32:24 +02:00
LGuidStr: string ;
2020-04-09 16:04:45 +02:00
begin
if AGuidStr. Length = 3 2 then { string uuid without braces and dashes: ae502abe430bb23a28782d18d6a6e465 }
begin
LGuidStr : = Format( '{%s-%s-%s-%s-%s}' , [ AGuidStr. Substring( 0 , 8 ) , AGuidStr. Substring( 8 , 4 ) ,
AGuidStr. Substring( 1 2 , 4 ) , AGuidStr. Substring( 1 6 , 4 ) , AGuidStr. Substring( 2 0 , 1 2 ) ] )
end
else if AGuidStr. Length = 3 6 then { string uuid without braces: ae502abe-430b-b23a-2878-2d18d6a6e465 }
begin
LGuidStr : = Format( '{%s}' , [ AGuidStr] )
end
else
begin
LGuidStr : = AGuidStr;
end ;
Result : = StringToGUID( LGuidStr) ;
end ;
2020-04-20 17:56:17 +02:00
function CamelCase( const Value: string ; const MakeFirstUpperToo: Boolean ) : string ;
var
I: Integer ;
lNextUpCase: Boolean ;
lSB: TStringBuilder;
2020-04-24 16:36:18 +02:00
C: Char ;
lIsLowerCase: Boolean ;
lIsUpperCase, lPreviousWasUpperCase: Boolean ;
lIsAlpha: Boolean ;
2020-10-17 01:12:20 +02:00
lIsNumber: Boolean ;
2020-04-18 23:32:24 +02:00
begin
2020-07-18 20:14:58 +02:00
{TODO -oDanieleT -cGeneral : Make this function faster!}
2020-04-20 17:56:17 +02:00
lNextUpCase : = MakeFirstUpperToo;
2020-04-24 16:36:18 +02:00
lPreviousWasUpperCase : = True ;
2020-04-20 17:56:17 +02:00
lSB : = TStringBuilder. Create;
try
for I : = 0 to Length( Value) - 1 do
begin
2020-04-24 16:36:18 +02:00
C : = Value. Chars[ I] ;
lIsLowerCase : = CharInSet( C, [ 'a' .. 'z' ] ) ;
lIsUpperCase : = CharInSet( C, [ 'A' .. 'Z' ] ) ;
2020-10-17 01:12:20 +02:00
lIsNumber : = CharInSet( C, [ '0' .. '9' ] ) ;
2020-04-24 16:36:18 +02:00
lIsAlpha : = lIsLowerCase or lIsUpperCase;
2020-10-17 01:12:20 +02:00
if not ( lIsAlpha or lIsNumber) then
2020-04-20 17:56:17 +02:00
begin
lNextUpCase : = True ;
2020-04-24 16:36:18 +02:00
lPreviousWasUpperCase : = False ;
2020-04-20 17:56:17 +02:00
Continue;
end
else
begin
if lNextUpCase then
begin
lNextUpCase : = False ;
2020-04-24 16:36:18 +02:00
lSB. Append( UpCase( C) ) ;
2020-04-20 17:56:17 +02:00
end
else
begin
2020-04-24 16:36:18 +02:00
if lPreviousWasUpperCase then
begin
lSB. Append( LowerCase( C) ) ;
end
else
begin
lSB. Append( C) ;
end ;
2020-04-20 17:56:17 +02:00
end ;
end ;
2020-04-24 16:36:18 +02:00
lPreviousWasUpperCase : = lIsUpperCase;
2020-10-17 01:12:20 +02:00
if lIsNumber then
begin
lNextUpCase : = True ;
end ;
2020-04-20 17:56:17 +02:00
end ;
Result : = lSB. ToString;
2020-09-29 19:57:43 +02:00
finally
lSB. Free;
end ;
end ;
function SnakeCase( const Value: string ) : string ;
2020-10-17 00:40:06 +02:00
var
I: Integer ;
lSB: TStringBuilder;
C: Char ;
2020-10-17 17:32:55 +02:00
lIsUpperCase: Boolean ;
lIsLowerCase: Boolean ;
lLastWasLowercase: Boolean ;
2020-10-17 00:40:06 +02:00
lIsNumber: Boolean ;
lLastWasUnderscore: Boolean ;
lIsUnderscore: Boolean ;
2020-10-17 01:12:20 +02:00
lLastWasNumber: Boolean ;
2020-10-17 00:40:06 +02:00
begin
lLastWasLowercase : = False ;
lLastWasUnderscore : = False ;
2020-10-17 01:12:20 +02:00
lLastWasNumber : = False ;
2020-10-17 00:40:06 +02:00
lSB : = TStringBuilder. Create;
try
for I : = 0 to Length( Value) - 1 do
begin
C : = Value. Chars[ I] ;
lIsUpperCase : = CharInSet( C, [ 'A' .. 'Z' ] ) ;
lIsLowerCase : = CharInSet( C, [ 'a' .. 'z' ] ) ;
lIsNumber : = CharInSet( C, [ '0' .. '9' ] ) ;
lIsUnderscore : = C = '_' ;
2020-10-17 17:32:55 +02:00
if ( I > 0 ) and ( not lLastWasUnderscore) and
( ( lIsUpperCase and ( lLastWasLowercase or lLastWasNumber) ) or
( lIsLowerCase and lLastWasNumber) or
( lIsNumber and ( not lLastWasNumber) ) ) then
2020-10-17 00:40:06 +02:00
begin
lSB. Append( '_' ) ;
end ;
2020-10-17 17:32:55 +02:00
2020-10-17 00:40:06 +02:00
if not ( lLastWasUnderscore and lIsUnderscore) then
begin
lSB. Append( LowerCase( C) ) ;
end ;
lLastWasUnderscore : = lIsUnderscore;
lLastWasLowercase : = lIsLowerCase;
2020-10-17 01:12:20 +02:00
lLastWasNumber : = lIsNumber;
2020-10-17 00:40:06 +02:00
end ;
Result : = lSB. ToString;
finally
lSB. Free;
end ;
end ;
2020-04-18 23:32:24 +02:00
2020-06-24 00:00:47 +02:00
function StrToJSONObject( const aString: String ) : TJsonObject;
begin
Result : = MVCFramework. Serializer. JSONDataObjects. StrToJSONObject( aString) ;
end ;
function StrToJSONArray( const aString: String ) : TJsonArray;
begin
Result : = MVCFramework. Serializer. JSONDataObjects. StrToJSONArray( aString) ;
end ;
2020-09-22 15:18:22 +02:00
function WrapAsList( const AObject: TObject; AOwnsObject: Boolean = False ) : IMVCList;
begin
Result : = MVCFramework. DuckTyping. WrapAsList( AObject, AOwnsObject) ;
end ;
2014-04-01 00:02:31 +02:00
initialization
2019-05-08 20:20:14 +02:00
gLock : = TObject. Create;
2014-04-01 00:02:31 +02:00
2017-07-05 00:17:46 +02:00
// SGR 2017-07-03 : Initialize decoding table for URLSafe Gb64 encoding
2019-03-16 17:20:28 +01:00
TURLSafeDecode. ConstructDecodeTable( GURLSafeBase64CodeTable,
TURLSafeDecode. GSafeBaseBase64DecodeTable) ;
2017-07-05 00:17:46 +02:00
2017-03-20 13:55:46 +01:00
GlobalAppExe : = ExtractFileName( GetModuleName( HInstance) ) ;
GlobalAppName : = ChangeFileExt( GlobalAppExe, EmptyStr) ;
GlobalAppPath : = IncludeTrailingPathDelimiter( ExtractFilePath( GetModuleName( HInstance) ) ) ;
2016-09-25 16:17:37 +02:00
2014-04-01 00:02:31 +02:00
finalization
2019-05-08 20:20:14 +02:00
FreeAndNil( gLock) ;
2014-04-01 00:02:31 +02:00
2013-10-30 00:48:23 +01:00
end .