Skip to content

Commit 0ac6ec5

Browse files
authored
Merge pull request #2 from phpexpertsinc/dulldusk_master
HTTPD default 8Kb header limit check, to avoid 500 Internal Server Error
2 parents 0c5debc + d2c828d commit 0ac6ec5

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

ChromePhp.php

Lines changed: 34 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,35 @@ 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'] = [];
389+
$data['rows'][] = [
390+
[
391+
'ChromePHP Error: The HTML header will surpass the limit of ' .
392+
$this->_formatSize(self::HTTPD_HEADER_LIMIT) . ' (' . $this->_formatSize(strlen($header)) .
393+
') - You can increase the HTTPD_HEADER_LIMIT on ChromePHP class, according to your Apache ' .
394+
'LimitRequestFieldsize directive'
395+
], '', self::ERROR
396+
];
397+
$header = self::HEADER_NAME . ': ' . $this->_encode($data);
398+
}
399+
header($header);
400+
}
401+
402+
protected function _formatSize($arg) {
403+
if ($arg > 0) {
404+
$j = 0;
405+
$ext = ["bytes","Kb","Mb","Gb","Tb"];
406+
while ($arg >= pow(1024, $j)) ++$j; {
407+
$arg = (round($arg / pow(1024, $j - 1) * 100) / 100).($ext[$j - 1]);
408+
}
409+
410+
return $arg;
411+
}
412+
413+
return "0Kb";
381414
}
382415

383416
/**

0 commit comments

Comments
 (0)