summaryrefslogtreecommitdiff
path: root/inc/HTTPClient.php
diff options
context:
space:
mode:
authorTom N Harris <tnharris@whoopdedo.org>2012-06-19 22:40:39 -0400
committerTom N Harris <tnharris@whoopdedo.org>2012-06-19 22:40:39 -0400
commit50d1968dceb92179b9a581e34a84d516b81511ce (patch)
treee82200756e66063b966a3509243129baf86b8adf /inc/HTTPClient.php
parentd37a395509092f21060e2a042a2be0ea759f6d0b (diff)
downloadrpg-50d1968dceb92179b9a581e34a84d516b81511ce.tar.gz
rpg-50d1968dceb92179b9a581e34a84d516b81511ce.tar.bz2
Utility function for writing to a socket
Diffstat (limited to 'inc/HTTPClient.php')
-rw-r--r--inc/HTTPClient.php71
1 files changed, 39 insertions, 32 deletions
diff --git a/inc/HTTPClient.php b/inc/HTTPClient.php
index ab41257bd..ea9b42862 100644
--- a/inc/HTTPClient.php
+++ b/inc/HTTPClient.php
@@ -308,8 +308,8 @@ class HTTPClient {
}
try {
- //set blocking
- stream_set_blocking($socket,1);
+ //set non-blocking
+ stream_set_blocking($socket, false);
// build request
$request = "$method $request_url HTTP/".$this->http.HTTP_NL;
@@ -319,36 +319,7 @@ class HTTPClient {
$request .= $data;
$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){
- // check timeout
- $time_used = $this->_time() - $this->start;
- if($time_used > $this->timeout)
- throw new HTTPClientException(sprintf('Timeout while sending request (%.3fs)',$time_used), -100);
-
- // wait for stream ready or timeout (1sec)
- if(@stream_select($sel_r,$sel_w,$sel_e,1) === false){
- usleep(1000);
- continue;
- }
-
- // write to stream
- $ret = fwrite($socket, substr($request,$written,4096));
- if($ret === false)
- throw new HTTPClientException('Failed writing to socket', -100);
- $written += $ret;
- }
-
- // continue non-blocking
- stream_set_blocking($socket,0);
+ $this->_sendData($socket, $request, 'request');
// read headers from socket
$r_headers = '';
@@ -510,6 +481,42 @@ class HTTPClient {
}
/**
+ * Safely write data to a socket
+ *
+ * @param handle $socket An open socket handle
+ * @param string $data The data to write
+ * @param string $message Description of what is being read
+ * @author Tom N Harris <tnharris@whoopdedo.org>
+ */
+ function _sendData($socket, $data, $message) {
+ // select parameters
+ $sel_r = null;
+ $sel_w = array($socket);
+ $sel_e = null;
+
+ // send request
+ $towrite = strlen($data);
+ $written = 0;
+ while($written < $towrite){
+ // check timeout
+ $time_used = $this->_time() - $this->start;
+ if($time_used > $this->timeout)
+ throw new HTTPClientException(sprintf('Timeout while sending %s (%.3fs)',$message, $time_used), -100);
+ if(feof($socket))
+ throw new HTTPClientException("Socket disconnected while writing $message");
+
+ // wait for stream ready or timeout
+ if(@stream_select($sel_r, $sel_w, $sel_e, $this->timeout - $time_used) !== false){
+ // write to stream
+ $nbytes = fwrite($socket, substr($data,$written,4096));
+ if($nbytes === false)
+ throw new HTTPClientException("Failed writing to socket while sending $message", -100);
+ $written += $nbytes;
+ }
+ }
+ }
+
+ /**
* Safely read data from a socket
*
* Reads up to a given number of bytes or throws an exception if the