Skip to content

Commit ad7139f

Browse files
dullduskhopeseekr
authored andcommitted
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 0c5debc commit ad7139f

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 int
8388
*/
@@ -377,7 +382,25 @@ protected function _addRow(array $logs, $backtrace, $type)
377382

378383
protected function _writeHeader($data)
379384
{
380-
header(self::HEADER_NAME . ': ' . $this->_encode($data));
385+
$header = self::HEADER_NAME . ': ' . $this->_encode($data);
386+
// Most HTTPD servers have a default header line length limit of 8kb, must test to avoid 500 Internal Server Error.
387+
if (strlen($header) > self::HTTPD_HEADER_LIMIT) {
388+
$data['rows'] = array();
389+
$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);
390+
$header = self::HEADER_NAME . ': ' . $this->_encode($data);
391+
}
392+
header($header);
393+
}
394+
395+
protected function _formatSize($arg) {
396+
if ($arg>0){
397+
$j = 0;
398+
$ext = array("bytes","Kb","Mb","Gb","Tb");
399+
while ($arg >= pow(1024,$j)) ++$j; {
400+
$arg = (round($arg/pow(1024,$j-1)*100)/100).($ext[$j-1]);
401+
}
402+
return $arg;
403+
} else return "0Kb";
381404
}
382405

383406
/**

0 commit comments

Comments
 (0)