diff options
author | Andreas Gohr <andi@splitbrain.org> | 2007-09-06 21:44:35 +0200 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2007-09-06 21:44:35 +0200 |
commit | 32b1888b993690049f99bf8c1dff51c81c974370 (patch) | |
tree | bda676cec8dd80701a44354bc18d2a06c16acf71 | |
parent | cc670e1ba1a9ec3870e091734458b22f4a63cf56 (diff) | |
download | rpg-32b1888b993690049f99bf8c1dff51c81c974370.tar.gz rpg-32b1888b993690049f99bf8c1dff51c81c974370.tar.bz2 |
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
-rw-r--r-- | inc/HTTPClient.php | 28 |
1 files 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 <andi@splitbrain.org> */ 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 '<b>'.$info.'</b><br />'; + print '<b>'.$info.'</b> '.($this->_time() - $this->start).'s<br />'; ob_start(); print_r($var); $content = htmlspecialchars(ob_get_contents()); @@ -361,6 +369,14 @@ class HTTPClient { } /** + * 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 * * All Keys are lowercased. |