Browse Source

* Set content-type on response (bug ID 35990)

git-svn-id: trunk@42768 -
michael 6 years ago
parent
commit
a7f363e246
1 changed files with 27 additions and 4 deletions
  1. 27 4
      packages/fcl-web/src/jsonrpc/webjsonrpc.pp

+ 27 - 4
packages/fcl-web/src/jsonrpc/webjsonrpc.pp

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