Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace React\Socket;
4
 
5
use React\EventLoop\LoopInterface;
6
use React\Promise;
7
use BadMethodCallException;
8
use InvalidArgumentException;
9
use UnexpectedValueException;
10
 
11
final class SecureConnector implements ConnectorInterface
12
{
13
    private $connector;
14
    private $streamEncryption;
15
    private $context;
16
 
17
    public function __construct(ConnectorInterface $connector, LoopInterface $loop, array $context = array())
18
    {
19
        $this->connector = $connector;
20
        $this->streamEncryption = new StreamEncryption($loop, false);
21
        $this->context = $context;
22
    }
23
 
24
    public function connect($uri)
25
    {
26
        if (!\function_exists('stream_socket_enable_crypto')) {
27
            return Promise\reject(new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)')); // @codeCoverageIgnore
28
        }
29
 
30
        if (\strpos($uri, '://') === false) {
31
            $uri = 'tls://' . $uri;
32
        }
33
 
34
        $parts = \parse_url($uri);
35
        if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') {
36
            return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
37
        }
38
 
39
        $uri = \str_replace('tls://', '', $uri);
40
        $context = $this->context;
41
 
42
        $encryption = $this->streamEncryption;
43
        $connected = false;
44
        $promise = $this->connector->connect($uri)->then(function (ConnectionInterface $connection) use ($context, $encryption, $uri, &$promise, &$connected) {
45
            // (unencrypted) TCP/IP connection succeeded
46
            $connected = true;
47
 
48
            if (!$connection instanceof Connection) {
49
                $connection->close();
50
                throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
51
            }
52
 
53
            // set required SSL/TLS context options
54
            foreach ($context as $name => $value) {
55
                \stream_context_set_option($connection->stream, 'ssl', $name, $value);
56
            }
57
 
58
            // try to enable encryption
59
            return $promise = $encryption->enable($connection)->then(null, function ($error) use ($connection, $uri) {
60
                // establishing encryption failed => close invalid connection and return error
61
                $connection->close();
62
 
63
                throw new \RuntimeException(
64
                    'Connection to ' . $uri . ' failed during TLS handshake: ' . $error->getMessage(),
65
                    $error->getCode()
66
                );
67
            });
68
        });
69
 
70
        return new \React\Promise\Promise(
71
            function ($resolve, $reject) use ($promise) {
72
                $promise->then($resolve, $reject);
73
            },
74
            function ($_, $reject) use (&$promise, $uri, &$connected) {
75
                if ($connected) {
76
                    $reject(new \RuntimeException('Connection to ' . $uri . ' cancelled during TLS handshake'));
77
                }
78
 
79
                $promise->cancel();
80
                $promise = null;
81
            }
82
        );
83
    }
84
}