[fpc-pascal] CustHttpApp again, error: "Missing HTTP protocol version in request"

silvioprog silvioprog at gmail.com
Fri Apr 25 16:04:19 CEST 2014


2014-04-25 10:24 GMT-03:00 Michael Van Canneyt <michael at freepascal.org>:
>
>  On Fri, 25 Apr 2014, silvioprog wrote:
>
>> 2014-04-25 3:39 GMT-03:00 Michael Van Canneyt <michael at freepascal.org>:
>> [...]
>>       Damn windows returning 0 on read... Can you please test with the
>> following change:
>>
>>          Procedure FillBuffer;
>>
>>          Var
>>            R : Integer;
>>
>>          begin
>>            SetLength(FBuffer,ReadBufLen);
>>      Repeat
>>        r:=FSocket.Read(FBuffer[1],ReadBufLen);
>>        If r<0 then
>>          Raise EHTTPServer.Create(SErrReadingSocket);
>>        if R=0 then
>>          Sleep(10);
>>      Until (R<>0);
>>      if (R<ReadBuflen) then
>>        SetLength(FBuffer,r);
>>    end;
>>
>>
>> After apply this change, now when read returns 0, it freezes the app.,
>> because the "sleep(10)" command is infinitely called. :/
>>
>
> OK, what we can do is add a timeout:
>
> Procedure FillBuffer;
>
> Const
>   MaxReadRetries = 150;
>
> Var
>   R,C : Integer;
>
> begin
>   C:=0;
>
>   SetLength(FBuffer,ReadBufLen);
>   Repeat
>     r:=FSocket.Read(FBuffer[1],ReadBufLen);
>     If r<0 then
>       Raise EHTTPServer.Create(SErrReadingSocket);
>     if R=0 then
>       begin
>       Sleep(10);
>       Inc(C);
>       end;
>   Until (R<>0) or (C>MaxReadRetries);
>
>   if (R<ReadBuflen) then
>     SetLength(FBuffer,r);
> end;
>
> It will then try 150 times, and then error out.
>
> BUT, reading this:
>
> http://msdn.microsoft.com/en-us/library/windows/desktop/
> ms740121%28v=vs.85%29.aspx
>
> "If the connection has been gracefully closed, the return value is zero."
>
> So, this means then thay the original implementation is correct. You just
> need to catch the error using the OnRequestError handler...


Sorry for my ignorance, but, would not it be better to read the HTTP data
only if the client is still connected or if read bytes > 0?

In original implementation, I made a ''quick fix" to solve my problem,
please see the "ADDED" lines:

[code]
...
  TFPHTTPConnection = Class(TObject)
  private
{$IFDEF MSWINDOWS} << ADDED
    FCanHandleRequest: Boolean; << ADDED
{$ENDIF} << ADDED
...
[/code]

[code]
...
function TFPHTTPConnection.ReadRequestHeaders: TFPHTTPConnectionRequest;

Var
  StartLine,S : String;
begin
  Result:=Server.CreateRequest;
  try
    Server.InitRequest(Result);
    Result.FConnection:=Self;
    StartLine:=ReadString;
{$IFDEF MSWINDOWS}
    FCanHandleRequest := (StartLine <> '');
    If not FCanHandleRequest Then
      Exit;
{$ENDIF}
...
[/code]

[code]
...
procedure TFPHTTPConnection.HandleRequest;

Var
  Req : TFPHTTPConnectionRequest;
  Resp : TFPHTTPConnectionResponse;

begin
  Try
    SetupSocket;
    // Read headers.
    Req:=ReadRequestHeaders;
    try
{$IFDEF MSWINDOWS} << ADDED
      if not FCanHandleRequest then << ADDED
        Exit; << ADDED
{$ENDIF} << ADDED
...
[/code]

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20140425/22831074/attachment.html>


More information about the fpc-pascal mailing list