aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/react/socket/src/SecureConnector.php
blob: f04183d33544d34e7fb012647a8de149f956fb67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

namespace React\Socket;

use React\EventLoop\LoopInterface;
use React\Promise;
use BadMethodCallException;
use InvalidArgumentException;
use UnexpectedValueException;

final class SecureConnector implements ConnectorInterface
{
    private $connector;
    private $streamEncryption;
    private $context;

    public function __construct(ConnectorInterface $connector, LoopInterface $loop, array $context = array())
    {
        $this->connector = $connector;
        $this->streamEncryption = new StreamEncryption($loop, false);
        $this->context = $context;
    }

    public function connect($uri)
    {
        if (!function_exists('stream_socket_enable_crypto')) {
            return Promise\reject(new BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)')); // @codeCoverageIgnore
        }

        if (strpos($uri, '://') === false) {
            $uri = 'tls://' . $uri;
        }

        $parts = parse_url($uri);
        if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') {
            return Promise\reject(new InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
        }

        $uri = str_replace('tls://', '', $uri);
        $context = $this->context;

        $encryption = $this->streamEncryption;
        return $this->connector->connect($uri)->then(function (ConnectionInterface $connection) use ($context, $encryption) {
            // (unencrypted) TCP/IP connection succeeded

            if (!$connection instanceof Connection) {
                $connection->close();
                throw new UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
            }

            // set required SSL/TLS context options
            foreach ($context as $name => $value) {
                stream_context_set_option($connection->stream, 'ssl', $name, $value);
            }

            // try to enable encryption
            return $encryption->enable($connection)->then(null, function ($error) use ($connection) {
                // establishing encryption failed => close invalid connection and return error
                $connection->close();
                throw $error;
            });
        });
    }
}