123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- require_once 'HTTP/Request2/Exception.php';
- class HTTP_Request2_SocketWrapper
- {
-
- protected $connectionWarnings = array();
-
- protected $socket;
-
- protected $deadline;
-
- protected $timeout;
-
- public function __construct($address, $timeout, array $contextOptions = array())
- {
- if (!empty($contextOptions)
- && !isset($contextOptions['socket']) && !isset($contextOptions['ssl'])
- ) {
-
- $contextOptions = array('ssl' => $contextOptions);
- }
- if (isset($contextOptions['ssl'])) {
- $contextOptions['ssl'] += array(
-
-
- 'ciphers' => 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:'
- . 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:'
- . 'DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:'
- . 'ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:'
- . 'ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:'
- . 'ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:'
- . 'ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:'
- . 'DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:'
- . 'DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:'
- . 'ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:'
- . 'AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:'
- . 'AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:'
- . '!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA'
- );
- if (version_compare(phpversion(), '5.4.13', '>=')) {
- $contextOptions['ssl']['disable_compression'] = true;
- if (version_compare(phpversion(), '5.6', '>=')) {
- $contextOptions['ssl']['crypto_method'] = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
- | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
- }
- }
- }
- $context = stream_context_create();
- foreach ($contextOptions as $wrapper => $options) {
- foreach ($options as $name => $value) {
- if (!stream_context_set_option($context, $wrapper, $name, $value)) {
- throw new HTTP_Request2_LogicException(
- "Error setting '{$wrapper}' wrapper context option '{$name}'"
- );
- }
- }
- }
- set_error_handler(array($this, 'connectionWarningsHandler'));
- $this->socket = stream_socket_client(
- $address, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context
- );
- restore_error_handler();
-
-
-
-
- if ($this->connectionWarnings) {
- if ($this->socket) {
- fclose($this->socket);
- }
- $error = $errstr ? $errstr : implode("\n", $this->connectionWarnings);
- throw new HTTP_Request2_ConnectionException(
- "Unable to connect to {$address}. Error: {$error}", 0, $errno
- );
- }
- }
-
- public function __destruct()
- {
- fclose($this->socket);
- }
-
- public function read($length)
- {
- if ($this->deadline) {
- stream_set_timeout($this->socket, max($this->deadline - time(), 1));
- }
- $data = fread($this->socket, $length);
- $this->checkTimeout();
- return $data;
- }
-
- public function readLine($bufferSize, $localTimeout = null)
- {
- $line = '';
- while (!feof($this->socket)) {
- if (null !== $localTimeout) {
- $timeout = $localTimeout;
- } elseif ($this->deadline) {
- $timeout = max($this->deadline - time(), 1);
- } else {
-
-
-
-
- $timeout = null;
- }
- $info = stream_get_meta_data($this->socket);
- $old_blocking = (bool)$info['blocked'];
- stream_set_blocking($this->socket, false);
- $r = array($this->socket);
- $w = array();
- $e = array();
- if (stream_select($r, $w, $e, $timeout)) {
- $line .= @fgets($this->socket, $bufferSize);
- }
- stream_set_blocking($this->socket, $old_blocking);
- if (null === $localTimeout) {
- $this->checkTimeout();
- } else {
- $info = stream_get_meta_data($this->socket);
-
-
- if (!$this->deadline) {
- $default = (int)@ini_get('default_socket_timeout');
- stream_set_timeout($this->socket, $default > 0 ? $default : PHP_INT_MAX);
- }
- if ($info['timed_out']) {
- throw new HTTP_Request2_MessageException(
- "readLine() call timed out", HTTP_Request2_Exception::TIMEOUT
- );
- }
- }
- if (substr($line, -1) == "\n") {
- return rtrim($line, "\r\n");
- }
- }
- return $line;
- }
-
- public function write($data)
- {
- if ($this->deadline) {
- stream_set_timeout($this->socket, max($this->deadline - time(), 1));
- }
- $written = fwrite($this->socket, $data);
- $this->checkTimeout();
-
- if ($written < strlen($data)) {
- throw new HTTP_Request2_MessageException('Error writing request');
- }
- return $written;
- }
-
- public function eof()
- {
- return feof($this->socket);
- }
-
- public function setDeadline($deadline, $timeout)
- {
- $this->deadline = $deadline;
- $this->timeout = $timeout;
- }
-
- public function enableCrypto()
- {
- if (version_compare(phpversion(), '5.6', '<')) {
- $cryptoMethod = STREAM_CRYPTO_METHOD_TLS_CLIENT;
- } else {
- $cryptoMethod = STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT
- | STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
- }
- if (!stream_socket_enable_crypto($this->socket, true, $cryptoMethod)) {
- throw new HTTP_Request2_ConnectionException(
- 'Failed to enable secure connection when connecting through proxy'
- );
- }
- }
-
- protected function checkTimeout()
- {
- $info = stream_get_meta_data($this->socket);
- if ($info['timed_out'] || $this->deadline && time() > $this->deadline) {
- $reason = $this->deadline
- ? "after {$this->timeout} second(s)"
- : 'due to default_socket_timeout php.ini setting';
- throw new HTTP_Request2_MessageException(
- "Request timed out {$reason}", HTTP_Request2_Exception::TIMEOUT
- );
- }
- }
-
- protected function connectionWarningsHandler($errno, $errstr)
- {
- if ($errno & E_WARNING) {
- array_unshift($this->connectionWarnings, $errstr);
- }
- return true;
- }
- }
- ?>
|