socket.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if (! ($this->_handle = $this->_openSocket($host, $port, $error_number, $error, $timeout))) {
  82. $this->_setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
  83. return;
  84. }
  85. $this->_is_open = true;
  86. $this->_block_size = $block_size;
  87. SimpleTestCompatibility::setTimeout($this->_handle, $timeout);
  88. }
  89. /**
  90. * Writes some data to the socket and saves alocal copy.
  91. * @param string $message String to send to socket.
  92. * @return boolean True if successful.
  93. * @access public
  94. */
  95. function write($message) {
  96. if ($this->isError() || ! $this->isOpen()) {
  97. return false;
  98. }
  99. $count = fwrite($this->_handle, $message);
  100. if (! $count) {
  101. if ($count === false) {
  102. $this->_setError('Cannot write to socket');
  103. $this->close();
  104. }
  105. return false;
  106. }
  107. fflush($this->_handle);
  108. $this->_sent .= $message;
  109. return true;
  110. }
  111. /**
  112. * Reads data from the socket. The error suppresion
  113. * is a workaround for PHP4 always throwing a warning
  114. * with a secure socket.
  115. * @return integer/boolean Incoming bytes. False
  116. * on error.
  117. * @access public
  118. */
  119. function read() {
  120. if ($this->isError() || ! $this->isOpen()) {
  121. return false;
  122. }
  123. $raw = @fread($this->_handle, $this->_block_size);
  124. if ($raw === false) {
  125. $this->_setError('Cannot read from socket');
  126. $this->close();
  127. }
  128. return $raw;
  129. }
  130. /**
  131. * Accessor for socket open state.
  132. * @return boolean True if open.
  133. * @access public
  134. */
  135. function isOpen() {
  136. return $this->_is_open;
  137. }
  138. /**
  139. * Closes the socket preventing further reads.
  140. * Cannot be reopened once closed.
  141. * @return boolean True if successful.
  142. * @access public
  143. */
  144. function close() {
  145. $this->_is_open = false;
  146. return fclose($this->_handle);
  147. }
  148. /**
  149. * Accessor for content so far.
  150. * @return string Bytes sent only.
  151. * @access public
  152. */
  153. function getSent() {
  154. return $this->_sent;
  155. }
  156. /**
  157. * Actually opens the low level socket.
  158. * @param string $host Host to connect to.
  159. * @param integer $port Port on host.
  160. * @param integer $error_number Recipient of error code.
  161. * @param string $error Recipoent of error message.
  162. * @param integer $timeout Maximum time to wait for connection.
  163. * @access protected
  164. */
  165. function _openSocket($host, $port, &$error_number, &$error, $timeout) {
  166. return @fsockopen($host, $port, $error_number, $error, $timeout);
  167. }
  168. }
  169. /**
  170. * Wrapper for TCP/IP socket over TLS.
  171. * @package SimpleTest
  172. * @subpackage WebTester
  173. */
  174. class SimpleSecureSocket extends SimpleSocket {
  175. /**
  176. * Opens a secure socket for reading and writing.
  177. * @param string $host Hostname to send request to.
  178. * @param integer $port Port on remote machine to open.
  179. * @param integer $timeout Connection timeout in seconds.
  180. * @access public
  181. */
  182. function SimpleSecureSocket($host, $port, $timeout) {
  183. $this->SimpleSocket($host, $port, $timeout);
  184. }
  185. /**
  186. * Actually opens the low level socket.
  187. * @param string $host Host to connect to.
  188. * @param integer $port Port on host.
  189. * @param integer $error_number Recipient of error code.
  190. * @param string $error Recipient of error message.
  191. * @param integer $timeout Maximum time to wait for connection.
  192. * @access protected
  193. */
  194. function _openSocket($host, $port, &$error_number, &$error, $timeout) {
  195. return parent::_openSocket("tls://$host", $port, $error_number, $error, $timeout);
  196. }
  197. }
  198. ?>