| 1 | <?php
|
|---|
| 2 | /**
|
|---|
| 3 | * Socket wrapper class used by Socket Adapter
|
|---|
| 4 | *
|
|---|
| 5 | * PHP version 5
|
|---|
| 6 | *
|
|---|
| 7 | * LICENSE:
|
|---|
| 8 | *
|
|---|
| 9 | * Copyright (c) 2008-2012, Alexey Borzov <[email protected]>
|
|---|
| 10 | * All rights reserved.
|
|---|
| 11 | *
|
|---|
| 12 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | * modification, are permitted provided that the following conditions
|
|---|
| 14 | * are met:
|
|---|
| 15 | *
|
|---|
| 16 | * * Redistributions of source code must retain the above copyright
|
|---|
| 17 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 18 | * * Redistributions in binary form must reproduce the above copyright
|
|---|
| 19 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 20 | * documentation and/or other materials provided with the distribution.
|
|---|
| 21 | * * The names of the authors may not be used to endorse or promote products
|
|---|
| 22 | * derived from this software without specific prior written permission.
|
|---|
| 23 | *
|
|---|
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|---|
| 25 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 26 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|---|
| 27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|---|
| 28 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|---|
| 29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|---|
| 30 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|---|
| 31 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|---|
| 32 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|---|
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|---|
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 35 | *
|
|---|
| 36 | * @category HTTP
|
|---|
| 37 | * @package HTTP_Request2
|
|---|
| 38 | * @author Alexey Borzov <[email protected]>
|
|---|
| 39 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 40 | * @version SVN: $Id: SocketWrapper.php 324935 2012-04-07 07:10:50Z avb $
|
|---|
| 41 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 42 | */
|
|---|
| 43 |
|
|---|
| 44 | /** Exception classes for HTTP_Request2 package */
|
|---|
| 45 | require_once 'HTTP/Request2/Exception.php';
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Socket wrapper class used by Socket Adapter
|
|---|
| 49 | *
|
|---|
| 50 | * Needed to properly handle connection errors, global timeout support and
|
|---|
| 51 | * similar things. Loosely based on Net_Socket used by older HTTP_Request.
|
|---|
| 52 | *
|
|---|
| 53 | * @category HTTP
|
|---|
| 54 | * @package HTTP_Request2
|
|---|
| 55 | * @author Alexey Borzov <[email protected]>
|
|---|
| 56 | * @license http://opensource.org/licenses/bsd-license.php New BSD License
|
|---|
| 57 | * @version Release: 2.1.1
|
|---|
| 58 | * @link http://pear.php.net/package/HTTP_Request2
|
|---|
| 59 | * @link http://pear.php.net/bugs/bug.php?id=19332
|
|---|
| 60 | * @link http://tools.ietf.org/html/rfc1928
|
|---|
| 61 | */
|
|---|
| 62 | class HTTP_Request2_SocketWrapper
|
|---|
| 63 | {
|
|---|
| 64 | /**
|
|---|
| 65 | * PHP warning messages raised during stream_socket_client() call
|
|---|
| 66 | * @var array
|
|---|
| 67 | */
|
|---|
| 68 | protected $connectionWarnings = array();
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * Connected socket
|
|---|
| 72 | * @var resource
|
|---|
| 73 | */
|
|---|
| 74 | protected $socket;
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Sum of start time and global timeout, exception will be thrown if request continues past this time
|
|---|
| 78 | * @var integer
|
|---|
| 79 | */
|
|---|
| 80 | protected $deadline;
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Global timeout value, mostly for exception messages
|
|---|
| 84 | * @var integer
|
|---|
| 85 | */
|
|---|
| 86 | protected $timeout;
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Class constructor, tries to establish connection
|
|---|
| 90 | *
|
|---|
| 91 | * @param string $address Address for stream_socket_client() call,
|
|---|
| 92 | * e.g. 'tcp://localhost:80'
|
|---|
| 93 | * @param int $timeout Connection timeout (seconds)
|
|---|
| 94 | * @param array $sslOptions SSL context options
|
|---|
| 95 | *
|
|---|
| 96 | * @throws HTTP_Request2_LogicException
|
|---|
| 97 | * @throws HTTP_Request2_ConnectionException
|
|---|
| 98 | */
|
|---|
| 99 | public function __construct($address, $timeout, array $sslOptions = array())
|
|---|
| 100 | {
|
|---|
| 101 | $context = stream_context_create();
|
|---|
| 102 | foreach ($sslOptions as $name => $value) {
|
|---|
| 103 | if (!stream_context_set_option($context, 'ssl', $name, $value)) {
|
|---|
| 104 | throw new HTTP_Request2_LogicException(
|
|---|
| 105 | "Error setting SSL context option '{$name}'"
|
|---|
| 106 | );
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 | set_error_handler(array($this, 'connectionWarningsHandler'));
|
|---|
| 110 | $this->socket = stream_socket_client(
|
|---|
| 111 | $address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context
|
|---|
| 112 | );
|
|---|
| 113 | restore_error_handler();
|
|---|
| 114 | if (!$this->socket) {
|
|---|
| 115 | $error = $errstr ? $errstr : implode("\n", $this->connectionWarnings);
|
|---|
| 116 | throw new HTTP_Request2_ConnectionException(
|
|---|
| 117 | "Unable to connect to {$address}. Error: {$error}", 0, $errno
|
|---|
| 118 | );
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Destructor, disconnects socket
|
|---|
| 124 | */
|
|---|
| 125 | public function __destruct()
|
|---|
| 126 | {
|
|---|
| 127 | fclose($this->socket);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Wrapper around fread(), handles global request timeout
|
|---|
| 132 | *
|
|---|
| 133 | * @param int $length Reads up to this number of bytes
|
|---|
| 134 | *
|
|---|
| 135 | * @return string Data read from socket
|
|---|
| 136 | * @throws HTTP_Request2_MessageException In case of timeout
|
|---|
| 137 | */
|
|---|
| 138 | public function read($length)
|
|---|
| 139 | {
|
|---|
| 140 | if ($this->deadline) {
|
|---|
| 141 | stream_set_timeout($this->socket, max($this->deadline - time(), 1));
|
|---|
| 142 | }
|
|---|
| 143 | $data = fread($this->socket, $length);
|
|---|
| 144 | $this->checkTimeout();
|
|---|
| 145 | return $data;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /**
|
|---|
| 149 | * Reads until either the end of the socket or a newline, whichever comes first
|
|---|
| 150 | *
|
|---|
| 151 | * Strips the trailing newline from the returned data, handles global
|
|---|
| 152 | * request timeout. Method idea borrowed from Net_Socket PEAR package.
|
|---|
| 153 | *
|
|---|
| 154 | * @param int $bufferSize buffer size to use for reading
|
|---|
| 155 | *
|
|---|
| 156 | * @return string Available data up to the newline (not including newline)
|
|---|
| 157 | * @throws HTTP_Request2_MessageException In case of timeout
|
|---|
| 158 | */
|
|---|
| 159 | public function readLine($bufferSize)
|
|---|
| 160 | {
|
|---|
| 161 | $line = '';
|
|---|
| 162 | while (!feof($this->socket)) {
|
|---|
| 163 | if ($this->deadline) {
|
|---|
| 164 | stream_set_timeout($this->socket, max($this->deadline - time(), 1));
|
|---|
| 165 | }
|
|---|
| 166 | $line .= @fgets($this->socket, $bufferSize);
|
|---|
| 167 | $this->checkTimeout();
|
|---|
| 168 | if (substr($line, -1) == "\n") {
|
|---|
| 169 | return rtrim($line, "\r\n");
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | return $line;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /**
|
|---|
| 176 | * Wrapper around fwrite(), handles global request timeout
|
|---|
| 177 | *
|
|---|
| 178 | * @param string $data String to be written
|
|---|
| 179 | *
|
|---|
| 180 | * @return int
|
|---|
| 181 | * @throws HTTP_Request2_MessageException
|
|---|
| 182 | */
|
|---|
| 183 | public function write($data)
|
|---|
| 184 | {
|
|---|
| 185 | if ($this->deadline) {
|
|---|
| 186 | stream_set_timeout($this->socket, max($this->deadline - time(), 1));
|
|---|
| 187 | }
|
|---|
| 188 | $written = fwrite($this->socket, $data);
|
|---|
| 189 | $this->checkTimeout();
|
|---|
| 190 | // http://www.php.net/manual/en/function.fwrite.php#96951
|
|---|
| 191 | if ($written < strlen($data)) {
|
|---|
| 192 | throw new HTTP_Request2_MessageException('Error writing request');
|
|---|
| 193 | }
|
|---|
| 194 | return $written;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Tests for end-of-file on a socket
|
|---|
| 199 | *
|
|---|
| 200 | * @return bool
|
|---|
| 201 | */
|
|---|
| 202 | public function eof()
|
|---|
| 203 | {
|
|---|
| 204 | return feof($this->socket);
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /**
|
|---|
| 208 | * Sets request deadline
|
|---|
| 209 | *
|
|---|
| 210 | * @param int $deadline Exception will be thrown if request continues
|
|---|
| 211 | * past this time
|
|---|
| 212 | * @param int $timeout Original request timeout value, to use in
|
|---|
| 213 | * Exception message
|
|---|
| 214 | */
|
|---|
| 215 | public function setDeadline($deadline, $timeout)
|
|---|
| 216 | {
|
|---|
| 217 | $this->deadline = $deadline;
|
|---|
| 218 | $this->timeout = $timeout;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Turns on encryption on a socket
|
|---|
| 223 | *
|
|---|
| 224 | * @throws HTTP_Request2_ConnectionException
|
|---|
| 225 | */
|
|---|
| 226 | public function enableCrypto()
|
|---|
| 227 | {
|
|---|
| 228 | $modes = array(
|
|---|
| 229 | STREAM_CRYPTO_METHOD_TLS_CLIENT,
|
|---|
| 230 | STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
|
|---|
| 231 | STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
|
|---|
| 232 | STREAM_CRYPTO_METHOD_SSLv2_CLIENT
|
|---|
| 233 | );
|
|---|
| 234 |
|
|---|
| 235 | foreach ($modes as $mode) {
|
|---|
| 236 | if (stream_socket_enable_crypto($this->socket, true, $mode)) {
|
|---|
| 237 | return;
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 | throw new HTTP_Request2_ConnectionException(
|
|---|
| 241 | 'Failed to enable secure connection when connecting through proxy'
|
|---|
| 242 | );
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /**
|
|---|
| 246 | * Throws an Exception if stream timed out
|
|---|
| 247 | *
|
|---|
| 248 | * @throws HTTP_Request2_MessageException
|
|---|
| 249 | */
|
|---|
| 250 | protected function checkTimeout()
|
|---|
| 251 | {
|
|---|
| 252 | $info = stream_get_meta_data($this->socket);
|
|---|
| 253 | if ($info['timed_out'] || $this->deadline && time() > $this->deadline) {
|
|---|
| 254 | $reason = $this->deadline
|
|---|
| 255 | ? "after {$this->timeout} second(s)"
|
|---|
| 256 | : 'due to default_socket_timeout php.ini setting';
|
|---|
| 257 | throw new HTTP_Request2_MessageException(
|
|---|
| 258 | "Request timed out {$reason}", HTTP_Request2_Exception::TIMEOUT
|
|---|
| 259 | );
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Error handler to use during stream_socket_client() call
|
|---|
| 265 | *
|
|---|
| 266 | * One stream_socket_client() call may produce *multiple* PHP warnings
|
|---|
| 267 | * (especially OpenSSL-related), we keep them in an array to later use for
|
|---|
| 268 | * the message of HTTP_Request2_ConnectionException
|
|---|
| 269 | *
|
|---|
| 270 | * @param int $errno error level
|
|---|
| 271 | * @param string $errstr error message
|
|---|
| 272 | *
|
|---|
| 273 | * @return bool
|
|---|
| 274 | */
|
|---|
| 275 | protected function connectionWarningsHandler($errno, $errstr)
|
|---|
| 276 | {
|
|---|
| 277 | if ($errno & E_WARNING) {
|
|---|
| 278 | array_unshift($this->connectionWarnings, $errstr);
|
|---|
| 279 | }
|
|---|
| 280 | return true;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | ?>
|
|---|