diff options
author | Andreas Gohr <andi@splitbrain.org> | 2011-02-06 15:18:16 +0100 |
---|---|---|
committer | Andreas Gohr <andi@splitbrain.org> | 2011-02-06 15:21:19 +0100 |
commit | dd47a3146c007e5609ac18a5d6db1f5271d1273a (patch) | |
tree | c02bd2e05fc3a3200fd0566e359e8d3e461643cf | |
parent | ffb291f214dd47aa34d4e84b166de6e62714307f (diff) | |
download | rpg-dd47a3146c007e5609ac18a5d6db1f5271d1273a.tar.gz rpg-dd47a3146c007e5609ac18a5d6db1f5271d1273a.tar.bz2 |
better stream writing in HTTPClient FS#2036
This changes the HTTP stream to blocking while writing to the stream
using select() to handle timeouts. Addtionally, wwriting is done in 4k
block now (as it is done with reading).
This is supposed to fix a problem with writing to a SSL stream that is
not quite ready.
Reading from the stream continues to be non-blocking as before.
-rw-r--r-- | inc/HTTPClient.php | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php index 1cb16714d..372769b71 100644 --- a/inc/HTTPClient.php +++ b/inc/HTTPClient.php @@ -299,8 +299,6 @@ class HTTPClient { $this->error = "Could not connect to $server:$port\n$errstr ($errno)"; return false; } - //set non blocking - stream_set_blocking($socket,0); // keep alive? if ($this->keep_alive) { @@ -310,6 +308,9 @@ class HTTPClient { } } + //set blocking + stream_set_blocking($socket,1); + // build request $request = "$method $request_url HTTP/".$this->http.HTTP_NL; $request .= $this->_buildHeaders($headers); @@ -319,11 +320,28 @@ class HTTPClient { $this->_debug('request',$request); + // select parameters + $sel_r = null; + $sel_w = array($socket); + $sel_e = null; + // send request $towrite = strlen($request); $written = 0; while($written < $towrite){ - $ret = fwrite($socket, substr($request,$written)); + // check timeout + if(time()-$start > $this->timeout){ + $this->status = -100; + $this->error = sprintf('Timeout while sending request (%.3fs)',$this->_time() - $this->start); + unset($this->connections[$connectionId]); + return false; + } + + // wait for stream ready or timeout (1sec) + if(stream_select($sel_r,$sel_w,$sel_e,1) === false) continue; + + // write to stream + $ret = fwrite($socket, substr($request,$written,4096)); if($ret === false){ $this->status = -100; $this->error = 'Failed writing to socket'; @@ -333,6 +351,9 @@ class HTTPClient { $written += $ret; } + // continue non-blocking + stream_set_blocking($socket,0); + // read headers from socket $r_headers = ''; do{ |