|
@@ -56,6 +56,7 @@ Type
|
|
constructor Create(ACollection: TCollection); override;
|
|
constructor Create(ACollection: TCollection); override;
|
|
Destructor Destroy; override;
|
|
Destructor Destroy; override;
|
|
Procedure Assign(Source: TPersistent); override;
|
|
Procedure Assign(Source: TPersistent); override;
|
|
|
|
+ Procedure ConfigConnection(aConn : TSQLConnection); virtual;
|
|
Published
|
|
Published
|
|
// Always use this connection instance
|
|
// Always use this connection instance
|
|
Property SingleConnection : TSQLConnection Read FConnection Write SetConnection;
|
|
Property SingleConnection : TSQLConnection Read FConnection Write SetConnection;
|
|
@@ -160,7 +161,9 @@ Type
|
|
Class Var FIOClass : TRestIOClass;
|
|
Class Var FIOClass : TRestIOClass;
|
|
Class Var FDBHandlerClass : TSQLDBRestDBHandlerClass;
|
|
Class Var FDBHandlerClass : TSQLDBRestDBHandlerClass;
|
|
private
|
|
private
|
|
|
|
+ FCORSAllowCredentials: Boolean;
|
|
FCORSAllowedOrigins: String;
|
|
FCORSAllowedOrigins: String;
|
|
|
|
+ FCORSMaxAge: Integer;
|
|
FDispatchOptions: TRestDispatcherOptions;
|
|
FDispatchOptions: TRestDispatcherOptions;
|
|
FInputFormat: String;
|
|
FInputFormat: String;
|
|
FCustomViewResource : TSQLDBRestResource;
|
|
FCustomViewResource : TSQLDBRestResource;
|
|
@@ -252,6 +255,7 @@ Type
|
|
// General HTTP handling
|
|
// General HTTP handling
|
|
procedure DoRegisterRoutes; virtual;
|
|
procedure DoRegisterRoutes; virtual;
|
|
procedure DoHandleEvent(IsBefore : Boolean;IO: TRestIO); virtual;
|
|
procedure DoHandleEvent(IsBefore : Boolean;IO: TRestIO); virtual;
|
|
|
|
+ function ResolvedCORSAllowedOrigins: String; virtual;
|
|
procedure HandleCORSRequest(aConnection: TSQLDBRestConnection; IO: TRestIO); virtual;
|
|
procedure HandleCORSRequest(aConnection: TSQLDBRestConnection; IO: TRestIO); virtual;
|
|
procedure HandleResourceRequest(aConnection : TSQLDBRestConnection; IO: TRestIO); virtual;
|
|
procedure HandleResourceRequest(aConnection : TSQLDBRestConnection; IO: TRestIO); virtual;
|
|
procedure DoHandleRequest(IO: TRestIO); virtual;
|
|
procedure DoHandleRequest(IO: TRestIO); virtual;
|
|
@@ -296,6 +300,10 @@ Type
|
|
Property EnforceLimit : Integer Read FEnforceLimit Write FEnforceLimit;
|
|
Property EnforceLimit : Integer Read FEnforceLimit Write FEnforceLimit;
|
|
// Domains that are allowed to use this REST service
|
|
// Domains that are allowed to use this REST service
|
|
Property CORSAllowedOrigins: String Read FCORSAllowedOrigins Write FCORSAllowedOrigins;
|
|
Property CORSAllowedOrigins: String Read FCORSAllowedOrigins Write FCORSAllowedOrigins;
|
|
|
|
+ // Access-Control-Max-Age header value. Set to zero not to send the header
|
|
|
|
+ Property CORSMaxAge : Integer Read FCORSMaxAge Write FCORSMaxAge;
|
|
|
|
+ // Access-Control-Allow-Credentials header value. Set to zero not to send the header
|
|
|
|
+ Property CORSAllowCredentials : Boolean Read FCORSAllowCredentials Write FCORSAllowCredentials;
|
|
// Called when Basic authentication is sufficient.
|
|
// Called when Basic authentication is sufficient.
|
|
Property OnBasicAuthentication : TBasicAuthenticationEvent Read FOnBasicAuthentication Write FOnBasicAuthentication;
|
|
Property OnBasicAuthentication : TBasicAuthenticationEvent Read FOnBasicAuthentication Write FOnBasicAuthentication;
|
|
// Allow a particular resource or not.
|
|
// Allow a particular resource or not.
|
|
@@ -623,6 +631,8 @@ begin
|
|
FOutputOptions:=allOutputOptions;
|
|
FOutputOptions:=allOutputOptions;
|
|
FDispatchOptions:=DefaultDispatcherOptions;
|
|
FDispatchOptions:=DefaultDispatcherOptions;
|
|
FStatus:=CreateRestStatusConfig;
|
|
FStatus:=CreateRestStatusConfig;
|
|
|
|
+ FCORSMaxAge:=SecsPerDay;
|
|
|
|
+ FCORSAllowCredentials:=True;
|
|
end;
|
|
end;
|
|
|
|
|
|
destructor TSQLDBRestDispatcher.Destroy;
|
|
destructor TSQLDBRestDispatcher.Destroy;
|
|
@@ -683,7 +693,10 @@ Var
|
|
begin
|
|
begin
|
|
Result:=TSQLDBRestResource.Create(Nil);
|
|
Result:=TSQLDBRestResource.Create(Nil);
|
|
Result.ResourceName:='metaData';
|
|
Result.ResourceName:='metaData';
|
|
- Result.AllowedOperations:=[roGet];
|
|
|
|
|
|
+ if rdoHandleCORS in DispatchOptions then
|
|
|
|
+ Result.AllowedOperations:=[roGet,roOptions,roHead]
|
|
|
|
+ else
|
|
|
|
+ Result.AllowedOperations:=[roGet,roHead];
|
|
Result.Fields.AddField('name',rftString,[foRequired]);
|
|
Result.Fields.AddField('name',rftString,[foRequired]);
|
|
Result.Fields.AddField('schemaName',rftString,[foRequired]);
|
|
Result.Fields.AddField('schemaName',rftString,[foRequired]);
|
|
for O in TRestOperation do
|
|
for O in TRestOperation do
|
|
@@ -704,7 +717,10 @@ Var
|
|
begin
|
|
begin
|
|
Result:=TSQLDBRestResource.Create(Nil);
|
|
Result:=TSQLDBRestResource.Create(Nil);
|
|
Result.ResourceName:='metaDataField';
|
|
Result.ResourceName:='metaDataField';
|
|
- Result.AllowedOperations:=[roGet];
|
|
|
|
|
|
+ if rdoHandleCORS in DispatchOptions then
|
|
|
|
+ Result.AllowedOperations:=[roGet,roOptions,roHead]
|
|
|
|
+ else
|
|
|
|
+ Result.AllowedOperations:=[roGet,roHead];
|
|
Result.Fields.AddField('name',rftString,[]);
|
|
Result.Fields.AddField('name',rftString,[]);
|
|
Result.Fields.AddField('type',rftString,[]);
|
|
Result.Fields.AddField('type',rftString,[]);
|
|
Result.Fields.AddField('maxlen',rftInteger,[]);
|
|
Result.Fields.AddField('maxlen',rftInteger,[]);
|
|
@@ -864,14 +880,7 @@ begin
|
|
if (Result=Nil) then
|
|
if (Result=Nil) then
|
|
begin
|
|
begin
|
|
Result:=CreateConnection;
|
|
Result:=CreateConnection;
|
|
- Result.CharSet:=aConnection.CharSet;
|
|
|
|
- Result.HostName:=aConnection.HostName;
|
|
|
|
- Result.DatabaseName:=aConnection.DatabaseName;
|
|
|
|
- Result.UserName:=aConnection.UserName;
|
|
|
|
- Result.Password:=aConnection.Password;
|
|
|
|
- Result.Params:=Aconnection.Params;
|
|
|
|
- if Result is TRestSQLConnector then
|
|
|
|
- TRestSQLConnector(Result).ConnectorType:=aConnection.ConnectionType;
|
|
|
|
|
|
+ aConnection.ConfigConnection(Result);
|
|
aConnection.SingleConnection:=Result;
|
|
aConnection.SingleConnection:=Result;
|
|
end;
|
|
end;
|
|
end;
|
|
end;
|
|
@@ -1162,7 +1171,14 @@ begin
|
|
raise ESQLDBRest.Create(FStatus.GetStatusCode(rsInvalidParam), SErrNoSQLStatement); // Should never happen.
|
|
raise ESQLDBRest.Create(FStatus.GetStatusCode(rsInvalidParam), SErrNoSQLStatement); // Should never happen.
|
|
Result:=CreateCustomViewDataset(IO,RN,aOwner);
|
|
Result:=CreateCustomViewDataset(IO,RN,aOwner);
|
|
end
|
|
end
|
|
|
|
+end;
|
|
|
|
|
|
|
|
+function TSQLDBRestDispatcher.ResolvedCORSAllowedOrigins: String;
|
|
|
|
+
|
|
|
|
+begin
|
|
|
|
+ Result:=FCORSAllowedOrigins;
|
|
|
|
+ if Result='' then
|
|
|
|
+ Result:='*';
|
|
end;
|
|
end;
|
|
|
|
|
|
procedure TSQLDBRestDispatcher.HandleCORSRequest(aConnection : TSQLDBRestConnection; IO : TRestIO);
|
|
procedure TSQLDBRestDispatcher.HandleCORSRequest(aConnection : TSQLDBRestConnection; IO : TRestIO);
|
|
@@ -1184,12 +1200,13 @@ begin
|
|
end
|
|
end
|
|
else
|
|
else
|
|
begin
|
|
begin
|
|
- S:=FCORSAllowedOrigins;
|
|
|
|
- if S='' then
|
|
|
|
- S:='*';
|
|
|
|
- IO.Response.SetCustomHeader('Access-Control-Allow-Origin',S);
|
|
|
|
|
|
+ IO.Response.SetCustomHeader('Access-Control-Allow-Origin',ResolvedCORSAllowedOrigins);
|
|
S:=IO.Resource.GetHTTPAllow;
|
|
S:=IO.Resource.GetHTTPAllow;
|
|
IO.Response.SetCustomHeader('Access-Control-Allow-Methods',S);
|
|
IO.Response.SetCustomHeader('Access-Control-Allow-Methods',S);
|
|
|
|
+ IO.Response.SetCustomHeader('Access-Control-Allow-Headers','x-requested-with, content-type, authorization');
|
|
|
|
+ if CorsMaxAge>0 then
|
|
|
|
+ IO.Response.SetCustomHeader('Access-Control-Max-Age',IntToStr(CorsMaxAge));
|
|
|
|
+ IO.Response.SetCustomHeader('Access-Control-Allow-Credentials',BoolToStr(CORSAllowCredentials,'true','false'));
|
|
IO.Response.Code:=FStatus.GetStatusCode(rsCORSOK);
|
|
IO.Response.Code:=FStatus.GetStatusCode(rsCORSOK);
|
|
IO.Response.CodeText:='OK';
|
|
IO.Response.CodeText:='OK';
|
|
end;
|
|
end;
|
|
@@ -1209,6 +1226,8 @@ begin
|
|
try
|
|
try
|
|
IO.SetConn(Conn,TR);
|
|
IO.SetConn(Conn,TR);
|
|
Try
|
|
Try
|
|
|
|
+ if (rdoHandleCORS in DispatchOptions) then
|
|
|
|
+ IO.Response.SetCustomHeader('Access-Control-Allow-Origin',ResolvedCORSAllowedOrigins);
|
|
if not AuthenticateRequest(IO,True) then
|
|
if not AuthenticateRequest(IO,True) then
|
|
exit;
|
|
exit;
|
|
if Not CheckResourceAccess(IO) then
|
|
if Not CheckResourceAccess(IO) then
|
|
@@ -1836,6 +1855,18 @@ begin
|
|
inherited Assign(Source);
|
|
inherited Assign(Source);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+procedure TSQLDBRestConnection.ConfigConnection(aConn: TSQLConnection);
|
|
|
|
+begin
|
|
|
|
+ aConn.CharSet:=Self.CharSet;
|
|
|
|
+ aConn.HostName:=Self.HostName;
|
|
|
|
+ aConn.DatabaseName:=Self.DatabaseName;
|
|
|
|
+ aConn.UserName:=Self.UserName;
|
|
|
|
+ aConn.Password:=Self.Password;
|
|
|
|
+ aConn.Params:=Self.Params;
|
|
|
|
+ if aConn is TSQLConnector then
|
|
|
|
+ TSQLConnector(aConn).ConnectorType:=Self.ConnectionType;
|
|
|
|
+end;
|
|
|
|
+
|
|
|
|
|
|
Procedure InitSQLDBRest;
|
|
Procedure InitSQLDBRest;
|
|
|
|
|