aboutsummaryrefslogtreecommitdiffhomepage
path: root/assets/php/vendor/ratchet/rfc6455/src/Handshake/ResponseVerifier.php
blob: de03f5365787bd36cb25b5e53692d34af78cae95 (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
<?php
namespace Ratchet\RFC6455\Handshake;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ResponseVerifier {
    public function verifyAll(RequestInterface $request, ResponseInterface $response) {
        $passes = 0;

        $passes += (int)$this->verifyStatus($response->getStatusCode());
        $passes += (int)$this->verifyUpgrade($response->getHeader('Upgrade'));
        $passes += (int)$this->verifyConnection($response->getHeader('Connection'));
        $passes += (int)$this->verifySecWebSocketAccept(
            $response->getHeader('Sec-WebSocket-Accept')
          , $request->getHeader('Sec-WebSocket-Key')
        );
        $passes += (int)$this->verifySubProtocol(
            $request->getHeader('Sec-WebSocket-Protocol')
          , $response->getHeader('Sec-WebSocket-Protocol')
        );

        return (5 === $passes);
    }

    public function verifyStatus($status) {
        return ((int)$status === 101);
    }

    public function verifyUpgrade(array $upgrade) {
        return (in_array('websocket', array_map('strtolower', $upgrade)));
    }

    public function verifyConnection(array $connection) {
        return (in_array('upgrade', array_map('strtolower', $connection)));
    }

    public function verifySecWebSocketAccept($swa, $key) {
        return (
            1 === count($swa) &&
            1 === count($key) &&
            $swa[0] === $this->sign($key[0])
        );
    }

    public function sign($key) {
        return base64_encode(sha1($key . NegotiatorInterface::GUID, true));
    }

    public function verifySubProtocol(array $requestHeader, array $responseHeader) {
        return 0 === count($responseHeader) || count(array_intersect($responseHeader, $requestHeader)) > 0;
    }
}