Skip to content

Commit

Permalink
Add logic to handle single-use headers
Browse files Browse the repository at this point in the history
The code now includes a mechanism to handle HTTP headers that are allowed to be used only once. This is done by storing such headers separately and checking their sense status before sending them again. This will prevent any issues arising from multiple instances of the same header.

https://stackoverflow.com/questions/15682496/will-duplicate-access-control-allow-origin-headers-break-cors
  • Loading branch information
nusphere committed Mar 4, 2024
1 parent b117518 commit 7d1bba9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/JK/RestServer/HeaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ final class HeaderManager
/** @var array Colletion of HTTP headers */
private $headers = array();

/** These headers are only once allowed */
private $singleHeader = [
"Access-Control-Allow-Origin"
];

/**
* Sets some defaults
*/
Expand Down Expand Up @@ -121,7 +126,23 @@ public function sendAllHeaders()
}

foreach ($this->headers as $name => $value) {
header($name . ': ' . $value);
if (!$this->isSingleHeaderAndNotAlreadySent($name)) {
header($name . ': ' . $value);
}
}
}

/**
* Bugfix: Remove duplicates of headers which are required to be single
*
* https://stackoverflow.com/questions/15682496/will-duplicate-access-control-allow-origin-headers-break-cors
*/
private function isSingleHeaderAndNotAlreadySent(string $name): bool
{
if (!in_array($name, $this->singleHeader)) {
return false;
}

return array_key_exists($name, array_keys(headers_list()));
}
}

0 comments on commit 7d1bba9

Please sign in to comment.