From 6e026dfa35b6d1e5beded6604491c9a8a9093f98 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 2 Oct 2005 14:54:21 +0200 Subject: HTTP Client tweaks darcs-hash:20051002125421-7ad00-6fafa7fce8fef4954f1e65e13a2bc095734715f5.gz --- inc/HTTPClient.php | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'inc/HTTPClient.php') diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php index 53bd941cd..326bf2692 100644 --- a/inc/HTTPClient.php +++ b/inc/HTTPClient.php @@ -54,7 +54,7 @@ class HTTPClient { //set these if you like var $agent; // User agent var $http; // HTTP version defaults to 1.0 - var $timeout; + var $timeout; // read timeout (seconds) var $cookies; var $referer; var $max_redirect; @@ -185,13 +185,18 @@ class HTTPClient { $headers['Proxy-Authorization'] = 'BASIC '.base64_encode($this->proxy_user.':'.$this->proxy_pass); } + // stop time + $start = time(); + // open socket $socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout); if (!$socket){ $resp->status = '-100'; $this->error = "Could not connect to $server:$port\n$errstr ($errno)"; - return $false; + return false; } + //set non blocking + stream_set_blocking($socket,0); // build request $request = "$method $request_url HTTP/".$this->http.HTTP_NL; @@ -204,10 +209,18 @@ class HTTPClient { // send request fputs($socket, $request); - // read headers from socket $r_headers = ''; do{ + if(time()-$start > $this->timeout){ + $this->status = -100; + $this->error = 'Timeout while reading headers'; + return false; + } + if(feof($socket)){ + $this->error = 'Premature End of File (socket)'; + return false; + } $r_headers .= fread($socket,1); #FIXME read full lines here? }while(!preg_match('/\r\n\r\n$/',$r_headers)); @@ -217,6 +230,15 @@ class HTTPClient { do { unset($chunk_size); do { + if(feof($socket)){ + $this->error = 'Premature End of File (socket)'; + return false; + } + if(time()-$start > $this->timeout){ + $this->status = -100; + $this->error = 'Timeout while reading chunk'; + return false; + } $byte = fread($socket,1); $chunk_size .= $byte; } while (preg_match('/[a-zA-Z0-9]/',$byte)); // read chunksize including \r @@ -235,6 +257,11 @@ class HTTPClient { }else{ // read entire socket while (!feof($socket)) { + if(time()-$start > $this->timeout){ + $this->status = -100; + $this->error = 'Timeout while reading response'; + return false; + } $r_body .= fread($socket,4096); if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){ $this->error = 'Allowed response size exceeded'; @@ -249,12 +276,6 @@ class HTTPClient { $this->_debug('response headers',$r_headers); - // check for timeout - if ($status['timed_out']){ - $this->error = "Connection timed out"; - return false; - } - // get Status if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) { $this->error = 'Server returned bad answer'; -- cgit v1.2.3