|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is a part of Woketo package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Nekland <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full license, take a look to the LICENSE file |
|
8
|
|
|
* on the root directory of this project |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Nekland\Woketo\Rfc6455\FrameHandler; |
|
12
|
|
|
|
|
13
|
|
|
use Nekland\Woketo\Rfc6455\Frame; |
|
14
|
|
|
use Nekland\Woketo\Rfc6455\Message; |
|
15
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor; |
|
16
|
|
|
use Nekland\Woketo\Utils\BitManipulation; |
|
17
|
|
|
use React\Stream\Stream; |
|
18
|
|
|
|
|
19
|
|
|
class CloseFrameHandler implements Rfc6455FrameHandlerInterface |
|
20
|
|
|
{ |
|
21
|
8 |
|
public function supports(Message $message) |
|
22
|
|
|
{ |
|
23
|
8 |
|
return $message->getFirstFrame()->getOpcode() === Frame::OP_CLOSE; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
8 |
|
public function process(Message $message, MessageProcessor $messageProcessor, Stream $socket) |
|
27
|
|
|
{ |
|
28
|
8 |
|
$code = Frame::CLOSE_NORMAL; |
|
29
|
|
|
|
|
30
|
8 |
|
$frame = $message->getFirstFrame(); |
|
31
|
8 |
|
$payload = $frame->getPayload(); |
|
32
|
8 |
|
if ($frame->getRsv1() || $frame->getRsv2() || $frame->getRsv3()) { |
|
33
|
1 |
|
$code = Frame::CLOSE_PROTOCOL_ERROR; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
if (BitManipulation::frameSize($payload) > 1) { |
|
37
|
8 |
|
$errorCode = BitManipulation::bytesFromTo($payload, 0, 1); |
|
38
|
|
|
|
|
39
|
|
|
// https://tools.ietf.org/html/rfc6455#section-7.4 |
|
40
|
|
|
if ( |
|
41
|
|
|
( |
|
42
|
8 |
|
!\in_array($errorCode, [ |
|
43
|
8 |
|
Frame::CLOSE_NORMAL, Frame::CLOSE_GOING_AWAY, Frame::CLOSE_PROTOCOL_ERROR, Frame::CLOSE_GOING_AWAY, |
|
44
|
|
|
Frame::CLOSE_WRONG_DATA, Frame::CLOSE_INCOHERENT_DATA, Frame::CLOSE_TOO_BIG_TO_PROCESS, |
|
45
|
|
|
Frame::CLOSE_POLICY_VIOLATION, Frame::CLOSE_MISSING_EXTENSION, Frame::CLOSE_UNEXPECTING_CONDITION |
|
46
|
|
|
]) |
|
47
|
8 |
|
&& $errorCode < 3000 && $errorCode > 900 |
|
48
|
|
|
) |
|
49
|
6 |
|
|| $errorCode > 4999 |
|
50
|
8 |
|
|| $errorCode < 1000 |
|
51
|
|
|
) { |
|
52
|
4 |
|
$code = Frame::CLOSE_PROTOCOL_ERROR; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
8 |
|
$messageProcessor->write($messageProcessor->getFrameFactory()->createCloseFrame($code), $socket); |
|
57
|
8 |
|
$socket->end(); |
|
58
|
8 |
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|