Skip to content

Commit

Permalink
HTTPD default 8Kb header limit check, to avoid 500 Internal Server Error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dulldusk committed May 14, 2017
1 parent c3c2976 commit 171e152
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ChromePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class ChromePhp
*/
const TABLE = 'table';

/**
* @var int
*/
const HTTPD_HEADER_LIMIT = 8192; // 8Kb - Default for most HTTPD Servers

/**
* @var string
*/
Expand Down Expand Up @@ -391,7 +396,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);

This comment has been minimized.

Copy link
@tabathap

tabathap Aug 30, 2019

// 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";
}

/**
Expand Down

0 comments on commit 171e152

Please sign in to comment.