Skip to content

Commit 171e152

Browse files
committed
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.
1 parent c3c2976 commit 171e152

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

ChromePhp.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class ChromePhp
7878
*/
7979
const TABLE = 'table';
8080

81+
/**
82+
* @var int
83+
*/
84+
const HTTPD_HEADER_LIMIT = 8192; // 8Kb - Default for most HTTPD Servers
85+
8186
/**
8287
* @var string
8388
*/
@@ -391,7 +396,25 @@ protected function _addRow(array $logs, $backtrace, $type)
391396

392397
protected function _writeHeader($data)
393398
{
394-
header(self::HEADER_NAME . ': ' . $this->_encode($data));
399+
$header = self::HEADER_NAME . ': ' . $this->_encode($data);
400+
// Most HTTPD servers have a default header line length limit of 8kb, must test to avoid 500 Internal Server Error.
401+
if (strlen($header) > self::HTTPD_HEADER_LIMIT) {
402+
$data['rows'] = array();
403+
$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);
404+
$header = self::HEADER_NAME . ': ' . $this->_encode($data);
405+
}
406+
header($header);
407+
}
408+
409+
protected function _formatSize($arg) {
410+
if ($arg>0){
411+
$j = 0;
412+
$ext = array("bytes","Kb","Mb","Gb","Tb");
413+
while ($arg >= pow(1024,$j)) ++$j; {
414+
$arg = (round($arg/pow(1024,$j-1)*100)/100).($ext[$j-1]);
415+
}
416+
return $arg;
417+
} else return "0Kb";
395418
}
396419

397420
/**

0 commit comments

Comments
 (0)