Skip to content

Commit 4700d38

Browse files
committed
Improve cookie handling
1 parent 9e8cd49 commit 4700d38

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/Http/Adapter/Swoole/Request.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,21 @@ public function getFiles($key): array
253253
*/
254254
public function getCookie(string $key, string $default = ''): string
255255
{
256-
$key = strtolower($key);
256+
$key = \strtolower($key);
257+
258+
$cookies = \explode(';', $this->getHeader('cookie', ''));
259+
foreach ($cookies as $cookie) {
260+
$cookie = \trim($cookie);
261+
[$cookieKey, $cookieValue] = \explode('=', $cookie, 2);
262+
$cookieKey = \trim($cookieKey);
263+
$cookieKey = \strtolower($cookieKey);
264+
$cookieValue = \trim($cookieValue);
265+
if ($cookieValue === $key) {
266+
return $cookieValue;
267+
}
268+
}
257269

258-
return $this->swoole->cookie[$key] ?? $default;
270+
return $default;
259271
}
260272

261273
/**
@@ -361,17 +373,8 @@ protected function generateHeaders(): array
361373
{
362374
$headers = $this->swoole->header;
363375

364-
if (empty($this->swoole->cookie)) {
365-
return $headers;
366-
}
367-
368-
$cookieHeaders = [];
369-
foreach ($this->swoole->cookie as $key => $value) {
370-
$cookieHeaders[] = "{$key}={$value}";
371-
}
372-
373-
if (!empty($cookieHeaders)) {
374-
$headers['cookie'] = \implode('; ', $cookieHeaders);
376+
foreach ($headers as $key => $value) {
377+
$headers[strtolower($key)] = $value;
375378
}
376379

377380
return $headers;

src/Http/Adapter/Swoole/Server.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public function __construct(string $host, ?string $port = null, array $settings
1919
{
2020
$this->server = new SwooleServer($host, $port);
2121
$this->server->set(\array_merge($settings, [
22-
'enable_coroutine' => true
22+
'enable_coroutine' => true,
23+
'http_parse_cookie' => false,
2324
]));
2425
}
2526

tests/e2e/BaseTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,26 @@ public function testFile()
3838

3939
public function testCookie()
4040
{
41+
$cookie = 'cookie1=value1';
4142
$response = $this->client->call(Client::METHOD_GET, '/cookies', [
42-
'Cookie: cookie1=value1; cookie2=value2'
43+
'Cookie: ' . $cookie
4344
]);
45+
$this->assertEquals(200, $response['headers']['status-code']);
46+
$this->assertEquals($cookie, $response['body']);
47+
48+
$cookie = 'cookie1=value1; cookie2=value2';
49+
$response = $this->client->call(Client::METHOD_GET, '/cookies', [
50+
'Cookie: ' . $cookie
51+
]);
52+
$this->assertEquals(200, $response['headers']['status-code']);
53+
$this->assertEquals($cookie, $response['body']);
54+
4455

56+
$cookie = 'cookie1=value1;cookie2=value2';
57+
$response = $this->client->call(Client::METHOD_GET, '/cookies', [
58+
'Cookie: ' . $cookie
59+
]);
4560
$this->assertEquals(200, $response['headers']['status-code']);
46-
$this->assertEquals('cookie1=value1; cookie2=value2', $response['body']);
61+
$this->assertEquals($cookie, $response['body']);
4762
}
4863
}

0 commit comments

Comments
 (0)