1
0
Fork 0
mirror of synced 2024-07-03 13:41:01 +12:00

Move cookie-header logic to request

This commit is contained in:
Matej Bačo 2023-10-27 17:23:43 +02:00
parent b2d385944a
commit 71807635b3
2 changed files with 39 additions and 10 deletions

View file

@ -117,16 +117,7 @@ function router(App $utopia, Database $dbForConsole, SwooleRequest $swooleReques
$path .= '?' . $query;
}
$swooleHeaders = $swooleRequest->header;
$cookieHeaders = [];
foreach ($swooleRequest->cookie as $key => $value) {
$cookieHeaders[] = "{$key}={$value}";
}
if (!empty($cookieHeaders)) {
$swooleHeaders['cookie'] = \implode('; ', $cookieHeaders);
}
$swooleHeaders = $request->getHeaders();
$body = \json_encode([
'async' => false,

View file

@ -95,4 +95,42 @@ class Request extends UtopiaRequest
{
return self::$route != null;
}
/**
* Get headers
*
* Method for getting all HTTP header parameters, including cookies.
*
* @return array<string,mixed>
*/
public function getHeaders(): array
{
$headers = $this->generateHeaders();
$cookieHeaders = [];
foreach ($this->swoole->cookie as $key => $value) {
$cookieHeaders[] = "{$key}={$value}";
}
if (!empty($cookieHeaders)) {
$headers['cookie'] = \implode('; ', $cookieHeaders);
}
return $headers;
}
/**
* Get header
*
* Method for querying HTTP header parameters. If $key is not found $default value will be returned.
*
* @param string $key
* @param string $default
* @return string
*/
public function getHeader(string $key, string $default = ''): string
{
$headers = $this->getHeaders();
return $headers[$key] ?? $default;
}
}