libcurl misses end of request data before reading response (patch)
Date: Thu, 04 Jun 2015 15:49:25 +0200
I experienced a problem in conjunction with HTTP Post requests and
keep-alive connections when the request body is being sent using the
The behaviour was that the keep-alive connection was droped by libcurl
after a POST request that was answered by a 4xx error response from the
server. The curl debug message read "HTTP error before end of send, stop
sending" which suggests, that the reply from the server was sent before
curl was aware the "request sending" was finished.
This is indeed the case: Curl detects that all request data has been
sent when the READFUNCTION returns '0', but this happens only after curl
tries to read the response from the server, which is already available,
since from the servers perspective the request is complete at this point.
Now this only happens if the Request is being sent with a content-length
header, meaning not a chunked.
Since the length of the request body is known, it could be set with
CURLOPT_INFILESIZE, in which case curl knows when the the "request
sending" is finished and there is no need to call the READFUNCTION again.
While this works as expected. Strangly this does not work when
CURLOPT_POSTFIELDSIZE is used instead.
So I'd propose 2 patches:
1) also respect CURLOPT_POSTFIELDSIZE as an information for the size of
the request body, and therefore improve the detection when "request
sending" is finished.
2) While uploading, call the READFUNCTION twice before trying to receive
the response. So it's possible to signal the end of the request from
inside the READFUNCTION (this might be a bit hacky and someone comes up
with a better idea)
- text/x-patch attachment: doubleCheckEof.patch
- text/x-patch attachment: postfieldsizeToInfilesize.patch