mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-08-29 20:40:24 +02:00
fphttpclient keepalive: failed socket operations raise exceptions - try to reconnect also in this case
This commit is contained in:
parent
246bff92d2
commit
c64c0e617d
@ -378,6 +378,12 @@ Type
|
|||||||
end;
|
end;
|
||||||
|
|
||||||
EHTTPClient = Class(EHTTP);
|
EHTTPClient = Class(EHTTP);
|
||||||
|
// socket stream exceptions
|
||||||
|
EHTTPClientStream = class(EHTTPClient);
|
||||||
|
// reading from socket
|
||||||
|
EHTTPClientStreamRead = Class(EHTTPClientStream);
|
||||||
|
// writing to socket
|
||||||
|
EHTTPClientStreamWrite = Class(EHTTPClientStream);
|
||||||
|
|
||||||
Function EncodeURLElement(S : String) : String;
|
Function EncodeURLElement(S : String) : String;
|
||||||
Function DecodeURLElement(Const S : String) : String;
|
Function DecodeURLElement(Const S : String) : String;
|
||||||
@ -387,6 +393,7 @@ implementation
|
|||||||
resourcestring
|
resourcestring
|
||||||
SErrInvalidProtocol = 'Invalid protocol : "%s"';
|
SErrInvalidProtocol = 'Invalid protocol : "%s"';
|
||||||
SErrReadingSocket = 'Error reading data from socket';
|
SErrReadingSocket = 'Error reading data from socket';
|
||||||
|
SErrWritingSocket = 'Error writing data to socket';
|
||||||
SErrInvalidProtocolVersion = 'Invalid protocol version in response: "%s"';
|
SErrInvalidProtocolVersion = 'Invalid protocol version in response: "%s"';
|
||||||
SErrInvalidStatusCode = 'Invalid response status code: %s';
|
SErrInvalidStatusCode = 'Invalid response status code: %s';
|
||||||
SErrUnexpectedResponse = 'Unexpected response status code: %d';
|
SErrUnexpectedResponse = 'Unexpected response status code: %d';
|
||||||
@ -736,10 +743,15 @@ begin
|
|||||||
FSentCookies:=FCookies;
|
FSentCookies:=FCookies;
|
||||||
FCookies:=Nil;
|
FCookies:=Nil;
|
||||||
S:=S+CRLF;
|
S:=S+CRLF;
|
||||||
if not Terminated then
|
try
|
||||||
FSocket.WriteBuffer(S[1],Length(S));
|
if not Terminated then
|
||||||
If Assigned(FRequestBody) and not Terminated then
|
FSocket.WriteBuffer(S[1],Length(S));
|
||||||
FSocket.CopyFrom(FRequestBody,FRequestBody.Size);
|
If Assigned(FRequestBody) and not Terminated then
|
||||||
|
FSocket.CopyFrom(FRequestBody,FRequestBody.Size);
|
||||||
|
except
|
||||||
|
on E: EWriteError do
|
||||||
|
raise EHTTPClientStreamWrite.Create(SErrWritingSocket);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TFPCustomHTTPClient.ReadString(out S: String): Boolean;
|
function TFPCustomHTTPClient.ReadString(out S: String): Boolean;
|
||||||
@ -757,7 +769,7 @@ function TFPCustomHTTPClient.ReadString(out S: String): Boolean;
|
|||||||
If (r=0) or Terminated Then
|
If (r=0) or Terminated Then
|
||||||
Exit(False);
|
Exit(False);
|
||||||
If (r<0) then
|
If (r<0) then
|
||||||
Raise EHTTPClient.Create(SErrReadingSocket);
|
Raise EHTTPClientStreamRead.Create(SErrReadingSocket);
|
||||||
if (r<ReadBuflen) then
|
if (r<ReadBuflen) then
|
||||||
SetLength(FBuffer,r);
|
SetLength(FBuffer,r);
|
||||||
FDataRead:=FDataRead+R;
|
FDataRead:=FDataRead+R;
|
||||||
@ -1031,7 +1043,7 @@ Function TFPCustomHTTPClient.ReadResponse(Stream: TStream;
|
|||||||
begin
|
begin
|
||||||
Result:=FSocket.Read(FBuffer[1],LB);
|
Result:=FSocket.Read(FBuffer[1],LB);
|
||||||
If Result<0 then
|
If Result<0 then
|
||||||
Raise EHTTPClient.Create(SErrReadingSocket);
|
Raise EHTTPClientStreamRead.Create(SErrReadingSocket);
|
||||||
if (Result>0) then
|
if (Result>0) then
|
||||||
begin
|
begin
|
||||||
FDataRead:=FDataRead+Result;
|
FDataRead:=FDataRead+Result;
|
||||||
@ -1065,7 +1077,7 @@ Function TFPCustomHTTPClient.ReadResponse(Stream: TStream;
|
|||||||
SetLength(FBuffer,ReadBuflen);
|
SetLength(FBuffer,ReadBuflen);
|
||||||
Cnt:=FSocket.Read(FBuffer[1],length(FBuffer));
|
Cnt:=FSocket.Read(FBuffer[1],length(FBuffer));
|
||||||
If Cnt<0 then
|
If Cnt<0 then
|
||||||
Raise EHTTPClient.Create(SErrReadingSocket);
|
Raise EHTTPClientStreamRead.Create(SErrReadingSocket);
|
||||||
SetLength(FBuffer,Cnt);
|
SetLength(FBuffer,Cnt);
|
||||||
BufPos:=1;
|
BufPos:=1;
|
||||||
Result:=Cnt>0;
|
Result:=Cnt>0;
|
||||||
@ -1280,14 +1292,23 @@ begin
|
|||||||
If Not IsConnected Then
|
If Not IsConnected Then
|
||||||
ConnectToServer(CHost,CPort,AIsHttps);
|
ConnectToServer(CHost,CPort,AIsHttps);
|
||||||
Try
|
Try
|
||||||
if not Terminated then
|
if Terminated then
|
||||||
|
break;
|
||||||
|
try
|
||||||
SendRequest(AMethod,AURI);
|
SendRequest(AMethod,AURI);
|
||||||
if not Terminated then
|
if Terminated then
|
||||||
begin
|
break;
|
||||||
T := ReadResponse(AStream,AAllowedResponseCodes,AHeadersOnly);
|
T := ReadResponse(AStream,AAllowedResponseCodes,AHeadersOnly);
|
||||||
If Not T Then
|
except
|
||||||
ReconnectToServer(CHost,CPort,AIsHttps);
|
on E: EHTTPClientStream do
|
||||||
|
begin
|
||||||
|
// failed socket stream operations raise exceptions - e.g. if ReadString() fails
|
||||||
|
// try to reconnect also in this case
|
||||||
|
T:=False;
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
|
If Not T Then
|
||||||
|
ReconnectToServer(CHost,CPort,AIsHttps);
|
||||||
Finally
|
Finally
|
||||||
// On terminate, we close the request
|
// On terminate, we close the request
|
||||||
If HasConnectionClose or Terminated Then
|
If HasConnectionClose or Terminated Then
|
||||||
|
Loading…
Reference in New Issue
Block a user