socket.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * base include file for SimpleTest
  4. * @package SimpleTest
  5. * @subpackage MockObjects
  6. * @version $Id$
  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. private $error = 'Constructor not chained';
  21. /**
  22. * Sets the error to empty.
  23. * @access public
  24. */
  25. function __construct() {
  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. * @package SimpleTest
  63. * @subpackage WebTester
  64. */
  65. class SimpleFileSocket extends SimpleStickyError {
  66. private $handle;
  67. private $is_open = false;
  68. private $sent = '';
  69. private $block_size;
  70. /**
  71. * Opens a socket for reading and writing.
  72. * @param SimpleUrl $file Target URI to fetch.
  73. * @param integer $block_size Size of chunk to read.
  74. * @access public
  75. */
  76. function __construct($file, $block_size = 1024) {
  77. parent::__construct();
  78. if (! ($this->handle = $this->openFile($file, $error))) {
  79. $file_string = $file->asString();
  80. $this->setError("Cannot open [$file_string] with [$error]");
  81. return;
  82. }
  83. $this->is_open = true;
  84. $this->block_size = $block_size;
  85. }
  86. /**
  87. * Writes some data to the socket and saves alocal copy.
  88. * @param string $message String to send to socket.
  89. * @return boolean True if successful.
  90. * @access public
  91. */
  92. function write($message) {
  93. return true;
  94. }
  95. /**
  96. * Reads data from the socket. The error suppresion
  97. * is a workaround for PHP4 always throwing a warning
  98. * with a secure socket.
  99. * @return integer/boolean Incoming bytes. False
  100. * on error.
  101. * @access public
  102. */
  103. function read() {
  104. $raw = @fread($this->handle, $this->block_size);
  105. if ($raw === false) {
  106. $this->setError('Cannot read from socket');
  107. $this->close();
  108. }
  109. return $raw;
  110. }
  111. /**
  112. * Accessor for socket open state.
  113. * @return boolean True if open.
  114. * @access public
  115. */
  116. function isOpen() {
  117. return $this->is_open;
  118. }
  119. /**
  120. * Closes the socket preventing further reads.
  121. * Cannot be reopened once closed.
  122. * @return boolean True if successful.
  123. * @access public
  124. */
  125. function close() {
  126. if (!$this->is_open) return false;
  127. $this->is_open = false;
  128. return fclose($this->handle);
  129. }
  130. /**
  131. * Accessor for content so far.
  132. * @return string Bytes sent only.
  133. * @access public
  134. */
  135. function getSent() {
  136. return $this->sent;
  137. }
  138. /**
  139. * Actually opens the low level socket.
  140. * @param SimpleUrl $file SimpleUrl file target.
  141. * @param string $error Recipient of error message.
  142. * @param integer $timeout Maximum time to wait for connection.
  143. * @access protected
  144. */
  145. protected function openFile($file, &$error) {
  146. return @fopen($file->asString(), 'r');
  147. }
  148. }
  149. /**
  150. * Wrapper for TCP/IP socket.
  151. * @package SimpleTest
  152. * @subpackage WebTester
  153. */
  154. class SimpleSocket extends SimpleStickyError {
  155. private $handle;
  156. private $is_open = false;
  157. private $sent = '';
  158. private $lock_size;
  159. /**
  160. * Opens a socket for reading and writing.
  161. * @param string $host Hostname to send request to.
  162. * @param integer $port Port on remote machine to open.
  163. * @param integer $timeout Connection timeout in seconds.
  164. * @param integer $block_size Size of chunk to read.
  165. * @access public
  166. */
  167. function __construct($host, $port, $timeout, $block_size = 255) {
  168. parent::__construct();
  169. if (! ($this->handle = $this->openSocket($host, $port, $error_number, $error, $timeout))) {
  170. $this->setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
  171. return;
  172. }
  173. $this->is_open = true;
  174. $this->block_size = $block_size;
  175. SimpleTestCompatibility::setTimeout($this->handle, $timeout);
  176. }
  177. /**
  178. * Writes some data to the socket and saves alocal copy.
  179. * @param string $message String to send to socket.
  180. * @return boolean True if successful.
  181. * @access public
  182. */
  183. function write($message) {
  184. if ($this->isError() || ! $this->isOpen()) {
  185. return false;
  186. }
  187. $count = fwrite($this->handle, $message);
  188. if (! $count) {
  189. if ($count === false) {
  190. $this->setError('Cannot write to socket');
  191. $this->close();
  192. }
  193. return false;
  194. }
  195. fflush($this->handle);
  196. $this->sent .= $message;
  197. return true;
  198. }
  199. /**
  200. * Reads data from the socket. The error suppresion
  201. * is a workaround for PHP4 always throwing a warning
  202. * with a secure socket.
  203. * @return integer/boolean Incoming bytes. False
  204. * on error.
  205. * @access public
  206. */
  207. function read() {
  208. if ($this->isError() || ! $this->isOpen()) {
  209. return false;
  210. }
  211. $raw = @fread($this->handle, $this->block_size);
  212. if ($raw === false) {
  213. $this->setError('Cannot read from socket');
  214. $this->close();
  215. }
  216. return $raw;
  217. }
  218. /**
  219. * Accessor for socket open state.
  220. * @return boolean True if open.
  221. * @access public
  222. */
  223. function isOpen() {
  224. return $this->is_open;
  225. }
  226. /**
  227. * Closes the socket preventing further reads.
  228. * Cannot be reopened once closed.
  229. * @return boolean True if successful.
  230. * @access public
  231. */
  232. function close() {
  233. $this->is_open = false;
  234. return fclose($this->handle);
  235. }
  236. /**
  237. * Accessor for content so far.
  238. * @return string Bytes sent only.
  239. * @access public
  240. */
  241. function getSent() {
  242. return $this->sent;
  243. }
  244. /**
  245. * Actually opens the low level socket.
  246. * @param string $host Host to connect to.
  247. * @param integer $port Port on host.
  248. * @param integer $error_number Recipient of error code.
  249. * @param string $error Recipoent of error message.
  250. * @param integer $timeout Maximum time to wait for connection.
  251. * @access protected
  252. */
  253. protected function openSocket($host, $port, &$error_number, &$error, $timeout) {
  254. return @fsockopen($host, $port, $error_number, $error, $timeout);
  255. }
  256. }
  257. /**
  258. * Wrapper for TCP/IP socket over TLS.
  259. * @package SimpleTest
  260. * @subpackage WebTester
  261. */
  262. class SimpleSecureSocket extends SimpleSocket {
  263. /**
  264. * Opens a secure socket for reading and writing.
  265. * @param string $host Hostname to send request to.
  266. * @param integer $port Port on remote machine to open.
  267. * @param integer $timeout Connection timeout in seconds.
  268. * @access public
  269. */
  270. function __construct($host, $port, $timeout) {
  271. parent::__construct($host, $port, $timeout);
  272. }
  273. /**
  274. * Actually opens the low level socket.
  275. * @param string $host Host to connect to.
  276. * @param integer $port Port on host.
  277. * @param integer $error_number Recipient of error code.
  278. * @param string $error Recipient of error message.
  279. * @param integer $timeout Maximum time to wait for connection.
  280. * @access protected
  281. */
  282. function openSocket($host, $port, &$error_number, &$error, $timeout) {
  283. return parent::openSocket("tls://$host", $port, $error_number, $error, $timeout);
  284. }
  285. }
  286. ?>