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