Debian Codegraph @

vendor/clue/redis-protocol/src/Clue/Redis/Protocol/Parser/RequestParser.php

raw
1 <?php
2
3 namespace Clue\Redis\Protocol\Parser;
4
5 use Clue\Redis\Protocol\Parser\ParserException;
6 use Clue\Redis\Protocol\Model\Request;
7
8 class RequestParser implements ParserInterface
9 {
10 const CRLF = "\r\n";
11
12 private $incomingBuffer = '';
13 private $incomingOffset = 0;
14
15 public function pushIncoming($dataChunk)
16 {
17 $this->incomingBuffer .= $dataChunk;
18
19 $parsed = array();
20
21 do {
22 $saved = $this->incomingOffset;
23 $message = $this->readRequest();
24 if ($message === null) {
25 // restore previous position for next parsing attempt
26 $this->incomingOffset = $saved;
27 break;
28 }
29
30 if ($message !== false) {
31 $parsed []= $message;
32 }
33 } while($this->incomingBuffer !== '');
34
35 if ($this->incomingOffset !== 0) {
36 $this->incomingBuffer = (string)substr($this->incomingBuffer, $this->incomingOffset);
37 $this->incomingOffset = 0;
38 }
39
40 return $parsed;
41 }
42
43 /**
44 * try to parse request from incoming buffer
45 *
46 * @throws ParserException if the incoming buffer is invalid
47 * @return Request|null
48 */
49 private function readRequest()
50 {
51 $crlf = strpos($this->incomingBuffer, "\r\n", $this->incomingOffset);
52 if ($crlf === false) {
53 return null;
54 }
55
56 // line starts with a multi-bulk header "*"
57 if (isset($this->incomingBuffer[$this->incomingOffset]) && $this->incomingBuffer[$this->incomingOffset] === '*') {
58 $line = substr($this->incomingBuffer, $this->incomingOffset + 1, $crlf - $this->incomingOffset + 1);
59 $this->incomingOffset = $crlf + 2;
60 $count = (int)$line;
61
62 if ($count <= 0) {
63 return false;
64 }
65 $command = null;
66 $args = array();
67 for ($i = 0; $i < $count; ++$i) {
68 $sub = $this->readBulk();
69 if ($sub === null) {
70 return null;
71 }
72 if ($command === null) {
73 $command = $sub;
74 } else {
75 $args []= $sub;
76 }
77 }
78 return new Request($command, $args);
79 }
80
81 // parse an old inline request instead
82 $line = substr($this->incomingBuffer, $this->incomingOffset, $crlf - $this->incomingOffset);
83 $this->incomingOffset = $crlf + 2;
84
85 $args = preg_split('/ +/', trim($line, ' '));
86 $command = array_shift($args);
87
88 if ($command === '') {
89 return false;
90 }
91
92 return new Request($command, $args);
93 }
94
95 private function readBulk()
96 {
97 $crlf = strpos($this->incomingBuffer, "\r\n", $this->incomingOffset);
98 if ($crlf === false) {
99 return null;
100 }
101
102 // line has to start with a bulk header "$"
103 if (!isset($this->incomingBuffer[$this->incomingOffset]) || $this->incomingBuffer[$this->incomingOffset] !== '$') {
104 throw new ParserException('ERR Protocol error: expected \'$\', got \'' . substr($this->incomingBuffer, $this->incomingOffset, 1) . '\'');
105 }
106
107 $line = substr($this->incomingBuffer, $this->incomingOffset + 1, $crlf - $this->incomingOffset + 1);
108 $this->incomingOffset = $crlf + 2;
109 $size = (int)$line;
110
111 if ($size < 0) {
112 throw new ParserException('ERR Protocol error: invalid bulk length');
113 }
114
115 if (!isset($this->incomingBuffer[$this->incomingOffset + $size + 1])) {
116 // check enough bytes + crlf are buffered
117 return null;
118 }
119
120 $ret = substr($this->incomingBuffer, $this->incomingOffset, $size);
121 $this->incomingOffset += $size + 2;
122
123 return $ret;
124 }
125 }