Skip to content

Commit f6572e3

Browse files
committed
closer to swoole
1 parent 8e12530 commit f6572e3

2 files changed

Lines changed: 40 additions & 55 deletions

File tree

src/Http/Adapter/Swoole/Request.php

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public function setServer(string $key, string $value): static
6767
/**
6868
* Get IP
6969
*
70-
* Returns users IP address.
71-
* Support HTTP_X_FORWARDED_FOR header usually return
72-
* from different proxy servers or PHP default REMOTE_ADDR
70+
* Extracts the client's IP address from trusted headers or falls back to the remote address.
71+
* Prioritizes headers like X-Forwarded-For when behind proxies or load balancers,
72+
* defaulting to REMOTE_ADDR when trusted headers are unavailable.
73+
*
74+
* @return string The validated client IP address or '0.0.0.0' if unavailable
7375
*/
7476
public function getIP(): string
7577
{
76-
$remoteAddr = $this->getServer('remote_addr') ?? '0.0.0.0';
78+
$remoteAddr = $this->getServer('REMOTE_ADDR') ?? '0.0.0.0';
7779

7880
foreach ($this->trustedIpHeaders as $header) {
7981
$headerValue = $this->getHeader($header);
@@ -83,11 +85,11 @@ public function getIP(): string
8385
}
8486

8587
// Leftmost IP address is the address of the originating client
86-
$ips = \explode(',', $headerValue);
87-
$ip = \trim($ips[0]);
88+
$ips = explode(',', $headerValue);
89+
$ip = trim($ips[0]);
8890

8991
// Validate IP format (supports both IPv4 and IPv6)
90-
if (\filter_var($ip, FILTER_VALIDATE_IP)) {
92+
if (filter_var($ip, FILTER_VALIDATE_IP)) {
9193
return $ip;
9294
}
9395
}
@@ -270,29 +272,9 @@ public function getFiles($key): array
270272
*/
271273
public function getCookie(string $key, string $default = ''): string
272274
{
273-
$key = \strtolower($key);
274-
275-
$cookieHeader = $this->getHeader('cookie', '');
276-
if ($cookieHeader === '') {
277-
return $default;
278-
}
279-
280-
$cookies = \explode(';', $cookieHeader);
281-
foreach ($cookies as $cookie) {
282-
$cookie = \trim($cookie);
283-
if ($cookie === '') {
284-
continue;
285-
}
286-
287-
$parts = \explode('=', $cookie, 2);
288-
$cookieKey = \trim($parts[0] ?? '');
289-
$cookieValue = \trim($parts[1] ?? '');
290-
if ($cookieKey === $key) {
291-
return $cookieValue;
292-
}
293-
}
275+
$key = strtolower($key);
294276

295-
return $default;
277+
return $this->swoole->cookie[$key] ?? $default;
296278
}
297279

298280
/**
@@ -338,11 +320,6 @@ public function removeHeader(string $key): static
338320
return $this;
339321
}
340322

341-
public function getSwooleRequest(): SwooleRequest
342-
{
343-
return $this->swoole;
344-
}
345-
346323
/**
347324
* Generate input
348325
*
@@ -396,12 +373,6 @@ protected function generateInput(): array
396373
*/
397374
protected function generateHeaders(): array
398375
{
399-
$headers = $this->swoole->header;
400-
401-
foreach ($headers as $key => $value) {
402-
$headers[strtolower($key)] = $value;
403-
}
404-
405-
return $headers;
376+
return $this->swoole->header;
406377
}
407378
}

src/Http/Adapter/Swoole/Response.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Response extends UtopiaResponse
1212
*
1313
* @var SwooleResponse
1414
*/
15-
protected $swoole;
15+
protected SwooleResponse $swoole;
1616

1717
/**
1818
* Response constructor.
@@ -23,11 +23,6 @@ public function __construct(SwooleResponse $response)
2323
parent::__construct(\microtime(true));
2424
}
2525

26-
public function getSwooleResponse(): SwooleResponse
27-
{
28-
return $this->swoole;
29-
}
30-
3126
/**
3227
* Write
3328
*
@@ -50,6 +45,25 @@ public function end(?string $content = null): void
5045
$this->swoole->end($content);
5146
}
5247

48+
/**
49+
* Get status code reason
50+
*
51+
* Get HTTP response status code reason from available options. If status code is unknown an exception will be thrown.
52+
*
53+
* @param int $code
54+
* @return string
55+
*
56+
* @throws \Exception
57+
*/
58+
protected function getStatusCodeReason(int $code): string
59+
{
60+
if (!\array_key_exists($code, $this->statusCodes)) {
61+
throw new \Exception('Unknown HTTP status code');
62+
}
63+
64+
return $this->statusCodes[$code];
65+
}
66+
5367
/**
5468
* Send Status Code
5569
*
@@ -58,7 +72,7 @@ public function end(?string $content = null): void
5872
*/
5973
protected function sendStatus(int $statusCode): void
6074
{
61-
$this->swoole->status($statusCode);
75+
$this->swoole->status((string) $statusCode, $this->getStatusCodeReason($statusCode));
6276
}
6377

6478
/**
@@ -87,13 +101,13 @@ protected function sendCookie(string $name, string $value, array $options): void
87101
{
88102
$this->swoole->cookie(
89103
$name,
90-
$value,
91-
$options['expire'] ?? 0,
92-
$options['path'] ?? '',
93-
$options['domain'] ?? '',
94-
$options['secure'] ?? false,
95-
$options['httponly'] ?? false,
96-
$options['samesite'] ?? ''
104+
value: $value,
105+
expires: $options['expire'] ?? 0,
106+
path: $options['path'] ?? '',
107+
domain: $options['domain'] ?? '',
108+
secure: $options['secure'] ?? false,
109+
httponly: $options['httponly'] ?? false,
110+
samesite: $options['samesite'] ?? false,
97111
);
98112
}
99113
}

0 commit comments

Comments
 (0)