From 2591e94e6badcf469a49a787738a3d4e6aa0c8dd Mon Sep 17 00:00:00 2001 From: Mark Friedrich Date: Sun, 2 Jun 2019 16:57:25 +0200 Subject: [PATCH] - fixed `OverflowException` for large JSON payload received over TCP Socket connections --- .gitignore | 1 - app/Socket/TcpSocket.php | 19 +++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 13cd459..1f1701e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,5 @@ out gen -composer.lock vendor diff --git a/app/Socket/TcpSocket.php b/app/Socket/TcpSocket.php index cf2f229..e989879 100644 --- a/app/Socket/TcpSocket.php +++ b/app/Socket/TcpSocket.php @@ -84,6 +84,12 @@ class TcpSocket { */ const DEFAULT_DEBUG = false; + /** + * max length for JSON data string + * -> throw OverflowException on exceed + */ + const JSON_DECODE_MAX_LENGTH = 65536 * 4; + /** * global server loop * @var EventLoop\LoopInterface @@ -218,7 +224,7 @@ class TcpSocket { if('json' == $this->acceptType){ // new empty stream for processing JSON $stream = new Stream\ThroughStream(); - $streamDecoded = new NDJson\Decoder($stream, true); + $streamDecoded = new NDJson\Decoder($stream, true, 512, 0, self::JSON_DECODE_MAX_LENGTH); // promise get resolved on first emit('data') $promise = Promise\Stream\first($streamDecoded); @@ -388,7 +394,7 @@ class TcpSocket { $this->hasConnection($connection) && ($data = (array)$this->connections->offsetGet($connection)) && isset($data['timers']) && isset($data['timers'][$timerName]) && - ($data['timers'][$timerName] instanceof EventLoop\Timer\TimerInterface) + ($data['timers'][$timerName] instanceof EventLoop\TimerInterface) ){ $this->loop->cancelTimer($data['timers'][$timerName]); @@ -535,11 +541,16 @@ class TcpSocket { */ protected function debug(Socket\ConnectionInterface $connection, string $method, string $message = ''){ if(self::DEFAULT_DEBUG){ + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); + $caller = array_shift($backtrace); + $callerOrig = array_shift($backtrace); + $data = [ date('Y-m-d H:i:s'), - __CLASS__ . '()', + 'DEBUG', $connection->getRemoteAddress(), - $method . '()', + $caller['file'] . ' line ' . $caller['line'], + $callerOrig['function'] . '()' . (($callerOrig['function'] !== $method) ? ' [' . $method . '()]' : ''), $message ];