- fixed OverflowException for large JSON payload received over TCP Socket connections

This commit is contained in:
Mark Friedrich
2019-06-02 16:57:25 +02:00
parent f78b177c79
commit 2591e94e6b
2 changed files with 15 additions and 5 deletions

1
.gitignore vendored
View File

@@ -7,6 +7,5 @@
out
gen
composer.lock
vendor

View File

@@ -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
];