Skip to content

Commit

Permalink
Use $_SERVER if getallheaders() is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
drmad committed Feb 19, 2024
1 parent 338d9a7 commit 68061a8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Psr/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public function withoutAttribute($name): self
*/
public function setHeadersFromPHP(): self
{
// Si no existe getallheaders(), usamos otro método para obtener las
// cabeceras
if (!function_exists('getallheaders')) {
return $this->setHeadersFromDollarServer();
}

foreach (getallheaders() as $name => $value) {
$lc_name = strtolower($name);
$this->headers[$name][] = $value;
Expand All @@ -120,4 +126,27 @@ public function setHeadersFromPHP(): self
return $this;
}

/**
* Sets the HTTP headers from $_SERVER, reverting the CGI name manipulation.
*
* Section 4.1.18 from (RFC 3875)[http://www.faqs.org/rfcs/rfc3875.html] defines
* how a client request header has to be changed.
*/
public function setHeadersFromDollarServer()
{
foreach ($_SERVER as $header => $value) {
if (str_starts_with($header, 'HTTP_')) {
// Estos son las cabeceras de la petición. Las convertimos de
// HTTP_NICE_HEADER to Nice-Header. No es obligatorio, pero se ve
$parts = explode('_', substr($header, 5));
$name = join('-', array_map(fn($part) => ucfirst(strtolower($part)), $parts));

$lc_name = strtolower($name);
$this->headers[$name][] = $value;
$this->header_case_map[$lc_name] = $name;
}
}

return $this;
}
}

0 comments on commit 68061a8

Please sign in to comment.