* Merging revisions 42875 from trunk:

------------------------------------------------------------------------
    r42875 | michael | 2019-08-30 08:57:53 +0200 (Fri, 30 Aug 2019) | 1 line
    
    * Fix bug ID #35306: use rawbytestring for stringcontent
    ------------------------------------------------------------------------

git-svn-id: branches/fixes_3_2@43250 -
This commit is contained in:
michael 2019-10-19 14:46:09 +00:00
parent dcd9071815
commit 39f9916ef5

View File

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