summaryrefslogtreecommitdiff
path: root/inc/HTTPClient.php
diff options
context:
space:
mode:
authorTobias Sarnowski <sarnowski@new-thoughts.org>2011-01-11 15:07:46 +0100
committerAndreas Gohr <andi@splitbrain.org>2011-01-12 21:00:46 +0100
commita6bacf701037fe4798658f00854ec9c5e6ddf835 (patch)
tree85df9fa45138d970485d65c0fd30e5a351ff5565 /inc/HTTPClient.php
parent1415e8b5968a264b9a2aa5bac0d77c2a898c8e81 (diff)
downloadrpg-a6bacf701037fe4798658f00854ec9c5e6ddf835.tar.gz
rpg-a6bacf701037fe4798658f00854ec9c5e6ddf835.tar.bz2
do not reuse errornous http connections
As soon as something goes wrong while querying a http server do not reuse the same connection again, its state is undefined. In addition, check the connection for feof() before reusing it.
Diffstat (limited to 'inc/HTTPClient.php')
-rw-r--r--inc/HTTPClient.php17
1 files changed, 16 insertions, 1 deletions
diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php
index 1dbcd6be1..53b344b76 100644
--- a/inc/HTTPClient.php
+++ b/inc/HTTPClient.php
@@ -285,10 +285,12 @@ class HTTPClient {
// already connected?
$connectionId = $this->_uniqueConnectionId($server,$port);
$this->_debug('connection pool', $this->connections);
+ $socket = null;
if (isset($this->connections[$connectionId])) {
$this->_debug('reusing connection', $connectionId);
$socket = $this->connections[$connectionId];
- } else {
+ }
+ if (is_null($socket) || feof($socket)) {
$this->_debug('opening connection', $connectionId);
// open socket
$socket = @fsockopen($server,$port,$errno, $errstr, $this->timeout);
@@ -303,6 +305,8 @@ class HTTPClient {
// keep alive?
if ($this->isKeepAlive()) {
$this->connections[$connectionId] = $socket;
+ } else {
+ unset($this->connections[$connectionId]);
}
}
@@ -323,6 +327,7 @@ class HTTPClient {
if($ret === false){
$this->status = -100;
$this->error = 'Failed writing to socket';
+ unset($this->connections[$connectionId]);
return false;
}
$written += $ret;
@@ -334,10 +339,12 @@ class HTTPClient {
if(time()-$start > $this->timeout){
$this->status = -100;
$this->error = sprintf('Timeout while reading headers (%.3fs)',$this->_time() - $this->start);
+ unset($this->connections[$connectionId]);
return false;
}
if(feof($socket)){
$this->error = 'Premature End of File (socket)';
+ unset($this->connections[$connectionId]);
return false;
}
$r_headers .= fgets($socket,1024);
@@ -350,6 +357,7 @@ class HTTPClient {
if($match[1] > $this->max_bodysize){
$this->error = 'Reported content length exceeds allowed response size';
if ($this->max_bodysize_abort)
+ unset($this->connections[$connectionId]);
return false;
}
}
@@ -357,6 +365,7 @@ class HTTPClient {
// get Status
if (!preg_match('/^HTTP\/(\d\.\d)\s*(\d+).*?\n/', $r_headers, $m)) {
$this->error = 'Server returned bad answer';
+ unset($this->connections[$connectionId]);
return false;
}
$this->status = $m[2];
@@ -414,6 +423,7 @@ class HTTPClient {
// check if headers are as expected
if($this->header_regexp && !preg_match($this->header_regexp,$r_headers)){
$this->error = 'The received headers did not match the given regexp';
+ unset($this->connections[$connectionId]);
return false;
}
@@ -425,11 +435,13 @@ class HTTPClient {
do {
if(feof($socket)){
$this->error = 'Premature End of File (socket)';
+ unset($this->connections[$connectionId]);
return false;
}
if(time()-$start > $this->timeout){
$this->status = -100;
$this->error = sprintf('Timeout while reading chunk (%.3fs)',$this->_time() - $this->start);
+ unset($this->connections[$connectionId]);
return false;
}
$byte = fread($socket,1);
@@ -447,6 +459,7 @@ class HTTPClient {
if($this->max_bodysize && strlen($r_body) > $this->max_bodysize){
$this->error = 'Allowed response size exceeded';
if ($this->max_bodysize_abort)
+ unset($this->connections[$connectionId]);
return false;
else
break;
@@ -458,6 +471,7 @@ class HTTPClient {
if(time()-$start > $this->timeout){
$this->status = -100;
$this->error = sprintf('Timeout while reading response (%.3fs)',$this->_time() - $this->start);
+ unset($this->connections[$connectionId]);
return false;
}
$r_body .= fread($socket,4096);
@@ -465,6 +479,7 @@ class HTTPClient {
if($this->max_bodysize && $r_size > $this->max_bodysize){
$this->error = 'Allowed response size exceeded';
if ($this->max_bodysize_abort)
+ unset($this->connections[$connectionId]);
return false;
else
break;