Browse Source

* Fix bug ID #35306: use rawbytestring for stringcontent

git-svn-id: trunk@42875 -
michael 6 years ago
parent
commit
def80285e6
1 changed files with 79 additions and 56 deletions
  1. 79 56
      packages/fcl-web/src/base/fphttpclient.pp

+ 79 - 56
packages/fcl-web/src/base/fphttpclient.pp

@@ -189,7 +189,7 @@ Type
     Procedure Get(Const AURL : String; Stream : TStream);
     Procedure Get(Const AURL : String; const LocalFileName : String);
     Procedure Get(Const AURL : String; Response : TStrings);
-    Function Get(Const AURL : String) : String;
+    Function Get(Const AURL : String) : RawByteString;
     // Check if responsecode is a redirect code that this class handles (301,302,303,307,308)
     Class Function IsRedirect(ACode : Integer) : Boolean; virtual;
     // If the code is a redirect, then this method  must return TRUE if the next request should happen with a GET (307/308)
@@ -198,70 +198,70 @@ Type
     Class Procedure SimpleGet(Const AURL : String; Stream : TStream);
     Class Procedure SimpleGet(Const AURL : String; const LocalFileName : String);
     Class Procedure SimpleGet(Const AURL : String; Response : TStrings);
-    Class Function SimpleGet(Const AURL : String) : String;
+    Class Function SimpleGet(Const AURL : String) : RawByteString;
     // Simple post
     // Post URL, and Requestbody. Return response in Stream, File, TstringList or String;
     Procedure Post(const URL: string; const Response: TStream);
     Procedure Post(const URL: string; Response : TStrings);
     Procedure Post(const URL: string; const LocalFileName: String);
-    function Post(const URL: string) : String;
+    function Post(const URL: string) : RawByteString;
     // Simple class methods.
     Class Procedure SimplePost(const URL: string; const Response: TStream);
     Class Procedure SimplePost(const URL: string; Response : TStrings);
     Class Procedure SimplePost(const URL: string; const LocalFileName: String);
-    Class function SimplePost(const URL: string) : String;
+    Class function SimplePost(const URL: string) : RawByteString;
     // Simple Put
     // Put URL, and Requestbody. Return response in Stream, File, TstringList or String;
     Procedure Put(const URL: string; const Response: TStream);
     Procedure Put(const URL: string; Response : TStrings);
     Procedure Put(const URL: string; const LocalFileName: String);
-    function Put(const URL: string) : String;
+    function Put(const URL: string) : RawByteString;
     // Simple class methods.
     Class Procedure SimplePut(const URL: string; const Response: TStream);
     Class Procedure SimplePut(const URL: string; Response : TStrings);
     Class Procedure SimplePut(const URL: string; const LocalFileName: String);
-    Class function SimplePut(const URL: string) : String;
+    Class function SimplePut(const URL: string) : RawByteString;
     // Simple Delete
     // Delete URL, and Requestbody. Return response in Stream, File, TstringList or String;
     Procedure Delete(const URL: string; const Response: TStream);
     Procedure Delete(const URL: string; Response : TStrings);
     Procedure Delete(const URL: string; const LocalFileName: String);
-    function Delete(const URL: string) : String;
+    function Delete(const URL: string) : RawByteString;
     // Simple class methods.
     Class Procedure SimpleDelete(const URL: string; const Response: TStream);
     Class Procedure SimpleDelete(const URL: string; Response : TStrings);
     Class Procedure SimpleDelete(const URL: string; const LocalFileName: String);
-    Class function SimpleDelete(const URL: string) : String;
+    Class function SimpleDelete(const URL: string) : RawByteString;
     // Simple Options
     // Options from URL, and Requestbody. Return response in Stream, File, TstringList or String;
     Procedure Options(const URL: string; const Response: TStream);
     Procedure Options(const URL: string; Response : TStrings);
     Procedure Options(const URL: string; const LocalFileName: String);
-    function Options(const URL: string) : String;
+    function Options(const URL: string) : RawByteString;
     // Simple class methods.
     Class Procedure SimpleOptions(const URL: string; const Response: TStream);
     Class Procedure SimpleOptions(const URL: string; Response : TStrings);
     Class Procedure SimpleOptions(const URL: string; const LocalFileName: String);
-    Class function SimpleOptions(const URL: string) : String;
+    Class function SimpleOptions(const URL: string) : RawByteString;
     // Get HEAD
     Class Procedure Head(AURL : String; Headers: TStrings);
     // Post Form data (www-urlencoded).
     // Formdata in string (urlencoded) or TStrings (plain text) format.
     // Form data will be inserted in the requestbody.
     // Return response in Stream, File, TStringList or String;
-    Procedure FormPost(const URL, FormData: string; const Response: TStream);
+    Procedure FormPost(const URL : String; FormData: RawByteString; const Response: TStream);
     Procedure FormPost(const URL : string; FormData:  TStrings; const Response: TStream);
     Procedure FormPost(const URL, FormData: string; const Response: TStrings);
     Procedure FormPost(const URL : string; FormData:  TStrings; const Response: TStrings);
-    function FormPost(const URL, FormData: string): String;
-    function FormPost(const URL: string; FormData : TStrings): String;
+    function FormPost(const URL : String; Const FormData: RawByteString): RawByteString;
+    function FormPost(const URL: string; FormData : TStrings): RawByteString;
     // Simple form 
-    Class Procedure SimpleFormPost(const URL, FormData: string; const Response: TStream);
+    Class Procedure SimpleFormPost(const URL : String; Const FormData: RawByteString; const Response: TStream);
     Class Procedure SimpleFormPost(const URL : string; FormData:  TStrings; const Response: TStream);
-    Class Procedure SimpleFormPost(const URL, FormData: string; const Response: TStrings);
+    Class Procedure SimpleFormPost(const URL : String; Const FormData: RawByteString; const Response: TStrings);
     Class Procedure SimpleFormPost(const URL : string; FormData:  TStrings; const Response: TStrings);
-    Class function SimpleFormPost(const URL, FormData: string): String;
-    Class function SimpleFormPost(const URL: string; FormData : TStrings): String;
+    Class function SimpleFormPost(const URL: String; Const FormData: RawByteString): RawByteString;
+    Class function SimpleFormPost(const URL: string; FormData : TStrings): RawByteString;
     // Post a file
     Procedure FileFormPost(const AURL, AFieldName, AFileName: string; const Response: TStream);
     // Post form with a file
@@ -460,6 +460,34 @@ begin
   SetLength(Result, P-Pchar(Result));
 end;
 
+Type
+
+  { TRawStringStream }
+
+  TRawStringStream = Class(TMemoryStream)
+  public
+    Constructor Create (const aData : RawByteString); overload;
+    function DataString: RawByteString;
+  end;
+
+constructor TRawStringStream.Create(const aData: RawByteString);
+begin
+  Inherited Create;
+  If Length(aData)>0 then
+    begin
+    WriteBuffer(aData[1],Length(aData));
+    Position:=0;
+    end;
+end;
+
+function TRawStringStream.DataString: RawByteString;
+begin
+  Result:='';
+  SetLength(Result,Size);
+  if Size>0 then
+    Move(Memory^, Result[1], Size);
+end;
+
 { TProxyData }
 
 function TProxyData.GetProxyHeaders: String;
@@ -1459,13 +1487,13 @@ begin
   Response.Text:=Get(AURL);
 end;
 
-function TFPCustomHTTPClient.Get(const AURL: String): String;
+function TFPCustomHTTPClient.Get(const AURL: String): RawByteString;
 
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create;
   try
     Get(AURL,SS);
     Result:=SS.Datastring;
@@ -1535,7 +1563,7 @@ begin
 end;
 
 
-class function TFPCustomHTTPClient.SimpleGet(const AURL: String): String;
+class function TFPCustomHTTPClient.SimpleGet(const AURL: String): RawByteString;
  
 begin
   With Self.Create(nil) do
@@ -1575,11 +1603,11 @@ begin
 end;
 
 
-function TFPCustomHTTPClient.Post(const URL: string): String;
+function TFPCustomHTTPClient.Post(const URL: string): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     Post(URL,SS);
     Result:=SS.Datastring;
@@ -1631,7 +1659,7 @@ begin
 end;
 
 
-class function TFPCustomHTTPClient.SimplePost(const URL: string): String;
+class function TFPCustomHTTPClient.SimplePost(const URL: string): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -1668,11 +1696,11 @@ begin
   end;
 end;
 
-function TFPCustomHTTPClient.Put(const URL: string): String;
+function TFPCustomHTTPClient.Put(const URL: string): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     Put(URL,SS);
     Result:=SS.Datastring;
@@ -1720,7 +1748,7 @@ begin
     end;
 end;
 
-class function TFPCustomHTTPClient.SimplePut(const URL: string): String;
+class function TFPCustomHTTPClient.SimplePut(const URL: string): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -1758,11 +1786,11 @@ begin
   end;
 end;
 
-function TFPCustomHTTPClient.Delete(const URL: string): String;
+function TFPCustomHTTPClient.Delete(const URL: string): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     Delete(URL,SS);
     Result:=SS.Datastring;
@@ -1810,7 +1838,7 @@ begin
     end;
 end;
 
-class function TFPCustomHTTPClient.SimpleDelete(const URL: string): String;
+class function TFPCustomHTTPClient.SimpleDelete(const URL: string): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -1848,11 +1876,11 @@ begin
   end;
 end;
 
-function TFPCustomHTTPClient.Options(const URL: string): String;
+function TFPCustomHTTPClient.Options(const URL: string): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     Options(URL,SS);
     Result:=SS.Datastring;
@@ -1900,7 +1928,7 @@ begin
     end;
 end;
 
-class function TFPCustomHTTPClient.SimpleOptions(const URL: string): String;
+class function TFPCustomHTTPClient.SimpleOptions(const URL: string): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -1924,11 +1952,10 @@ begin
     end;
 end;
 
-procedure TFPCustomHTTPClient.FormPost(const URL, FormData: string;
-  const Response: TStream);
+procedure TFPCustomHTTPClient.FormPost(const URL : String; FormData: RawBytestring; const Response: TStream);
 
 begin
-  RequestBody:=TStringStream.Create(FormData);
+  RequestBody:=TRawStringStream.Create(FormData);
   try
     AddHeader('Content-Type','application/x-www-form-urlencoded');
     Post(URL,Response);
@@ -1969,11 +1996,11 @@ begin
   Response.Text:=FormPost(URL,FormData);
 end;
 
-function TFPCustomHTTPClient.FormPost(const URL, FormData: string): String;
+function TFPCustomHTTPClient.FormPost(const URL : String;  Const FormData: RawBytestring): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     FormPost(URL,FormData,SS);
     Result:=SS.Datastring;
@@ -1982,11 +2009,11 @@ begin
   end;
 end;
 
-function TFPCustomHTTPClient.FormPost(const URL: string; FormData: TStrings): String;
+function TFPCustomHTTPClient.FormPost(const URL: string; FormData: TStrings): RawByteString;
 Var
-  SS : TStringStream;
+  SS : TRawStringStream;
 begin
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     FormPost(URL,FormData,SS);
     Result:=SS.Datastring;
@@ -1995,8 +2022,7 @@ begin
   end;
 end;
 
-class procedure TFPCustomHTTPClient.SimpleFormPost(const URL, FormData: string;
-  const Response: TStream);
+class procedure TFPCustomHTTPClient.SimpleFormPost(const URL : String; Const FormData: RawByteString; const Response: TStream);
 
 begin
   With Self.Create(nil) do
@@ -2023,8 +2049,7 @@ begin
 end;
 
 
-class procedure TFPCustomHTTPClient.SimpleFormPost(const URL, FormData: string;
-  const Response: TStrings);
+class procedure TFPCustomHTTPClient.SimpleFormPost(const URL : String; Const FormData: RawBytestring; const Response: TStrings);
 
 begin
   With Self.Create(nil) do
@@ -2049,8 +2074,7 @@ begin
     end;
 end;
 
-class function TFPCustomHTTPClient.SimpleFormPost(const URL, FormData: string
-  ): String;
+class function TFPCustomHTTPClient.SimpleFormPost(const URL: string;Const FormData : RawByteString): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -2062,8 +2086,7 @@ begin
     end;
 end;
 
-class function TFPCustomHTTPClient.SimpleFormPost(const URL: string;
-  FormData: TStrings): String;
+class function TFPCustomHTTPClient.SimpleFormPost(const URL: string; FormData: TStrings): RawByteString;
 
 begin
   With Self.Create(nil) do
@@ -2106,13 +2129,13 @@ procedure TFPCustomHTTPClient.StreamFormPost(const AURL: string;
   const AStream: TStream; const Response: TStream);
 Var
   S, Sep : string;
-  SS : TStringStream;
+  SS : TRawStringStream;
   I: Integer;
   N,V: String;
 begin
   Sep:=Format('%.8x_multipart_boundary',[Random($ffffff)]);
   AddHeader('Content-Type','multipart/form-data; boundary='+Sep);
-  SS:=TStringStream.Create('');
+  SS:=TRawStringStream.Create();
   try
     if (FormData<>Nil) then
       for I:=0 to FormData.Count -1 do