mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-17 05:19:17 +02:00
* Made creating socket handler configurable
git-svn-id: trunk@27570 -
This commit is contained in:
parent
67e8a3bf54
commit
cd05deafc2
@ -40,6 +40,8 @@ Type
|
|||||||
// During read of content, of Server did not specify contentlength, -1 is passed.
|
// During read of content, of Server did not specify contentlength, -1 is passed.
|
||||||
// CurrentPos is reset to 0 when the actual content is read, i.e. it is the position in the data, discarding header size.
|
// CurrentPos is reset to 0 when the actual content is read, i.e. it is the position in the data, discarding header size.
|
||||||
TDataEvent = Procedure (Sender : TObject; Const ContentLength, CurrentPos : Int64) of object;
|
TDataEvent = Procedure (Sender : TObject; Const ContentLength, CurrentPos : Int64) of object;
|
||||||
|
// Use this to set up a socket handler. UseSSL is true if protocol was https
|
||||||
|
TGetSocketHandlerEvent = Procedure (Sender : TObject; Const UseSSL : Boolean; Out AHandler : TSocketHandler) of object;
|
||||||
|
|
||||||
{ TFPCustomHTTPClient }
|
{ TFPCustomHTTPClient }
|
||||||
TFPCustomHTTPClient = Class(TComponent)
|
TFPCustomHTTPClient = Class(TComponent)
|
||||||
@ -65,6 +67,7 @@ Type
|
|||||||
FSocket : TInetSocket;
|
FSocket : TInetSocket;
|
||||||
FBuffer : Ansistring;
|
FBuffer : Ansistring;
|
||||||
FUserName: String;
|
FUserName: String;
|
||||||
|
FOnGetSocketHandler : TGetSocketHandlerEvent;
|
||||||
function CheckContentLength: Int64;
|
function CheckContentLength: Int64;
|
||||||
function CheckTransferEncoding: string;
|
function CheckTransferEncoding: string;
|
||||||
function GetCookies: TStrings;
|
function GetCookies: TStrings;
|
||||||
@ -101,6 +104,8 @@ Type
|
|||||||
Procedure DoMethod(Const AMethod,AURL : String; Stream : TStream; Const AllowedResponseCodes : Array of Integer); virtual;
|
Procedure DoMethod(Const AMethod,AURL : String; Stream : TStream; Const AllowedResponseCodes : Array of Integer); virtual;
|
||||||
// Send request to server: construct request line and send headers and request body.
|
// Send request to server: construct request line and send headers and request body.
|
||||||
Procedure SendRequest(const AMethod: String; URI: TURI); virtual;
|
Procedure SendRequest(const AMethod: String; URI: TURI); virtual;
|
||||||
|
// Create socket handler for protocol AProtocol. Calls OnGetSocketHandler.
|
||||||
|
Function GetSocketHandler(Const UseSSL : Boolean) : TSocketHandler; virtual;
|
||||||
Public
|
Public
|
||||||
Constructor Create(AOwner: TComponent); override;
|
Constructor Create(AOwner: TComponent); override;
|
||||||
Destructor Destroy; override;
|
Destructor Destroy; override;
|
||||||
@ -239,6 +244,8 @@ Type
|
|||||||
Property OnDataReceived : TDataEvent Read FOnDataReceived Write FOnDataReceived;
|
Property OnDataReceived : TDataEvent Read FOnDataReceived Write FOnDataReceived;
|
||||||
// Called when headers have been processed.
|
// Called when headers have been processed.
|
||||||
Property OnHeaders : TNotifyEvent Read FOnHeaders Write FOnHeaders;
|
Property OnHeaders : TNotifyEvent Read FOnHeaders Write FOnHeaders;
|
||||||
|
// Called to create socket handler. If not set, or Nil is returned, a standard socket handler is created.
|
||||||
|
Property OnGetSocketHandler : TGetSocketHandlerEvent Read FOnGetSocketHandler Write FOnGetSocketHandler;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|
||||||
@ -411,6 +418,19 @@ begin
|
|||||||
Result:=Result+'?'+URI.Params;
|
Result:=Result+'?'+URI.Params;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
Function TFPCustomHTTPClient.GetSocketHandler(Const UseSSL : Boolean) : TSocketHandler;
|
||||||
|
|
||||||
|
begin
|
||||||
|
Result:=Nil;
|
||||||
|
if Assigned(FonGetSocketHandler) then
|
||||||
|
FOnGetSocketHandler(Self,UseSSL,Result);
|
||||||
|
if (Result=Nil) then
|
||||||
|
If UseSSL then
|
||||||
|
Result:=TSSLSocketHandler.Create
|
||||||
|
else
|
||||||
|
Result:=TSocketHandler.Create;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TFPCustomHTTPClient.ConnectToServer(const AHost: String;
|
procedure TFPCustomHTTPClient.ConnectToServer(const AHost: String;
|
||||||
APort: Integer; UseSSL : Boolean = False);
|
APort: Integer; UseSSL : Boolean = False);
|
||||||
|
|
||||||
@ -424,10 +444,7 @@ begin
|
|||||||
Aport:=443
|
Aport:=443
|
||||||
else
|
else
|
||||||
Aport:=80;
|
Aport:=80;
|
||||||
If UseSSL then
|
G:=GetSocketHandler(UseSSL);
|
||||||
G:=TSSLSocketHandler.Create
|
|
||||||
else
|
|
||||||
G:=TSocketHandler.Create;
|
|
||||||
FSocket:=TInetSocket.Create(AHost,APort,G);
|
FSocket:=TInetSocket.Create(AHost,APort,G);
|
||||||
FSocket.Connect;
|
FSocket.Connect;
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user