Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libCURL timeout while receiving HTTP multi-part flow

I'm using libCURL to perform an HTTP GET request toward a device that responds with a continuous flow of data in a multipart HTTP response.

I'd like to handle the unfortunate but possible case where the device is disconnected/shutdown or is not reachable anymore on the network.
By default libCURL does not have a few seconds timeout as I need, so I tried:

  1. setting the CURLOPT_CONNECTTIMEOUT options,
    but this only works at connection stage, not while already receiving data.

  2. setting the CURLOPT_TIMEOUT option,
    but this seems to always force a timeout even when data is still received.

My question is: how can I properly handle a timeout with libCURL, in the case described above?

like image 535
ErniBrown Avatar asked Jul 12 '26 16:07

ErniBrown


1 Answers

For your scenario instead of

curl_easy_setopt(curl, CURLOPT_TIMEOUT, <your timeout in seconds>);

use

curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, <your timeout in seconds>);

The above two lines make sure that if the average speed drops below 1 byte per second, in a time frame of X seconds, then the operation is aborted (timeout).

See reference here.

like image 69
roalz Avatar answered Jul 14 '26 08:07

roalz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!