3 * base include file for SimpleTest
5 * @subpackage MockObjects
6 * @version $Id: socket.php 1953 2009-09-20 01:26:25Z jsweat $
10 * include SimpleTest files
12 require_once(dirname(__FILE__
) . '/compatibility.php');
16 * Stashes an error for later. Useful for constructors
17 * until PHP gets exceptions.
19 * @subpackage WebTester
21 class SimpleStickyError
{
22 private $error = 'Constructor not chained';
25 * Sets the error to empty.
28 function __construct() {
33 * Test for an outstanding error.
34 * @return boolean True if there is an error.
38 return ($this->error
!= '');
42 * Accessor for an outstanding error.
43 * @return string Empty string if no error otherwise
52 * Sets the internal error.
53 * @param string Error message to stash.
56 function setError($error) {
57 $this->error
= $error;
61 * Resets the error state to no error.
64 function clearError() {
71 * @subpackage WebTester
73 class SimpleFileSocket
extends SimpleStickyError
{
75 private $is_open = false;
80 * Opens a socket for reading and writing.
81 * @param SimpleUrl $file Target URI to fetch.
82 * @param integer $block_size Size of chunk to read.
85 function __construct($file, $block_size = 1024) {
86 parent
::__construct();
87 if (! ($this->handle
= $this->openFile($file, $error))) {
88 $file_string = $file->asString();
89 $this->setError("Cannot open [$file_string] with [$error]");
92 $this->is_open
= true;
93 $this->block_size
= $block_size;
97 * Writes some data to the socket and saves alocal copy.
98 * @param string $message String to send to socket.
99 * @return boolean True if successful.
102 function write($message) {
107 * Reads data from the socket. The error suppresion
108 * is a workaround for PHP4 always throwing a warning
109 * with a secure socket.
110 * @return integer/boolean Incoming bytes. False
115 $raw = @fread
($this->handle
, $this->block_size
);
116 if ($raw === false) {
117 $this->setError('Cannot read from socket');
124 * Accessor for socket open state.
125 * @return boolean True if open.
129 return $this->is_open
;
133 * Closes the socket preventing further reads.
134 * Cannot be reopened once closed.
135 * @return boolean True if successful.
139 if (!$this->is_open
) return false;
140 $this->is_open
= false;
141 return fclose($this->handle
);
145 * Accessor for content so far.
146 * @return string Bytes sent only.
154 * Actually opens the low level socket.
155 * @param SimpleUrl $file SimpleUrl file target.
156 * @param string $error Recipient of error message.
157 * @param integer $timeout Maximum time to wait for connection.
160 protected function openFile($file, &$error) {
161 return @fopen
($file->asString(), 'r');
166 * Wrapper for TCP/IP socket.
167 * @package SimpleTest
168 * @subpackage WebTester
170 class SimpleSocket
extends SimpleStickyError
{
172 private $is_open = false;
177 * Opens a socket for reading and writing.
178 * @param string $host Hostname to send request to.
179 * @param integer $port Port on remote machine to open.
180 * @param integer $timeout Connection timeout in seconds.
181 * @param integer $block_size Size of chunk to read.
184 function __construct($host, $port, $timeout, $block_size = 255) {
185 parent
::__construct();
186 if (! ($this->handle
= $this->openSocket($host, $port, $error_number, $error, $timeout))) {
187 $this->setError("Cannot open [$host:$port] with [$error] within [$timeout] seconds");
190 $this->is_open
= true;
191 $this->block_size
= $block_size;
192 SimpleTestCompatibility
::setTimeout($this->handle
, $timeout);
196 * Writes some data to the socket and saves alocal copy.
197 * @param string $message String to send to socket.
198 * @return boolean True if successful.
201 function write($message) {
202 if ($this->isError() ||
! $this->isOpen()) {
205 $count = fwrite($this->handle
, $message);
207 if ($count === false) {
208 $this->setError('Cannot write to socket');
213 fflush($this->handle
);
214 $this->sent
.= $message;
219 * Reads data from the socket. The error suppresion
220 * is a workaround for PHP4 always throwing a warning
221 * with a secure socket.
222 * @return integer/boolean Incoming bytes. False
227 if ($this->isError() ||
! $this->isOpen()) {
230 $raw = @fread
($this->handle
, $this->block_size
);
231 if ($raw === false) {
232 $this->setError('Cannot read from socket');
239 * Accessor for socket open state.
240 * @return boolean True if open.
244 return $this->is_open
;
248 * Closes the socket preventing further reads.
249 * Cannot be reopened once closed.
250 * @return boolean True if successful.
254 $this->is_open
= false;
255 return fclose($this->handle
);
259 * Accessor for content so far.
260 * @return string Bytes sent only.
268 * Actually opens the low level socket.
269 * @param string $host Host to connect to.
270 * @param integer $port Port on host.
271 * @param integer $error_number Recipient of error code.
272 * @param string $error Recipoent of error message.
273 * @param integer $timeout Maximum time to wait for connection.
276 protected function openSocket($host, $port, &$error_number, &$error, $timeout) {
277 return @fsockopen
($host, $port, $error_number, $error, $timeout);
282 * Wrapper for TCP/IP socket over TLS.
283 * @package SimpleTest
284 * @subpackage WebTester
286 class SimpleSecureSocket
extends SimpleSocket
{
289 * Opens a secure socket for reading and writing.
290 * @param string $host Hostname to send request to.
291 * @param integer $port Port on remote machine to open.
292 * @param integer $timeout Connection timeout in seconds.
295 function __construct($host, $port, $timeout) {
296 parent
::__construct($host, $port, $timeout);
300 * Actually opens the low level socket.
301 * @param string $host Host to connect to.
302 * @param integer $port Port on host.
303 * @param integer $error_number Recipient of error code.
304 * @param string $error Recipient of error message.
305 * @param integer $timeout Maximum time to wait for connection.
308 function openSocket($host, $port, &$error_number, &$error, $timeout) {
309 return parent
::openSocket("tls://$host", $port, $error_number, $error, $timeout);