diff --git a/src/Appwrite/Utopia/Request.php b/src/Appwrite/Utopia/Request.php index ba832fb229..be23c2f211 100644 --- a/src/Appwrite/Utopia/Request.php +++ b/src/Appwrite/Utopia/Request.php @@ -177,6 +177,32 @@ class Request extends UtopiaRequest return $this->route !== null; } + /** + * Get headers + * + * Method for getting all HTTP headers, including a synthesized `cookie` + * header. Swoole parses the incoming Cookie header into its own cookie jar, + * so it is absent from the raw header map; rebuild it here so consumers that + * forward request headers (e.g. function/site executions) still receive it. + * + * @return array> + */ + public function getHeaders(): array + { + $headers = parent::getHeaders(); + + $cookies = $this->getCookieParams(); + if (!empty($cookies)) { + $pairs = []; + foreach ($cookies as $key => $value) { + $pairs[] = "{$key}={$value}"; + } + $headers['cookie'] = [\implode('; ', $pairs)]; + } + + return $headers; + } + /** * Get User Agent * diff --git a/tests/unit/Utopia/RequestTest.php b/tests/unit/Utopia/RequestTest.php index 5594a906a5..476c5e232f 100644 --- a/tests/unit/Utopia/RequestTest.php +++ b/tests/unit/Utopia/RequestTest.php @@ -187,6 +187,30 @@ final class RequestTest extends TestCase $this->assertSame('https://a.example, https://b.example', $request->getHeaderLine('referer')); } + public function testGetHeadersSynthesizesCookieHeaderFromCookieJar(): void + { + // Swoole parses the Cookie header into its cookie jar, so getHeaders() + // must rebuild it for consumers that forward request headers onward + // (e.g. function/site executions). + $swoole = new SwooleRequest(); + $swoole->header = ['host' => 'example.com']; + $swoole->cookie = ['custom-session-id' => 'abcd123', 'custom-user-id' => 'efgh456']; + $request = new Request($swoole); + + $headers = $request->getHeaders(); + + $this->assertSame(['custom-session-id=abcd123; custom-user-id=efgh456'], $headers['cookie']); + } + + public function testGetHeadersOmitsCookieHeaderWhenNoCookies(): void + { + $swoole = new SwooleRequest(); + $swoole->header = ['host' => 'example.com']; + $request = new Request($swoole); + + $this->assertArrayNotHasKey('cookie', $request->getHeaders()); + } + public function testGetParamsCachesRawParamsWhenFilterThrows4xx(): void { /*