ADDED MVCDocAttributes

ADDED Alias MVCHTTPMethodsAttribute to MVCHTTPMethodAttribute
This commit is contained in:
danieleteti 2015-12-16 15:57:20 +01:00
parent e7eb4949ab
commit 85c04d7bb4
2 changed files with 89 additions and 109 deletions

View File

@ -8,6 +8,7 @@ uses
type type
[MVCPath('/system')] [MVCPath('/system')]
[MVCDoc('Built-in DelphiMVCFramework System controller')]
TMVCSystemController = class(TMVCController) TMVCSystemController = class(TMVCController)
protected protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string; procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
@ -15,12 +16,17 @@ type
function GetUpTime: string; function GetUpTime: string;
public public
[MVCPath('/describeserver.info')] [MVCPath('/describeserver.info')]
[MVCHTTPMethods([httpGET, httpPOST])]
[MVCDoc('Describe controllers and actions published by the RESTful server per resources')
]
procedure DescribeServer(Context: TWebContext); procedure DescribeServer(Context: TWebContext);
[MVCPath('/describeplatform.info')] [MVCPath('/describeplatform.info')]
[MVCDoc('Describe the system where server is running')]
procedure DescribePlatform(Context: TWebContext); procedure DescribePlatform(Context: TWebContext);
[MVCPath('/serverconfig.info')] [MVCPath('/serverconfig.info')]
[MVCDoc('Server configuration')]
procedure ServerConfig(Context: TWebContext); procedure ServerConfig(Context: TWebContext);
end; end;
@ -69,7 +75,8 @@ begin
try try
LJRes.AddPair('OS', TOSVersion.ToString); LJRes.AddPair('OS', TOSVersion.ToString);
LJRes.AddPair('CPU_count', TJSONNumber.Create(TThread.ProcessorCount)); LJRes.AddPair('CPU_count', TJSONNumber.Create(TThread.ProcessorCount));
LJRes.AddPair('CPU_architecture', GetEnumName(TypeInfo(TOSVersion.TArchitecture), LJRes.AddPair('CPU_architecture',
GetEnumName(TypeInfo(TOSVersion.TArchitecture),
Ord(TOSVersion.Architecture))); Ord(TOSVersion.Architecture)));
LJRes.AddPair('system_uptime', GetUpTime); LJRes.AddPair('system_uptime', GetUpTime);
ContentType := TMVCMimeType.APPLICATION_JSON; ContentType := TMVCMimeType.APPLICATION_JSON;
@ -93,6 +100,7 @@ var
LFoundAttrib: Boolean; LFoundAttrib: Boolean;
LStrRelativePath: string; LStrRelativePath: string;
LStrHTTPMethods: string; LStrHTTPMethods: string;
LStrDoc: string;
LStrConsumes: string; LStrConsumes: string;
LStrProduces: string; LStrProduces: string;
LJMethod: TJSONObject; LJMethod: TJSONObject;
@ -108,7 +116,11 @@ begin
for LAttribute in LRTTIType.GetAttributes do for LAttribute in LRTTIType.GetAttributes do
begin begin
if LAttribute is MVCPathAttribute then if LAttribute is MVCPathAttribute then
ControllerInfo.AddPair('resource_path', MVCPathAttribute(LAttribute).Path) ControllerInfo.AddPair('resource_path',
MVCPathAttribute(LAttribute).Path);
if LAttribute is MVCDocAttribute then
ControllerInfo.AddPair('description',
MVCDocAttribute(LAttribute).Value);
end; end;
LJMethods := TJSONArray.Create; LJMethods := TJSONArray.Create;
@ -121,8 +133,15 @@ begin
LStrHTTPMethods := ''; LStrHTTPMethods := '';
LStrConsumes := ''; LStrConsumes := '';
LStrProduces := ''; LStrProduces := '';
LStrHTTPMethods :=
'httpGET,httpPOST,httpPUT,httpDELETE,httpHEAD,httpOPTIONS,httpPATCH,httpTRACE';
for LAttribute in LMethod.GetAttributes do for LAttribute in LMethod.GetAttributes do
begin begin
if LAttribute is MVCDocAttribute then
begin
LStrDoc := MVCDocAttribute(LAttribute).Value;
LFoundAttrib := true;
end;
if LAttribute is MVCPathAttribute then if LAttribute is MVCPathAttribute then
begin begin
LStrRelativePath := MVCPathAttribute(LAttribute).Path; LStrRelativePath := MVCPathAttribute(LAttribute).Path;
@ -130,7 +149,8 @@ begin
end; end;
if LAttribute is MVCHTTPMethodAttribute then if LAttribute is MVCHTTPMethodAttribute then
begin begin
LStrHTTPMethods := MVCHTTPMethodAttribute(LAttribute).MVCHTTPMethodsAsString; LStrHTTPMethods := MVCHTTPMethodAttribute(LAttribute)
.MVCHTTPMethodsAsString;
LFoundAttrib := true; LFoundAttrib := true;
end; end;
if LAttribute is MVCConsumesAttribute then if LAttribute is MVCConsumesAttribute then
@ -153,6 +173,7 @@ begin
LJMethod.AddPair('consumes', LStrConsumes); LJMethod.AddPair('consumes', LStrConsumes);
LJMethod.AddPair('produces', LStrProduces); LJMethod.AddPair('produces', LStrProduces);
LJMethod.AddPair('http_methods', LStrHTTPMethods); LJMethod.AddPair('http_methods', LStrHTTPMethods);
LJMethod.AddPair('description', LStrDoc);
LJMethods.AddElement(LJMethod); LJMethods.AddElement(LJMethod);
end; end;
end; end;
@ -169,15 +190,15 @@ begin
Result := MSecToTime(GetTickCount); Result := MSecToTime(GetTickCount);
end; end;
procedure TMVCSystemController.OnBeforeAction(Context: TWebContext; const AActionNAme: string; procedure TMVCSystemController.OnBeforeAction(Context: TWebContext;
var Handled: Boolean); const AActionNAme: string; var Handled: Boolean);
var var
LClientIP: string; LClientIP: string;
begin begin
inherited; inherited;
LClientIP := Context.Request.ClientIP; LClientIP := Context.Request.ClientIP;
Handled := not((LClientIP = '127.0.0.1') or (LClientIP = '0:0:0:0:0:0:0:1') or Handled := not((LClientIP = '::1') or (LClientIP = '127.0.0.1') or
(LClientIP.ToLower = 'localhost')); (LClientIP = '0:0:0:0:0:0:0:1') or (LClientIP.ToLower = 'localhost'));
end; end;
procedure TMVCSystemController.ServerConfig(Context: TWebContext); procedure TMVCSystemController.ServerConfig(Context: TWebContext);

View File

@ -51,6 +51,8 @@ type
end; end;
MVCHTTPMethodsAttribute = MVCHTTPMethodAttribute; //just an alias
MVCBaseAttribute = class(TCustomAttribute) MVCBaseAttribute = class(TCustomAttribute)
end; end;
@ -68,6 +70,10 @@ type
end; end;
MVCDocAttribute = class(MVCStringAttribute)
end;
MVCProducesAttribute = class(MVCStringAttribute) MVCProducesAttribute = class(MVCStringAttribute)
private private
FProduceEncoding: string; FProduceEncoding: string;
@ -117,7 +123,7 @@ type
destructor Destroy; override; destructor Destroy; override;
procedure SetParamsTable(AParamsTable: TMVCRequestParamsTable); procedure SetParamsTable(AParamsTable: TMVCRequestParamsTable);
function GetParamNames: TArray<string>; function GetParamNames: TArray<string>;
function ClientIP: string; virtual; abstract; function ClientIP: string; virtual;
function ClientPrefer(MimeType: string): boolean; function ClientPrefer(MimeType: string): boolean;
function ThereIsRequestBody: boolean; function ThereIsRequestBody: boolean;
function Accept: string; function Accept: string;
@ -152,21 +158,17 @@ type
TMVCApacheWebRequest = class(TMVCWebRequest) TMVCApacheWebRequest = class(TMVCWebRequest)
public public
constructor Create(AWebRequest: TWebRequest); override; constructor Create(AWebRequest: TWebRequest); override;
function ClientIP: string; override;
end; end;
{$ENDIF} {$ENDIF}
TMVCISAPIWebRequest = class(TMVCWebRequest) TMVCISAPIWebRequest = class(TMVCWebRequest)
public public
constructor Create(AWebRequest: TWebRequest); override; constructor Create(AWebRequest: TWebRequest); override;
function ClientIP: string; override;
end; end;
TMVCINDYWebRequest = class(TMVCWebRequest) TMVCINDYWebRequest = class(TMVCWebRequest)
public public
constructor Create(AWebRequest: TWebRequest); override; constructor Create(AWebRequest: TWebRequest); override;
function ClientIP: string; override;
end; end;
{$IFDEF IOCP} {$IFDEF IOCP}
@ -1335,6 +1337,60 @@ begin
[ContentType]); [ContentType]);
end; end;
function TMVCWebRequest.ClientIP: string;
{
This code has been converted to Delphi from a PHP code
http://www.grantburton.com/2008/11/30/fix-for-incorrect-ip-addresses-in-wordpress-comments/
}
var
S: string;
begin
if FWebRequest.GetFieldByName('HTTP_CLIENT_IP') <> '' then
Exit(FWebRequest.GetFieldByName('HTTP_CLIENT_IP'));
for S in String(FWebRequest.GetFieldByName('HTTP_X_FORWARDED_FOR'))
.Split([',']) do
begin
if not S.trim.IsEmpty then
Exit(S.trim);
end;
if FWebRequest.GetFieldByName('HTTP_X_FORWARDED') <> '' then
Exit(FWebRequest.GetFieldByName('HTTP_X_FORWARDED'));
if FWebRequest.GetFieldByName('HTTP_X_CLUSTER_CLIENT_IP') <> '' then
Exit(FWebRequest.GetFieldByName('HTTP_X_CLUSTER_CLIENT_IP'));
if FWebRequest.GetFieldByName('HTTP_FORWARDED_FOR') <> '' then
Exit(FWebRequest.GetFieldByName('HTTP_FORWARDED_FOR'));
if FWebRequest.GetFieldByName('HTTP_FORWARDED') <> '' then
Exit(FWebRequest.GetFieldByName('HTTP_FORWARDED'));
if FWebRequest.GetFieldByName('REMOTE_ADDR') <> '' then
Exit(FWebRequest.GetFieldByName('REMOTE_ADDR'));
if FWebRequest.RemoteIP <> '' then
Exit(FWebRequest.RemoteIP);
if FWebRequest.RemoteAddr <> '' then
Exit(FWebRequest.RemoteAddr);
if FWebRequest.RemoteHost <> '' then
Exit(FWebRequest.RemoteHost);
if FWebRequest.RemoteAddr <> '' then
Exit(FWebRequest.RemoteAddr);
if FWebRequest.RemoteIP <> '' then
Exit(FWebRequest.RemoteIP);
if FWebRequest.RemoteHost <> '' then
Exit(FWebRequest.RemoteHost);
Result := '';
end;
function TMVCWebRequest.ClientPrefer(MimeType: string): boolean; function TMVCWebRequest.ClientPrefer(MimeType: string): boolean;
begin begin
Result := AnsiPos(MimeType, LowerCase(RawWebRequest.Accept)) = 1; Result := AnsiPos(MimeType, LowerCase(RawWebRequest.Accept)) = 1;
@ -1416,8 +1472,6 @@ begin
inherited; inherited;
end; end;
procedure TMVCController.EnqueueMessageOnTopicOrQueue(const IsQueue: boolean; procedure TMVCController.EnqueueMessageOnTopicOrQueue(const IsQueue: boolean;
const ATopic: string; AJSONObject: TJSONObject; AOwnsInstance: boolean); const ATopic: string; AJSONObject: TJSONObject; AOwnsInstance: boolean);
var var
@ -2066,11 +2120,6 @@ end;
{ TMVCISAPIWebRequest } { TMVCISAPIWebRequest }
function TMVCISAPIWebRequest.ClientIP: string;
begin
raise EMVCException.Create('<TMVCISAPIWebRequest.ClientIP> Not implemented');
end;
constructor TMVCISAPIWebRequest.Create(AWebRequest: TWebRequest); constructor TMVCISAPIWebRequest.Create(AWebRequest: TWebRequest);
begin begin
inherited; inherited;
@ -2080,11 +2129,6 @@ end;
{ TMVCApacheWebRequest } { TMVCApacheWebRequest }
{$IF CompilerVersion >= 27} {$IF CompilerVersion >= 27}
function TMVCApacheWebRequest.ClientIP: string;
begin
raise EMVCException.Create('<TMVCApacheWebRequest.ClientIP> Not implemented');
end;
constructor TMVCApacheWebRequest.Create(AWebRequest: TWebRequest); constructor TMVCApacheWebRequest.Create(AWebRequest: TWebRequest);
begin begin
inherited; inherited;
@ -2093,91 +2137,6 @@ end;
{$ENDIF} {$ENDIF}
{ TMVCINDYWebRequest } { TMVCINDYWebRequest }
function TMVCINDYWebRequest.ClientIP: string;
{
This code has been converted to Delphi from a PHP code
http://www.grantburton.com/2008/11/30/fix-for-incorrect-ip-addresses-in-wordpress-comments/
}
function CheckIP(IP: string): boolean;
// var
// IPv6Address: TIdIPv6Address;
// LErr: boolean;
begin
// this is not a real check, it checks only if the IP is not empty
Result := not IP.IsEmpty;
//
// idglobal.IPv6ToIdIPv6Address(IP, IPv6Address, LErr);
// Result := LErr and (not IP.IsEmpty) and { (IP2Long(IP) <> -1) and }
// (IP2Long(IP) > 0);
end;
var
S: string;
req: TIdHTTPAppRequestHack;
{$IFDEF IOCP}
Headers: TStringList;
{$ELSE}
Headers: TIdHeaderList;
{$ENDIF}
begin
req := TIdHTTPAppRequestHack(FWebRequest);
{$IFDEF IOCP}
Headers := req.FHttpConnection.RequestHeader;
{$ELSE}
Headers := req.FRequestInfo.RawHeaders;
{$ENDIF}
if CheckIP(Headers.Values['HTTP_CLIENT_IP']) then
Exit(Headers.Values['HTTP_CLIENT_IP']);
for S in Headers.Values['HTTP_X_FORWARDED_FOR'].Split([',']) do
begin
if CheckIP(S.trim) then
Exit(S.trim);
end;
if CheckIP(Headers.Values['HTTP_X_FORWARDED']) then
Exit(Headers.Values['HTTP_X_FORWARDED']);
if CheckIP(Headers.Values['HTTP_X_CLUSTER_CLIENT_IP']) then
Exit(Headers.Values['HTTP_X_CLUSTER_CLIENT_IP']);
if CheckIP(Headers.Values['HTTP_FORWARDED_FOR']) then
Exit(Headers.Values['HTTP_FORWARDED_FOR']);
if CheckIP(Headers.Values['HTTP_FORWARDED']) then
Exit(Headers.Values['HTTP_FORWARDED']);
if CheckIP(Headers.Values['REMOTE_ADDR']) then
Exit(Headers.Values['REMOTE_ADDR']);
if CheckIP(FWebRequest.RemoteIP) then
Exit(FWebRequest.RemoteIP);
if CheckIP(FWebRequest.RemoteAddr) then
Exit(FWebRequest.RemoteAddr);
if CheckIP(FWebRequest.RemoteHost) then
Exit(FWebRequest.RemoteHost);
if CheckIP(req.RemoteAddr) then
Exit(req.RemoteAddr);
if CheckIP(req.RemoteIP) then
Exit(req.RemoteIP);
if CheckIP(req.RemoteHost) then
Exit(req.RemoteHost);
Result := '';
end;
constructor TMVCINDYWebRequest.Create(AWebRequest: TWebRequest); constructor TMVCINDYWebRequest.Create(AWebRequest: TWebRequest);
begin begin
inherited; inherited;