socket.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage MockObjects
  6. * @version $Id: socket.php 1723 2008-04-08 00:34:10Z lastcraft $
  7. */
  8. /**#@+
  9. * include SimpleTest files
  10. */
  11. require_once(dirname(__FILE__) . '/compatibility.php');
  12. /**#@-*/
  13. /**
  14. * Stashes an error for later. Useful for constructors
  15. * until PHP gets exceptions.
  16. * @package SimpleTest
  17. * @subpackage WebTester
  18. */
  19. class SimpleStickyError {
  20. var $_error = 'Constructor not chained';
  21. /**
  22. * Sets the error to empty.
  23. * @access public
  24. */
  25. function SimpleStickyError() {
  26. $this->_clearError();
  27. }
  28. /**
  29. * Test for an outstanding error.
  30. * @return boolean True if there is an error.
  31. * @access public
  32. */
  33. function isError() {
  34. return ($this->_error != '');
  35. }
  36. /**
  37. * Accessor for an outstanding error.
  38. * @return string Empty string if no error otherwise
  39. * the error message.
  40. * @access public
  41. */
  42. function getError() {
  43. return $this->_error;
  44. }
  45. /**
  46. * Sets the internal error.
  47. * @param string Error message to stash.
  48. * @access protected
  49. */
  50. function _setError($error) {
  51. $this->_error = $error;
  52. }
  53. /**
  54. * Resets the error state to no error.
  55. * @access protected
  56. */
  57. function _clearError() {
  58. $this->_setError('');
  59. }
  60. }
  61. /**
  62. * Wrapper for TCP/IP socket.
  63. * @package SimpleTest
  64. * @subpackage WebTester
  65. */
  66. class SimpleSocket extends SimpleStickyError {
  67. var $_handle;
  68. var $_is_open = false;
  69. var $_sent = '';
  70. var $lock_size;
  71. /**
  72. * Opens a socket for reading and writing.
  73. * @param string $host Hostname to send request to.
  74. * @param integer $port Port on remote machine to open.
  75. * @param integer $timeout Connection timeout in seconds.
  76. * @param integer $block_size Size of chunk to read.
  77. * @access public
  78. */
  79. function SimpleSocket($host, $port, $timeout, $block_size = 255) {
  80. $this->SimpleStickyError();
  81. $error_number = 0;
  82. $error = '';
  83. if (! ($this->_handle = $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
  84. $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
  85. return;
  86. }
  87. $this->_is_open = true;
  88. $this->_block_size = $block_size;
  89. SimpleTestCompatibility::setTimeout($this->_handle, $timeout);
  90. }
  91. /**
  92. * Writes some data to the socket and saves alocal copy.
  93. * @param string $message String to send to socket.
  94. * @return boolean True if successful.
  95. * @access public
  96. */
  97. function write($message) {
  98. if ($this->isError() || ! $this->isOpen()) {
  99. return false;
  100. }
  101. $count = fwrite($this->_handle, $message);
  102. if (! $count) {
  103. if ($count === false) {
  104. $this->_setError('Cannot write to socket');
  105. $this->close();
  106. }
  107. return false;
  108. }
  109. fflush($this->_handle);
  110. $this->_sent .= $message;
  111. return true;
  112. }
  113. /**
  114. * Reads data from the socket. The error suppresion
  115. * is a workaround for PHP4 always throwing a warning
  116. * with a secure socket.
  117. * @return integer/boolean Incoming bytes. False
  118. * on error.
  119. * @access public
  120. */
  121. function read() {
  122. if ($this->isError() || ! $this->isOpen()) {
  123. return false;
  124. }
  125. $raw = @fread($this->_handle, $this->_block_size);
  126. if ($raw === false) {
  127. $this->_setError('Cannot read from socket');
  128. $this->close();
  129. }
  130. return $raw;
  131. }
  132. /**
  133. * Accessor for socket open state.
  134. * @return boolean True if open.
  135. * @access public
  136. */
  137. function isOpen() {
  138. return $this->_is_open;
  139. }
  140. /**
  141. * Closes the socket preventing further reads.
  142. * Cannot be reopened once closed.
  143. * @return boolean True if successful.
  144. * @access public
  145. */
  146. function close() {
  147. $this->_is_open = false;
  148. return fclose($this->_handle);
  149. }
  150. /**
  151. * Accessor for content so far.
  152. * @return string Bytes sent only.
  153. * @access public
  154. */
  155. function getSent() {
  156. return $this->_sent;
  157. }
  158. /**
  159. * Actually opens the low level socket.
  160. * @param string $host Host to connect to.
  161. * @param integer $port Port on host.
  162. * @param integer $error_number Recipient of error code.
  163. * @param string $error Recipoent of error message.
  164. * @param integer $timeout Maximum time to wait for connection.
  165. * @access protected
  166. */
  167. function _openSocket($host, $port, &$error_number, &$error, $timeout) {
  168. return @fsockopen($host, $port, $error_number, $error, $timeout);
  169. }
  170. }
  171. /**
  172. * Wrapper for TCP/IP socket over TLS.
  173. * @package SimpleTest
  174. * @subpackage WebTester
  175. */
  176. class SimpleSecureSocket extends SimpleSocket {
  177. /**
  178. * Opens a secure socket for reading and writing.
  179. * @param string $host Hostname to send request to.
  180. * @param integer $port Port on remote machine to open.
  181. * @param integer $timeout Connection timeout in seconds.
  182. * @access public
  183. */
  184. function SimpleSecureSocket($host, $port, $timeout) {
  185. $this->SimpleSocket($host, $port, $timeout);
  186. }
  187. /**
  188. * Actually opens the low level socket.
  189. * @param string $host Host to connect to.
  190. * @param integer $port Port on host.
  191. * @param integer $error_number Recipient of error code.
  192. * @param string $error Recipient of error message.
  193. * @param integer $timeout Maximum time to wait for connection.
  194. * @access protected
  195. */
  196. function _openSocket($host, $port, &$error_number, &$error, $timeout) {
  197. return parent::_openSocket("tls://$host", $port, $error_number, $error, $timeout);
  198. }
  199. }
  200. ?>