|
@@ -89,8 +89,10 @@ Type
|
|
FOptions: TJSONRPCDispatchOptions;
|
|
FOptions: TJSONRPCDispatchOptions;
|
|
FRequest: TRequest;
|
|
FRequest: TRequest;
|
|
FResponse: TResponse;
|
|
FResponse: TResponse;
|
|
|
|
+ FResponseContentType: String;
|
|
procedure SetDispatcher(const AValue: TCustomJSONRPCDispatcher);
|
|
procedure SetDispatcher(const AValue: TCustomJSONRPCDispatcher);
|
|
Protected
|
|
Protected
|
|
|
|
+ Function GetResponseContentType : String;
|
|
Function CreateDispatcher : TCustomJSONRPCDispatcher; virtual;
|
|
Function CreateDispatcher : TCustomJSONRPCDispatcher; virtual;
|
|
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
|
|
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
|
|
Property Dispatcher : TCustomJSONRPCDispatcher Read FDispatcher Write SetDispatcher;
|
|
Property Dispatcher : TCustomJSONRPCDispatcher Read FDispatcher Write SetDispatcher;
|
|
@@ -102,14 +104,19 @@ Type
|
|
Property Request: TRequest Read FRequest;
|
|
Property Request: TRequest Read FRequest;
|
|
// Access to response
|
|
// Access to response
|
|
Property Response: TResponse Read FResponse;
|
|
Property Response: TResponse Read FResponse;
|
|
|
|
+ // Response Content-Type. If left empty, application/json is used.
|
|
|
|
+ Property ResponseContentType : String Read FResponseContentType Write FResponseContentType;
|
|
end;
|
|
end;
|
|
|
|
|
|
{ TJSONRPCDataModule }
|
|
{ TJSONRPCDataModule }
|
|
|
|
|
|
|
|
+ { TJSONRPCModule }
|
|
|
|
+
|
|
TJSONRPCModule = Class(TCustomJSONRPCModule)
|
|
TJSONRPCModule = Class(TCustomJSONRPCModule)
|
|
Published
|
|
Published
|
|
Property Dispatcher;
|
|
Property Dispatcher;
|
|
Property DispatchOptions;
|
|
Property DispatchOptions;
|
|
|
|
+ Property ResponseContentType;
|
|
end;
|
|
end;
|
|
|
|
|
|
implementation
|
|
implementation
|
|
@@ -118,6 +125,9 @@ implementation
|
|
uses dbugintf;
|
|
uses dbugintf;
|
|
{$endif}
|
|
{$endif}
|
|
|
|
|
|
|
|
+Const
|
|
|
|
+ SApplicationJSON = 'application/json';
|
|
|
|
+
|
|
{ TCustomJSONRPCContentProducer }
|
|
{ TCustomJSONRPCContentProducer }
|
|
|
|
|
|
function TCustomJSONRPCContentProducer.GetIDProperty: String;
|
|
function TCustomJSONRPCContentProducer.GetIDProperty: String;
|
|
@@ -133,7 +143,7 @@ Var
|
|
Disp : TCustomJSONRPCDispatcher;
|
|
Disp : TCustomJSONRPCDispatcher;
|
|
P : TJSONParser;
|
|
P : TJSONParser;
|
|
Req,res : TJSONData;
|
|
Req,res : TJSONData;
|
|
- R : String;
|
|
|
|
|
|
+ R : TJSONStringType;
|
|
|
|
|
|
begin
|
|
begin
|
|
Disp:=Self.GetDispatcher;
|
|
Disp:=Self.GetDispatcher;
|
|
@@ -211,6 +221,13 @@ begin
|
|
FDispatcher.FreeNotification(Self);
|
|
FDispatcher.FreeNotification(Self);
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
+function TCustomJSONRPCModule.GetResponseContentType: String;
|
|
|
|
+begin
|
|
|
|
+ Result:=FResponseContentType;
|
|
|
|
+ if Result='' then
|
|
|
|
+ Result:=SApplicationJSON;
|
|
|
|
+end;
|
|
|
|
+
|
|
function TCustomJSONRPCModule.CreateDispatcher: TCustomJSONRPCDispatcher;
|
|
function TCustomJSONRPCModule.CreateDispatcher: TCustomJSONRPCDispatcher;
|
|
|
|
|
|
Var
|
|
Var
|
|
@@ -245,6 +262,7 @@ procedure TCustomJSONRPCModule.HandleRequest(ARequest: TRequest;
|
|
Var
|
|
Var
|
|
Disp : TCustomJSONRPCDispatcher;
|
|
Disp : TCustomJSONRPCDispatcher;
|
|
res : TJSONData;
|
|
res : TJSONData;
|
|
|
|
+ R : TJSONStringType;
|
|
|
|
|
|
begin
|
|
begin
|
|
If (Dispatcher=Nil) then
|
|
If (Dispatcher=Nil) then
|
|
@@ -254,10 +272,15 @@ begin
|
|
try
|
|
try
|
|
If Assigned(Res) then
|
|
If Assigned(Res) then
|
|
begin
|
|
begin
|
|
- AResponse.Content:=Res.AsJSON;
|
|
|
|
- AResponse.ContentLength:=Length(AResponse.Content);
|
|
|
|
|
|
+ AResponse.FreeContentStream:=True;
|
|
|
|
+ AResponse.ContentStream:=TMemoryStream.Create;
|
|
|
|
+ R:=Res.AsJSON;
|
|
|
|
+ AResponse.ContentStream.WriteBuffer(R[1],Length(R));
|
|
|
|
+ AResponse.ContentLength:=AResponse.ContentStream.Size;
|
|
|
|
+ R:=''; // Free up mem
|
|
|
|
+ AResponse.ContentType:=GetResponseContentType;
|
|
end;
|
|
end;
|
|
- AResponse.SendResponse;
|
|
|
|
|
|
+ AResponse.SendResponse;
|
|
finally
|
|
finally
|
|
Res.Free;
|
|
Res.Free;
|
|
end;
|
|
end;
|