Skip to content

Commit 4c34ff5

Browse files
committed
feat: add native header detection to DebugToolbar
1 parent 455068a commit 4c34ff5

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

system/Debug/Toolbar.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,11 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
372372
* @var IncomingRequest|null $request
373373
*/
374374
if (CI_DEBUG && ! is_cli()) {
375+
376+
if ($this->hasNativeHeaderConflict()) {
377+
return;
378+
}
379+
375380
$app = service('codeigniter');
376381

377382
$request ??= service('request');
@@ -544,6 +549,32 @@ protected function format(string $data, string $format = 'html'): string
544549
return $output;
545550
}
546551

552+
/**
553+
* Checks if the native PHP headers indicate a non-HTML response
554+
* or if headers are already sent.
555+
*/
556+
protected function hasNativeHeaderConflict(): bool
557+
{
558+
// If headers are sent, we can't inject HTML.
559+
if (headers_sent()) {
560+
return true;
561+
}
562+
563+
// Native Header Inspection
564+
foreach (headers_list() as $header) {
565+
// Content-Type is set but is NOT text/html
566+
if (str_starts_with(strtolower($header), strtolower('Content-Type:')) && ! str_contains(strtolower($header), strtolower('text/html'))) {
567+
return true;
568+
}
569+
// File is being downloaded (Attachment)
570+
if (str_starts_with(strtolower($header), strtolower('Content-Disposition:')) && str_contains(strtolower($header), strtolower('attachment'))) {
571+
return true;
572+
}
573+
}
574+
575+
return false;
576+
}
577+
547578
/**
548579
* Determine if the toolbar should be disabled based on the request headers.
549580
*

0 commit comments

Comments
 (0)