Skip to content
Snippets Groups Projects
Commit bfd975d2 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

fix regression bug in HTTPClient FS#2621

In the recent refactoring of the HTTPClient, a problem with certain
systems was reintroduced. On these systems a select() call always
waits for a timeout on the first call before working properly on the
second call.

This patch reintroduces the shorter timeouts with usleep rate limiting
again.

Since this bug is not reproducible on other systems it can't be unit
tested unfortunately.
parent 6e197037
No related branches found
No related tags found
No related merge requests found
...@@ -509,15 +509,17 @@ class HTTPClient { ...@@ -509,15 +509,17 @@ class HTTPClient {
if(feof($socket)) if(feof($socket))
throw new HTTPClientException("Socket disconnected while writing $message"); throw new HTTPClientException("Socket disconnected while writing $message");
// wait for stream ready or timeout // wait for stream ready or timeout (1sec)
self::selecttimeout($this->timeout - $time_used, $sec, $usec); if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ usleep(1000);
// write to stream continue;
$nbytes = fwrite($socket, substr($data,$written,4096));
if($nbytes === false)
throw new HTTPClientException("Failed writing to socket while sending $message", -100);
$written += $nbytes;
} }
// write to stream
$nbytes = fwrite($socket, substr($data,$written,4096));
if($nbytes === false)
throw new HTTPClientException("Failed writing to socket while sending $message", -100);
$written += $nbytes;
} }
} }
...@@ -556,15 +558,17 @@ class HTTPClient { ...@@ -556,15 +558,17 @@ class HTTPClient {
} }
if ($to_read > 0) { if ($to_read > 0) {
// wait for stream ready or timeout // wait for stream ready or timeout (1sec)
self::selecttimeout($this->timeout - $time_used, $sec, $usec); if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ usleep(1000);
$bytes = fread($socket, $to_read); continue;
if($bytes === false)
throw new HTTPClientException("Failed reading from socket while reading $message", -100);
$r_data .= $bytes;
$to_read -= strlen($bytes);
} }
$bytes = fread($socket, $to_read);
if($bytes === false)
throw new HTTPClientException("Failed reading from socket while reading $message", -100);
$r_data .= $bytes;
$to_read -= strlen($bytes);
} }
} while ($to_read > 0 && strlen($r_data) < $nbytes); } while ($to_read > 0 && strlen($r_data) < $nbytes);
return $r_data; return $r_data;
...@@ -595,11 +599,13 @@ class HTTPClient { ...@@ -595,11 +599,13 @@ class HTTPClient {
if(feof($socket)) if(feof($socket))
throw new HTTPClientException("Premature End of File (socket) while reading $message"); throw new HTTPClientException("Premature End of File (socket) while reading $message");
// wait for stream ready or timeout // wait for stream ready or timeout (1sec)
self::selecttimeout($this->timeout - $time_used, $sec, $usec); if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
if(@stream_select($sel_r, $sel_w, $sel_e, $sec, $usec) !== false){ usleep(1000);
$r_data = fgets($socket, 1024); continue;
} }
$r_data = fgets($socket, 1024);
} while (!preg_match('/\n$/',$r_data)); } while (!preg_match('/\n$/',$r_data));
return $r_data; return $r_data;
} }
...@@ -629,14 +635,6 @@ class HTTPClient { ...@@ -629,14 +635,6 @@ class HTTPClient {
return ((float)$usec + (float)$sec); return ((float)$usec + (float)$sec);
} }
/**
* Calculate seconds and microseconds
*/
static function selecttimeout($time, &$sec, &$usec){
$sec = floor($time);
$usec = (int)(($time - $sec) * 1000000);
}
/** /**
* convert given header string to Header array * convert given header string to Header array
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment