File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 *
You can’t perform that action at this time.
0 commit comments