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