diff options
Diffstat (limited to 'vendor/gipfl/protocol-netstring')
-rw-r--r-- | vendor/gipfl/protocol-netstring/LICENSE | 21 | ||||
-rw-r--r-- | vendor/gipfl/protocol-netstring/composer.json | 25 | ||||
-rw-r--r-- | vendor/gipfl/protocol-netstring/src/StreamWrapper.php | 113 |
3 files changed, 159 insertions, 0 deletions
diff --git a/vendor/gipfl/protocol-netstring/LICENSE b/vendor/gipfl/protocol-netstring/LICENSE new file mode 100644 index 0000000..dd88e09 --- /dev/null +++ b/vendor/gipfl/protocol-netstring/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2018 Thomas Gelf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/gipfl/protocol-netstring/composer.json b/vendor/gipfl/protocol-netstring/composer.json new file mode 100644 index 0000000..0387e7d --- /dev/null +++ b/vendor/gipfl/protocol-netstring/composer.json @@ -0,0 +1,25 @@ +{ + "name": "gipfl/protocol-netstring", + "description": "Simple NetString stream wrapper", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Thomas Gelf", + "email": "thomas@gelf.net" + } + ], + "config": { + "sort-packages": true + }, + "autoload": { + "psr-4": { + "gipfl\\Protocol\\NetString\\": "src" + } + }, + "require": { + "php": ">=5.4.0", + "ext-ctype": "*", + "gipfl/protocol": ">=0.2" + } +} diff --git a/vendor/gipfl/protocol-netstring/src/StreamWrapper.php b/vendor/gipfl/protocol-netstring/src/StreamWrapper.php new file mode 100644 index 0000000..b23b909 --- /dev/null +++ b/vendor/gipfl/protocol-netstring/src/StreamWrapper.php @@ -0,0 +1,113 @@ +<?php + +namespace gipfl\Protocol\NetString; + +use gipfl\Protocol\Exception\ProtocolError; +use gipfl\Protocol\Generic\AbstractStreamWrapper; + +class StreamWrapper extends AbstractStreamWrapper +{ + protected $buffer = ''; + protected $bufferLength = 0; + protected $bufferOffset = 0; + protected $expectedLength; + + public function close() + { + // We might want to complain when buffer is not empty + $this->buffer = ''; + $this->bufferLength = 0; + $this->bufferOffset = 0; + $this->expectedLength = null; + parent::close(); + } + + /** + * @param $data + */ + public function handleData($data) + { + $this->buffer .= $data; + $this->bufferLength += \strlen($data); + while ($this->bufferHasPacket()) { + $this->processNextPacket(); + + if ($this->bufferOffset !== 0) { + $this->buffer = \substr($this->buffer, $this->bufferOffset); + $this->bufferOffset = 0; + $this->bufferLength = \strlen($this->buffer); + } + } + } + + public function write($data) + { + return $this->output->write(strlen($data) . ':' . $data . ','); + } + + public function end($data = null) + { + if ($data !== null) { + $this->write($data); + } + + $this->output->end(); + } + + /** + * @return bool + */ + protected function bufferHasPacket() + { + if ($this->expectedLength === null) { + if (false !== ($pos = \strpos(\substr($this->buffer, $this->bufferOffset, 10), ':'))) { + $lengthString = \ltrim(\substr($this->buffer, $this->bufferOffset, $pos), ','); + if (! \ctype_digit($lengthString)) { + $this->emit('error', [ + new ProtocolError("Invalid length $lengthString") + ]); + $this->close(); + + return false; + } + $this->expectedLength = (int) $lengthString; + $this->bufferOffset = $pos + 1; + } elseif ($this->bufferLength > ($this->bufferOffset + 10)) { + $this->throwInvalidBuffer(); + $this->close(); + + return false; + } else { + return false; + } + } + + return $this->bufferLength > ($this->bufferOffset + $this->expectedLength); + } + + protected function processNextPacket() + { + $packet = \substr($this->buffer, $this->bufferOffset, $this->expectedLength); + + $this->bufferOffset = $this->bufferOffset + $this->expectedLength; + $this->expectedLength = null; + + $this->emit('data', [$packet]); + } + + protected function throwInvalidBuffer() + { + $len = \strlen($this->buffer); + if ($len < 200) { + $debug = $this->buffer; + } else { + $debug = \substr($this->buffer, 0, 100) + . \sprintf('[..] truncated %d bytes [..] ', $len) + . \substr($this->buffer, -100); + } + + $this->emit('error', [ + new ProtocolError("Got invalid NetString data: $debug") + ]); + } +} |