• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sirn-se / websocket-php / 8346487915

19 Mar 2024 04:15PM UTC coverage: 22.584% (-77.4%) from 100.0%
8346487915

push

github

sirn-se
Temp test verification

2 of 2 new or added lines in 1 file covered. (100.0%)

742 existing lines in 32 files now uncovered.

222 of 983 relevant lines covered (22.58%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Middleware/SubprotocolNegotiation.php
1
<?php
2

3
/**
4
 * Copyright (C) 2014-2024 Textalk and contributors.
5
 *
6
 * This file is part of Websocket PHP and is free software under the ISC License.
7
 * License text: https://raw.githubusercontent.com/sirn-se/websocket-php/master/COPYING.md
8
 */
9

10
namespace WebSocket\Middleware;
11

12
use Psr\Log\{
13
    LoggerAwareInterface,
14
    LoggerAwareTrait
15
};
16
use Stringable;
17
use WebSocket\Connection;
18
use WebSocket\Exception\HandshakeException;
19
use WebSocket\Http\{
20
    Message,
21
    Request,
22
    Response,
23
    ServerRequest,
24
};
25
use WebSocket\Trait\StringableTrait;
26

27
/**
28
 * WebSocket\Middleware\CloseHandler class.
29
 * Handles close procedure.
30
 */
31
class SubprotocolNegotiation implements
32
    LoggerAwareInterface,
33
    ProcessHttpOutgoingInterface,
34
    ProcessHttpIncomingInterface,
35
    Stringable
36
{
37
    use LoggerAwareTrait;
38
    use StringableTrait;
39

40
    private $subprotocols;
41
    private $require;
42

43
    public function __construct(array $subprotocols, bool $require = false)
44
    {
UNCOV
45
        $this->subprotocols = $subprotocols;
×
UNCOV
46
        $this->require = $require;
×
47
    }
48

49
    public function processHttpOutgoing(ProcessHttpStack $stack, Connection $connection, Message $message): Message
50
    {
UNCOV
51
        if ($message instanceof Request) {
×
52
            // Outgoing requests on Client
UNCOV
53
            foreach ($this->subprotocols as $subprotocol) {
×
UNCOV
54
                $message = $message->withAddedHeader('Sec-WebSocket-Protocol', $subprotocol);
×
55
            }
UNCOV
56
            if ($supported = implode(', ', $this->subprotocols)) {
×
UNCOV
57
                $this->logger->debug("[subprotocol-negotiation] Requested subprotocols: {$supported}");
×
58
            }
UNCOV
59
        } elseif ($message instanceof Response) {
×
60
            // Outgoing Response on Server
UNCOV
61
            if ($selected = $connection->getMeta('subprotocolNegotiation.selected')) {
×
UNCOV
62
                $message = $message->withHeader('Sec-WebSocket-Protocol', $selected);
×
UNCOV
63
                $this->logger->info("[subprotocol-negotiation] Selected subprotocol: {$selected}");
×
UNCOV
64
            } elseif ($this->require) {
×
65
                // No matching subprotocol, fail handshake
UNCOV
66
                $message = $message->withStatus(426);
×
67
            }
68
        }
UNCOV
69
        return $stack->handleHttpOutgoing($message);
×
70
    }
71

72
    public function processHttpIncoming(ProcessHttpStack $stack, Connection $connection): Message
73
    {
UNCOV
74
        $connection->setMeta('subprotocolNegotiation.selected', null);
×
UNCOV
75
        $message = $stack->handleHttpIncoming();
×
76

UNCOV
77
        if ($message instanceof ServerRequest) {
×
78
            // Incoming requests on Server
UNCOV
79
            if ($requested = $message->getHeaderLine('Sec-WebSocket-Protocol')) {
×
UNCOV
80
                $this->logger->debug("[subprotocol-negotiation] Requested subprotocols: {$requested}");
×
81
            }
UNCOV
82
            if ($supported = implode(', ', $this->subprotocols)) {
×
UNCOV
83
                $this->logger->debug("[subprotocol-negotiation] Supported subprotocols: {$supported}");
×
84
            }
UNCOV
85
            foreach ($message->getHeader('Sec-WebSocket-Protocol') as $subprotocol) {
×
UNCOV
86
                if (in_array($subprotocol, $this->subprotocols)) {
×
UNCOV
87
                    $connection->setMeta('subprotocolNegotiation.selected', $subprotocol);
×
UNCOV
88
                    return $message;
×
89
                }
90
            }
UNCOV
91
        } elseif ($message instanceof Response) {
×
92
            // Incoming Response on Client
UNCOV
93
            if ($selected = $message->getHeaderLine('Sec-WebSocket-Protocol')) {
×
UNCOV
94
                $connection->setMeta('subprotocolNegotiation.selected', $selected);
×
UNCOV
95
                $this->logger->info("[subprotocol-negotiation] Selected subprotocol: {$selected}");
×
UNCOV
96
            } elseif ($this->require) {
×
97
                // No matching subprotocol, close and fail
UNCOV
98
                $connection->close();
×
UNCOV
99
                throw new HandshakeException("Could not resolve subprotocol.", $message);
×
100
            }
101
        }
UNCOV
102
        return $message;
×
103
    }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc