--- /dev/null
+<?php\r
+/**\r
+ * Socket wrapper class used by Socket Adapter\r
+ *\r
+ * PHP version 5\r
+ *\r
+ * LICENSE:\r
+ *\r
+ * Copyright (c) 2008-2012, Alexey Borzov <avb@php.net>\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ *\r
+ * * Redistributions of source code must retain the above copyright\r
+ * notice, this list of conditions and the following disclaimer.\r
+ * * Redistributions in binary form must reproduce the above copyright\r
+ * notice, this list of conditions and the following disclaimer in the\r
+ * documentation and/or other materials provided with the distribution.\r
+ * * The names of the authors may not be used to endorse or promote products\r
+ * derived from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ *\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version SVN: $Id: SocketWrapper.php 324935 2012-04-07 07:10:50Z avb $\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
+ */\r
+\r
+/** Exception classes for HTTP_Request2 package */\r
+require_once 'HTTP/Request2/Exception.php';\r
+\r
+/**\r
+ * Socket wrapper class used by Socket Adapter\r
+ *\r
+ * Needed to properly handle connection errors, global timeout support and\r
+ * similar things. Loosely based on Net_Socket used by older HTTP_Request.\r
+ *\r
+ * @category HTTP\r
+ * @package HTTP_Request2\r
+ * @author Alexey Borzov <avb@php.net>\r
+ * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
+ * @version Release: 2.1.1\r
+ * @link http://pear.php.net/package/HTTP_Request2\r
+ * @link http://pear.php.net/bugs/bug.php?id=19332\r
+ * @link http://tools.ietf.org/html/rfc1928\r
+ */\r
+class HTTP_Request2_SocketWrapper\r
+{\r
+ /**\r
+ * PHP warning messages raised during stream_socket_client() call\r
+ * @var array\r
+ */\r
+ protected $connectionWarnings = array();\r
+\r
+ /**\r
+ * Connected socket\r
+ * @var resource\r
+ */\r
+ protected $socket;\r
+\r
+ /**\r
+ * Sum of start time and global timeout, exception will be thrown if request continues past this time\r
+ * @var integer\r
+ */\r
+ protected $deadline;\r
+\r
+ /**\r
+ * Global timeout value, mostly for exception messages\r
+ * @var integer\r
+ */\r
+ protected $timeout;\r
+\r
+ /**\r
+ * Class constructor, tries to establish connection\r
+ *\r
+ * @param string $address Address for stream_socket_client() call,\r
+ * e.g. 'tcp://localhost:80'\r
+ * @param int $timeout Connection timeout (seconds)\r
+ * @param array $sslOptions SSL context options\r
+ *\r
+ * @throws HTTP_Request2_LogicException\r
+ * @throws HTTP_Request2_ConnectionException\r
+ */\r
+ public function __construct($address, $timeout, array $sslOptions = array())\r
+ {\r
+ $context = stream_context_create();\r
+ foreach ($sslOptions as $name => $value) {\r
+ if (!stream_context_set_option($context, 'ssl', $name, $value)) {\r
+ throw new HTTP_Request2_LogicException(\r
+ "Error setting SSL context option '{$name}'"\r
+ );\r
+ }\r
+ }\r
+ set_error_handler(array($this, 'connectionWarningsHandler'));\r
+ $this->socket = stream_socket_client(\r
+ $address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context\r
+ );\r
+ restore_error_handler();\r
+ if (!$this->socket) {\r
+ $error = $errstr ? $errstr : implode("\n", $this->connectionWarnings);\r
+ throw new HTTP_Request2_ConnectionException(\r
+ "Unable to connect to {$address}. Error: {$error}", 0, $errno\r
+ );\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Destructor, disconnects socket\r
+ */\r
+ public function __destruct()\r
+ {\r
+ fclose($this->socket);\r
+ }\r
+\r
+ /**\r
+ * Wrapper around fread(), handles global request timeout\r
+ *\r
+ * @param int $length Reads up to this number of bytes\r
+ *\r
+ * @return string Data read from socket\r
+ * @throws HTTP_Request2_MessageException In case of timeout\r
+ */\r
+ public function read($length)\r
+ {\r
+ if ($this->deadline) {\r
+ stream_set_timeout($this->socket, max($this->deadline - time(), 1));\r
+ }\r
+ $data = fread($this->socket, $length);\r
+ $this->checkTimeout();\r
+ return $data;\r
+ }\r
+\r
+ /**\r
+ * Reads until either the end of the socket or a newline, whichever comes first\r
+ *\r
+ * Strips the trailing newline from the returned data, handles global\r
+ * request timeout. Method idea borrowed from Net_Socket PEAR package.\r
+ *\r
+ * @param int $bufferSize buffer size to use for reading\r
+ *\r
+ * @return string Available data up to the newline (not including newline)\r
+ * @throws HTTP_Request2_MessageException In case of timeout\r
+ */\r
+ public function readLine($bufferSize)\r
+ {\r
+ $line = '';\r
+ while (!feof($this->socket)) {\r
+ if ($this->deadline) {\r
+ stream_set_timeout($this->socket, max($this->deadline - time(), 1));\r
+ }\r
+ $line .= @fgets($this->socket, $bufferSize);\r
+ $this->checkTimeout();\r
+ if (substr($line, -1) == "\n") {\r
+ return rtrim($line, "\r\n");\r
+ }\r
+ }\r
+ return $line;\r
+ }\r
+\r
+ /**\r
+ * Wrapper around fwrite(), handles global request timeout\r
+ *\r
+ * @param string $data String to be written\r
+ *\r
+ * @return int\r
+ * @throws HTTP_Request2_MessageException\r
+ */\r
+ public function write($data)\r
+ {\r
+ if ($this->deadline) {\r
+ stream_set_timeout($this->socket, max($this->deadline - time(), 1));\r
+ }\r
+ $written = fwrite($this->socket, $data);\r
+ $this->checkTimeout();\r
+ // http://www.php.net/manual/en/function.fwrite.php#96951\r
+ if ($written < strlen($data)) {\r
+ throw new HTTP_Request2_MessageException('Error writing request');\r
+ }\r
+ return $written;\r
+ }\r
+\r
+ /**\r
+ * Tests for end-of-file on a socket\r
+ *\r
+ * @return bool\r
+ */\r
+ public function eof()\r
+ {\r
+ return feof($this->socket);\r
+ }\r
+\r
+ /**\r
+ * Sets request deadline\r
+ *\r
+ * @param int $deadline Exception will be thrown if request continues\r
+ * past this time\r
+ * @param int $timeout Original request timeout value, to use in\r
+ * Exception message\r
+ */\r
+ public function setDeadline($deadline, $timeout)\r
+ {\r
+ $this->deadline = $deadline;\r
+ $this->timeout = $timeout;\r
+ }\r
+\r
+ /**\r
+ * Turns on encryption on a socket\r
+ *\r
+ * @throws HTTP_Request2_ConnectionException\r
+ */\r
+ public function enableCrypto()\r
+ {\r
+ $modes = array(\r
+ STREAM_CRYPTO_METHOD_TLS_CLIENT,\r
+ STREAM_CRYPTO_METHOD_SSLv3_CLIENT,\r
+ STREAM_CRYPTO_METHOD_SSLv23_CLIENT,\r
+ STREAM_CRYPTO_METHOD_SSLv2_CLIENT\r
+ );\r
+\r
+ foreach ($modes as $mode) {\r
+ if (stream_socket_enable_crypto($this->socket, true, $mode)) {\r
+ return;\r
+ }\r
+ }\r
+ throw new HTTP_Request2_ConnectionException(\r
+ 'Failed to enable secure connection when connecting through proxy'\r
+ );\r
+ }\r
+\r
+ /**\r
+ * Throws an Exception if stream timed out\r
+ *\r
+ * @throws HTTP_Request2_MessageException\r
+ */\r
+ protected function checkTimeout()\r
+ {\r
+ $info = stream_get_meta_data($this->socket);\r
+ if ($info['timed_out'] || $this->deadline && time() > $this->deadline) {\r
+ $reason = $this->deadline\r
+ ? "after {$this->timeout} second(s)"\r
+ : 'due to default_socket_timeout php.ini setting';\r
+ throw new HTTP_Request2_MessageException(\r
+ "Request timed out {$reason}", HTTP_Request2_Exception::TIMEOUT\r
+ );\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Error handler to use during stream_socket_client() call\r
+ *\r
+ * One stream_socket_client() call may produce *multiple* PHP warnings\r
+ * (especially OpenSSL-related), we keep them in an array to later use for\r
+ * the message of HTTP_Request2_ConnectionException\r
+ *\r
+ * @param int $errno error level\r
+ * @param string $errstr error message\r
+ *\r
+ * @return bool\r
+ */\r
+ protected function connectionWarningsHandler($errno, $errstr)\r
+ {\r
+ if ($errno & E_WARNING) {\r
+ array_unshift($this->connectionWarnings, $errstr);\r
+ }\r
+ return true;\r
+ }\r
+}\r
+?>\r