Skip to content

Commit

Permalink
feat: add check if headers() returns array
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 22, 2023
1 parent 7852e3f commit a769e53
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 17 deletions.
33 changes: 26 additions & 7 deletions app/Views/errors/html/error_exception.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use CodeIgniter\HTTP\Header;
use Config\Services;
use CodeIgniter\CodeIgniter;

Expand Down Expand Up @@ -289,10 +290,20 @@
</tr>
</thead>
<tbody>
<?php foreach ($headers as $header) : ?>
<?php foreach ($headers as $name => $value) : ?>
<tr>
<td><?= esc($header->getName(), 'html') ?></td>
<td><?= esc($header->getValueLine(), 'html') ?></td>
<td><?= esc($name, 'html') ?></td>
<td>
<?php
if ($value instanceof Header) {
echo esc($value->getValueLine(), 'html');
} else {
foreach ($value as $i => $header) {
echo ' ('. $i+1 . ') ' . esc($header->getValueLine(), 'html');
}
}
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Expand All @@ -316,8 +327,6 @@

<?php $headers = $response->headers(); ?>
<?php if (! empty($headers)) : ?>
<?php natsort($headers) ?>

<h3>Headers</h3>

<table>
Expand All @@ -328,10 +337,20 @@
</tr>
</thead>
<tbody>
<?php foreach (array_keys($headers) as $name) : ?>
<?php foreach ($headers as $name => $value) : ?>
<tr>
<td><?= esc($name, 'html') ?></td>
<td><?= esc($response->getHeaderLine($name), 'html') ?></td>
<td>
<?php
if ($value instanceof Header) {
echo esc($response->getHeaderLine($name), 'html');
} else {
foreach ($value as $i => $header) {
echo ' ('. $i+1 . ') ' . esc($header->getValueLine(), 'html');
}
}
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
11 changes: 9 additions & 2 deletions system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace CodeIgniter\Cache;

use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\Header;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Cache as CacheConfig;
Expand Down Expand Up @@ -99,8 +100,14 @@ public function make($request, ResponseInterface $response): bool

$headers = [];

foreach ($response->headers() as $header) {
$headers[$header->getName()] = $header->getValueLine();
foreach ($response->headers() as $name => $value) {
if ($value instanceof Header) {
$headers[$name] = $value->getValueLine();
} else {
foreach ($value as $header) {
$headers[$name][] = $header->getValueLine();
}
}
}

return $this->cache->save(
Expand Down
25 changes: 21 additions & 4 deletions system/Debug/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Format\JSONFormatter;
use CodeIgniter\Format\XMLFormatter;
use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\Header;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
Expand Down Expand Up @@ -140,8 +141,16 @@ public function run(float $startTime, float $totalTime, RequestInterface $reques
$data['vars']['post'][esc($name)] = is_array($value) ? '<pre>' . esc(print_r($value, true)) . '</pre>' : esc($value);
}

foreach ($request->headers() as $header) {
$data['vars']['headers'][esc($header->getName())] = esc($header->getValueLine());
foreach ($request->headers() as $name => $value) {
if ($value instanceof Header) {
$data['vars']['headers'][esc($name)] = esc($value->getValueLine());
} else {
foreach ($value as $i => $header) {
$data['vars']['headers'][esc($name)] ??= '';
$data['vars']['headers'][esc($name)] .= ' (' . $i + 1 . ') '
. esc($header->getValueLine());
}
}
}

foreach ($request->getCookie() as $name => $value) {
Expand All @@ -157,8 +166,16 @@ public function run(float $startTime, float $totalTime, RequestInterface $reques
'headers' => [],
];

foreach ($response->headers() as $header) {
$data['vars']['response']['headers'][esc($header->getName())] = esc($header->getValueLine());
foreach ($response->headers() as $name => $value) {
if ($value instanceof Header) {
$data['vars']['response']['headers'][esc($name)] = esc($value->getValueLine());
} else {
foreach ($value as $i => $header) {
$data['vars']['response']['headers'][esc($name)] ??= '';
$data['vars']['response']['headers'][esc($name)] .= ' (' . $i + 1 . ') '
. esc($header->getValueLine());
}
}
}

$data['config'] = Config::display();
Expand Down
10 changes: 8 additions & 2 deletions system/HTTP/RedirectResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ public function withCookies()
*/
public function withHeaders()
{
foreach (Services::response()->headers() as $name => $header) {
$this->setHeader($name, $header->getValue());
foreach (Services::response()->headers() as $name => $value) {
if ($value instanceof Header) {
$this->setHeader($name, $value->getValue());
} else {
foreach ($value as $header) {
$this->addHeader($name, $header->getValue());
}
}
}

return $this;
Expand Down
18 changes: 16 additions & 2 deletions system/HTTP/ResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,22 @@ public function sendHeaders()
header(sprintf('HTTP/%s %s %s', $this->getProtocolVersion(), $this->getStatusCode(), $this->getReasonPhrase()), true, $this->getStatusCode());

// Send all of our headers
foreach (array_keys($this->headers()) as $name) {
header($name . ': ' . $this->getHeaderLine($name), false, $this->getStatusCode());
foreach ($this->headers() as $name => $value) {
if ($value instanceof Header) {
header(
$name . ': ' . $value->getValueLine(),
false,
$this->getStatusCode()
);
} else {
foreach ($value as $header) {
header(
$name . ': ' . $header->getValueLine(),
false,
$this->getStatusCode()
);
}
}
}

return $this;
Expand Down

0 comments on commit a769e53

Please sign in to comment.