<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2014-04-25 10:24 GMT-03:00 Michael Van Canneyt <span dir="ltr"><<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>></span>:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><div class="h5">
On Fri, 25 Apr 2014, silvioprog wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
2014-04-25 3:39 GMT-03:00 Michael Van Canneyt <<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>>:<br>
[...] <br>
Damn windows returning 0 on read... Can you please test with the following change:<br>
<br>
Procedure FillBuffer;<br>
<br>
Var<br>
R : Integer;<br>
<br>
begin<br>
SetLength(FBuffer,ReadBufLen);<br>
Repeat<br>
r:=FSocket.Read(FBuffer[1],<u></u>ReadBufLen);<br>
If r<0 then<br>
Raise EHTTPServer.Create(<u></u>SErrReadingSocket);<br>
if R=0 then<br>
Sleep(10);<br>
Until (R<>0);<br>
if (R<ReadBuflen) then<br>
SetLength(FBuffer,r);<br>
end;<br>
<br>
<br>
After apply this change, now when read returns 0, it freezes the app., because the "sleep(10)" command is infinitely called. :/<br>
</blockquote>
<br></div></div>
OK, what we can do is add a timeout:<br>
<br>
Procedure FillBuffer;<br>
<br>
Const<br>
MaxReadRetries = 150;<br>
<br>
Var<br>
R,C : Integer;<br>
<br>
begin<br>
C:=0;<div class=""><br>
SetLength(FBuffer,ReadBufLen);<br>
Repeat<br>
r:=FSocket.Read(FBuffer[1],<u></u>ReadBufLen);<br>
If r<0 then<br>
Raise EHTTPServer.Create(<u></u>SErrReadingSocket);<br>
if R=0 then<br></div>
begin<br>
Sleep(10);<br>
Inc(C);<br>
end;<br>
Until (R<>0) or (C>MaxReadRetries);<div class=""><br>
if (R<ReadBuflen) then<br>
SetLength(FBuffer,r);<br>
end;<br>
<br></div>
It will then try 150 times, and then error out.<br>
<br>
BUT, reading this:<br>
<br>
<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121%28v=vs.85%29.aspx" target="_blank">http://msdn.microsoft.com/en-<u></u>us/library/windows/desktop/<u></u>ms740121%28v=vs.85%29.aspx</a><br>
<br>
"If the connection has been gracefully closed, the return value is zero."<br>
<br>
So, this means then thay the original implementation is correct. You just need to catch the error using the OnRequestError handler...</blockquote></div><div><br></div><div>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?<br>
</div><div><br></div><div>In original implementation, I made a ''quick fix" to solve my problem, please see the "ADDED" lines:</div><div><br></div><div>[code]</div><div>...</div><div><div> TFPHTTPConnection = Class(TObject)</div>
<div> private</div><div>{$IFDEF MSWINDOWS} << ADDED</div><div> FCanHandleRequest: Boolean; << ADDED</div><div>{$ENDIF} << ADDED</div></div><div>...</div><div>[/code]</div><div><br></div><div>[code]</div>
<div>...</div><div><div>function TFPHTTPConnection.ReadRequestHeaders: TFPHTTPConnectionRequest;</div><div><br></div><div>Var</div><div> StartLine,S : String;</div><div>begin</div><div> Result:=Server.CreateRequest;</div>
<div> try</div><div> Server.InitRequest(Result);</div><div> Result.FConnection:=Self;</div><div> StartLine:=ReadString;</div><div>{$IFDEF MSWINDOWS}</div><div> FCanHandleRequest := (StartLine <> '');</div>
<div> If not FCanHandleRequest Then</div><div> Exit;</div><div>{$ENDIF}</div></div><div>...</div><div>[/code]</div><div><br></div><div>[code]</div><div>...</div><div><div>procedure TFPHTTPConnection.HandleRequest;</div>
<div><br></div><div>Var</div><div> Req : TFPHTTPConnectionRequest;</div><div> Resp : TFPHTTPConnectionResponse;</div><div><br></div><div>begin</div><div> Try</div><div> SetupSocket;</div><div> // Read headers.</div>
<div> Req:=ReadRequestHeaders;</div><div> try</div><div>{$IFDEF MSWINDOWS} << ADDED</div><div> if not FCanHandleRequest then << ADDED</div><div> Exit; << ADDED</div><div>{$ENDIF} << ADDED</div>
</div><div>...</div><div>[/code]</div><div><br></div><div>-- <br></div>Silvio Clécio<br>My public projects - <a href="http://github.com/silvioprog" target="_blank">github.com/silvioprog</a>
</div></div>