From ad7139fcc314c0030bf647c287539ace11399602 Mon Sep 17 00:00:00 2001 From: Fabricio Seger Kolling Date: Sun, 14 May 2017 04:11:27 -0300 Subject: [PATCH 1/2] HTTPD default 8Kb header limit check, to avoid 500 Internal Server Error The HTTP spec does not define a limit, however many servers do by default: Apache 2.0, 2.2: 8K nginx: 4K - 8K IIS: varies by version, 8K - 16K Tomcat: varies by version, 8K - 48K(?!) Ans it is not just with ChormePHP. Some user agents and some requests just get too big for web server defaults. It seems like a silly problem to run into, but it keeps happening. --- ChromePhp.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ChromePhp.php b/ChromePhp.php index ce4bf86..de05500 100755 --- a/ChromePhp.php +++ b/ChromePhp.php @@ -78,6 +78,11 @@ class ChromePhp */ const TABLE = 'table'; + /** + * @var int + */ + const HTTPD_HEADER_LIMIT = 8192; // 8Kb - Default for most HTTPD Servers + /** * @var int */ @@ -377,7 +382,25 @@ protected function _addRow(array $logs, $backtrace, $type) protected function _writeHeader($data) { - header(self::HEADER_NAME . ': ' . $this->_encode($data)); + $header = self::HEADER_NAME . ': ' . $this->_encode($data); + // Most HTTPD servers have a default header line length limit of 8kb, must test to avoid 500 Internal Server Error. + if (strlen($header) > self::HTTPD_HEADER_LIMIT) { + $data['rows'] = array(); + $data['rows'][] = array(array('ChromePHP Error: The HTML header will surpass the limit of '.$this->_formatSize(self::HTTPD_HEADER_LIMIT).' ('.$this->_formatSize(strlen($header)).') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache LimitRequestFieldsize directive'), '', self::ERROR); + $header = self::HEADER_NAME . ': ' . $this->_encode($data); + } + header($header); + } + + protected function _formatSize($arg) { + if ($arg>0){ + $j = 0; + $ext = array("bytes","Kb","Mb","Gb","Tb"); + while ($arg >= pow(1024,$j)) ++$j; { + $arg = (round($arg/pow(1024,$j-1)*100)/100).($ext[$j-1]); + } + return $arg; + } else return "0Kb"; } /** From d2c828d03a8cb9940e1160d8ca75590f6e4e8d62 Mon Sep 17 00:00:00 2001 From: "Theodore R. Smith" Date: Tue, 14 May 2019 23:30:57 -0500 Subject: [PATCH 2/2] Modern code formatting. --- ChromePhp.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/ChromePhp.php b/ChromePhp.php index de05500..382a435 100755 --- a/ChromePhp.php +++ b/ChromePhp.php @@ -385,22 +385,32 @@ protected function _writeHeader($data) $header = self::HEADER_NAME . ': ' . $this->_encode($data); // Most HTTPD servers have a default header line length limit of 8kb, must test to avoid 500 Internal Server Error. if (strlen($header) > self::HTTPD_HEADER_LIMIT) { - $data['rows'] = array(); - $data['rows'][] = array(array('ChromePHP Error: The HTML header will surpass the limit of '.$this->_formatSize(self::HTTPD_HEADER_LIMIT).' ('.$this->_formatSize(strlen($header)).') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache LimitRequestFieldsize directive'), '', self::ERROR); + $data['rows'] = []; + $data['rows'][] = [ + [ + 'ChromePHP Error: The HTML header will surpass the limit of ' . + $this->_formatSize(self::HTTPD_HEADER_LIMIT) . ' (' . $this->_formatSize(strlen($header)) . + ') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache ' . + 'LimitRequestFieldsize directive' + ], '', self::ERROR + ]; $header = self::HEADER_NAME . ': ' . $this->_encode($data); } header($header); } protected function _formatSize($arg) { - if ($arg>0){ + if ($arg > 0) { $j = 0; - $ext = array("bytes","Kb","Mb","Gb","Tb"); - while ($arg >= pow(1024,$j)) ++$j; { - $arg = (round($arg/pow(1024,$j-1)*100)/100).($ext[$j-1]); + $ext = ["bytes","Kb","Mb","Gb","Tb"]; + while ($arg >= pow(1024, $j)) ++$j; { + $arg = (round($arg / pow(1024, $j - 1) * 100) / 100).($ext[$j - 1]); } + return $arg; - } else return "0Kb"; + } + + return "0Kb"; } /**