Skip to content

Commit 9e8cd49

Browse files
authored
Merge pull request #178 from utopia-php/fix-cookie-headers
Fix: Cookie headers
2 parents 349d403 + 9d62aad commit 9e8cd49

File tree

18 files changed

+373
-306
lines changed

18 files changed

+373
-306
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"lint": "vendor/bin/pint --test",
2626
"format": "vendor/bin/pint",
27-
"check": "vendor/bin/phpstan analyse -c phpstan.neon",
27+
"check": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit 512M",
2828
"test": "vendor/bin/phpunit --configuration phpunit.xml",
2929
"bench": "vendor/bin/phpbench run --report=benchmark"
3030
},
@@ -34,9 +34,9 @@
3434
},
3535
"require-dev": {
3636
"phpunit/phpunit": "^9.5.25",
37-
"laravel/pint": "^1.2",
37+
"laravel/pint": "1.*",
3838
"swoole/ide-helper": "4.8.3",
39-
"phpstan/phpstan": "^1.10",
39+
"phpstan/phpstan": "1.*",
4040
"phpbench/phpbench": "^1.2"
4141
}
4242
}

composer.lock

Lines changed: 301 additions & 268 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Http/Adapter/FPM/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function getRawPayload(): string
3636
* @param string|null $default
3737
* @return string|null
3838
*/
39-
public function getServer(string $key, string $default = null): ?string
39+
public function getServer(string $key, ?string $default = null): ?string
4040
{
4141
return $_SERVER[$key] ?? $default;
4242
}

src/Http/Adapter/FPM/Response.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function write(string $content): bool
2525
*
2626
* Send optional content and end
2727
*
28-
* @param string $content
28+
* @param string|null $content
2929
* @return void
3030
*/
31-
public function end(string $content = null): void
31+
public function end(?string $content = null): void
3232
{
3333
if (!is_null($content)) {
3434
echo $content;

src/Http/Adapter/Swoole/Request.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getRawPayload(): string
4343
* @param string|null $default
4444
* @return string|null
4545
*/
46-
public function getServer(string $key, string $default = null): ?string
46+
public function getServer(string $key, ?string $default = null): ?string
4747
{
4848
return $this->swoole->server[$key] ?? $default;
4949
}
@@ -359,6 +359,21 @@ protected function generateInput(): array
359359
*/
360360
protected function generateHeaders(): array
361361
{
362-
return $this->swoole->header;
362+
$headers = $this->swoole->header;
363+
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);
375+
}
376+
377+
return $headers;
363378
}
364379
}

src/Http/Adapter/Swoole/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function write(string $content): bool
4545
* @param string|null $content
4646
* @return void
4747
*/
48-
public function end(string $content = null): void
48+
public function end(?string $content = null): void
4949
{
5050
$this->swoole->end($content);
5151
}

src/Http/Adapter/Swoole/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Server extends Adapter
1515
{
1616
protected SwooleServer $server;
1717

18-
public function __construct(string $host, string $port = null, array $settings = [])
18+
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, [
@@ -42,7 +42,7 @@ public function onStart(callable $callback)
4242

4343
public function start()
4444
{
45-
if(Coroutine::getCid() === -1) {
45+
if (Coroutine::getCid() === -1) {
4646
run(fn () => $this->server->start());
4747
} else {
4848
$this->server->start();

src/Http/Files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function getCount(): int
8383
*
8484
* @throws \Exception
8585
*/
86-
public function load(string $directory, string $root = null): void
86+
public function load(string $directory, ?string $root = null): void
8787
{
8888
if (!is_readable($directory)) {
8989
throw new Exception("Failed to load directory: {$directory}");

src/Http/Http.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public static function error(): Hook
295295
* @param string|null $default
296296
* @return string|null
297297
*/
298-
public static function getEnv(string $key, string $default = null): ?string
298+
public static function getEnv(string $key, ?string $default = null): ?string
299299
{
300300
return $_SERVER[$key] ?? $default;
301301
}
@@ -366,7 +366,7 @@ public function getResource(string $name, string $context = 'utopia', bool $fres
366366
$this->resources[$context] ??= [];
367367

368368
$resourcesCallback = &self::$resourcesCallbacks[$context] ?? [];
369-
if(empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
369+
if (empty($resourcesCallback) || !\array_key_exists($name, $resourcesCallback)) {
370370
$resourcesCallback = &self::$resourcesCallbacks['utopia'];
371371
}
372372

@@ -514,7 +514,7 @@ public static function addRoute(string $method, string $url): Route
514514
*
515515
* @throws \Exception
516516
*/
517-
public function loadFiles(string $directory, string $root = null): void
517+
public function loadFiles(string $directory, ?string $root = null): void
518518
{
519519
$this->files->load($directory, $root);
520520
}
@@ -576,7 +576,7 @@ public function start()
576576
try {
577577
$this->run($request, $response, $context);
578578
} finally {
579-
if(isset(self::$resourcesCallbacks[$context])) {
579+
if (isset(self::$resourcesCallbacks[$context])) {
580580
unset(self::$resourcesCallbacks[$context]);
581581
}
582582
}
@@ -593,7 +593,7 @@ public function start()
593593
$arguments = $this->getArguments($hook, 'utopia', [], []);
594594
\call_user_func_array($hook->getAction(), $arguments);
595595
}
596-
} catch(\Exception $e) {
596+
} catch (\Exception $e) {
597597
self::setResource('error', fn () => $e);
598598

599599
foreach (self::$errors as $error) { // Global error hooks
@@ -793,7 +793,7 @@ public function run(Request $request, Response $response, string $context): stat
793793
$arguments = $this->getArguments($hook, $context, [], []);
794794
\call_user_func_array($hook->getAction(), $arguments);
795795
}
796-
} catch(\Exception $e) {
796+
} catch (\Exception $e) {
797797
self::setResource('error', fn () => $e, [], $context);
798798

799799
foreach (self::$errors as $error) { // Global error hooks

src/Http/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ abstract public function getRawPayload(): string;
124124
* @param string|null $default
125125
* @return string|null
126126
*/
127-
abstract public function getServer(string $key, string $default = null): ?string;
127+
abstract public function getServer(string $key, ?string $default = null): ?string;
128128

129129
/**
130130
* Set server

0 commit comments

Comments
 (0)