From 32b1888b993690049f99bf8c1dff51c81c974370 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 6 Sep 2007 21:44:35 +0200 Subject: Use Content-Length in HTTP client If a Server supplies a Content-Length header we stop reading when the specified number of bytes was read. This fixes problems with Servers not closing the connection after sending the body. darcs-hash:20070906194435-7ad00-cfb9b77ee085d28f5a643e45750b0a7be8ad7bd7.gz --- inc/HTTPClient.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php index 6c8de7328..795c12581 100644 --- a/inc/HTTPClient.php +++ b/inc/HTTPClient.php @@ -62,6 +62,7 @@ class HTTPClient { var $header_regexp; // if set this RE must match against the headers, else abort var $headers; var $debug; + var $start = 0; // for timings // don't set these, read on error var $error; @@ -144,7 +145,8 @@ class HTTPClient { * @author Andreas Gohr */ function sendRequest($url,$data=array(),$method='GET'){ - $this->error = ''; + $this->start = $this->_time(); + $this->error = ''; $this->status = 0; // parse URL into bits @@ -219,7 +221,7 @@ class HTTPClient { do{ if(time()-$start > $this->timeout){ $this->status = -100; - $this->error = 'Timeout while reading headers'; + $this->error = sprintf('Timeout while reading headers (%.3fs)',$this->_time() - $this->start); return false; } if(feof($socket)){ @@ -295,7 +297,7 @@ class HTTPClient { } if(time()-$start > $this->timeout){ $this->status = -100; - $this->error = 'Timeout while reading chunk'; + $this->error = sprintf('Timeout while reading chunk (%.3fs)',$this->_time() - $this->start); return false; } $byte = fread($socket,1); @@ -318,14 +320,20 @@ class HTTPClient { while (!feof($socket)) { if(time()-$start > $this->timeout){ $this->status = -100; - $this->error = 'Timeout while reading response'; + $this->error = sprintf('Timeout while reading response (%.3fs)',$this->_time() - $this->start); return false; } $r_body .= fread($socket,4096); - if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){ + $r_size = strlen($r_body); + if($this->max_bodysize && $r_size > $this->max_bodysize){ $this->error = 'Allowed response size exceeded'; return false; } + if($this->resp_headers['content-length'] && !$this->resp_headers['transfer-encoding'] && + $this->resp_headers['content-length'] == $r_size){ + // we read the content-length, finish here + break; + } } } @@ -352,7 +360,7 @@ class HTTPClient { */ function _debug($info,$var){ if(!$this->debug) return; - print ''.$info.'
'; + print ''.$info.' '.($this->_time() - $this->start).'s
'; ob_start(); print_r($var); $content = htmlspecialchars(ob_get_contents()); @@ -360,6 +368,14 @@ class HTTPClient { print '
'.$content.'
'; } + /** + * Return current timestamp in microsecond resolution + */ + function _time(){ + list($usec, $sec) = explode(" ", microtime()); + return ((float)$usec + (float)$sec); + } + /** * convert given header string to Header array * -- cgit v1.2.3