summaryrefslogtreecommitdiff
path: root/inc/HTTPClient.php
diff options
context:
space:
mode:
authorAndreas Gohr <andi@splitbrain.org>2005-10-02 14:54:21 +0200
committerAndreas Gohr <andi@splitbrain.org>2005-10-02 14:54:21 +0200
commit6e026dfa35b6d1e5beded6604491c9a8a9093f98 (patch)
tree5556b2eaf685885c10efdf8e9721397ed7b63efb /inc/HTTPClient.php
parent90033e9dea3c273570fba8855009d0b9bf550393 (diff)
downloadrpg-6e026dfa35b6d1e5beded6604491c9a8a9093f98.tar.gz
rpg-6e026dfa35b6d1e5beded6604491c9a8a9093f98.tar.bz2
HTTP Client tweaks
darcs-hash:20051002125421-7ad00-6fafa7fce8fef4954f1e65e13a2bc095734715f5.gz
Diffstat (limited to 'inc/HTTPClient.php')
-rw-r--r--inc/HTTPClient.php39
1 files changed, 30 insertions, 9 deletions
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';