-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathResponse.php
More file actions
248 lines (217 loc) · 6.9 KB
/
Response.php
File metadata and controls
248 lines (217 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace Utopia\Http\Adapter\Swoole;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Server as SwooleServer;
use Utopia\Http\Response as UtopiaResponse;
class Response extends UtopiaResponse
{
/**
* Swoole Response Object
*
* @var SwooleResponse
*/
protected SwooleResponse $swoole;
/**
* Swoole HTTP Server for raw TCP sends after detach().
*/
protected ?SwooleServer $server = null;
/**
* Response constructor.
*/
public function __construct(SwooleResponse $response)
{
$this->swoole = $response;
parent::__construct(\microtime(true));
}
/**
* Set the Swoole HTTP Server instance.
*
* Required for stream() to use detach() + server->send() for
* sending responses with Content-Length and streaming body.
*/
public function setSwooleServer(SwooleServer $server): void
{
$this->server = $server;
}
/**
* Write
*
* @param string $content
* @return bool False if write cannot complete, such as request ended by client
*/
public function write(string $content): bool
{
return $this->swoole->write($content);
}
/**
* End
*
* @param string|null $content
* @return void
*/
public function end(?string $content = null): void
{
$this->swoole->end($content);
}
/**
* Stream a large response body with Content-Length.
*
* Overrides the base implementation to use Swoole's detach() +
* $server->send() pattern. This bypasses Swoole's forced chunked
* Transfer-Encoding, allowing Content-Length to be sent with a
* streaming body so browsers can show download progress.
*
* @param callable(int, int): string $reader fn($offset, $length) returns chunk data
* @param int $totalSize Total response body size in bytes
*/
public function stream(callable $reader, int $totalSize): void
{
if ($this->sent) {
return;
}
// Fallback to base implementation if server not available
if ($this->server === null) {
parent::stream($reader, $totalSize);
return;
}
$this->sent = true;
if ($this->disablePayload) {
$this->appendCookies()->appendHeaders();
$this->end();
return;
}
// Build raw HTTP response with Content-Length
$this->addHeader('Content-Length', (string) $totalSize, override: true);
$this->addHeader('Connection', 'close', override: true);
$this->addHeader('X-Debug-Speed', (string) (\microtime(true) - $this->startTime), override: true);
$serverHeader = $this->headers['Server'] ?? 'Utopia/Http';
$this->addHeader('Server', $serverHeader, override: true);
if (!empty($this->contentType)) {
$this->addHeader('Content-Type', $this->contentType, override: true);
}
$statusCode = $this->getStatusCode();
$reason = $this->statusCodes[$statusCode] ?? 'Unknown';
$raw = "HTTP/1.1 {$statusCode} {$reason}\r\n";
foreach ($this->headers as $key => $value) {
if (\is_array($value)) {
foreach ($value as $v) {
$raw .= "{$key}: {$v}\r\n";
}
} else {
$raw .= "{$key}: {$value}\r\n";
}
}
foreach ($this->cookies as $cookie) {
$raw .= 'Set-Cookie: ' . $this->buildSetCookieHeader($cookie) . "\r\n";
}
$raw .= "\r\n";
// Detach from Swoole's HTTP layer and send raw TCP
$fd = $this->swoole->fd;
$this->swoole->detach();
if ($this->server->send($fd, $raw) === false) {
$this->server->close($fd);
$this->disablePayload();
return;
}
// Stream body in 2MB chunks
$chunkSize = 2 * 1024 * 1024;
for ($offset = 0; $offset < $totalSize; $offset += $chunkSize) {
$length = \min($chunkSize, $totalSize - $offset);
$data = $reader($offset, $length);
if ($this->server->send($fd, $data) === false) {
break;
}
unset($data);
}
$this->server->close($fd);
$this->disablePayload();
}
/**
* Build a Set-Cookie header string from a cookie array.
*/
private function buildSetCookieHeader(array $cookie): string
{
$parts = [\urlencode($cookie['name']) . '=' . \urlencode($cookie['value'] ?? '')];
if (!empty($cookie['expire'])) {
$parts[] = 'Expires=' . \gmdate('D, d M Y H:i:s T', $cookie['expire']);
$parts[] = 'Max-Age=' . \max(0, $cookie['expire'] - \time());
}
if (!empty($cookie['path'])) {
$parts[] = 'Path=' . $cookie['path'];
}
if (!empty($cookie['domain'])) {
$parts[] = 'Domain=' . $cookie['domain'];
}
if (!empty($cookie['secure'])) {
$parts[] = 'Secure';
}
if (!empty($cookie['httponly'])) {
$parts[] = 'HttpOnly';
}
if (!empty($cookie['samesite'])) {
$parts[] = 'SameSite=' . $cookie['samesite'];
}
return \implode('; ', $parts);
}
/**
* Get status code reason
*
* Get HTTP response status code reason from available options. If status code is unknown an exception will be thrown.
*
* @param int $code
* @return string
*
* @throws \Exception
*/
protected function getStatusCodeReason(int $code): string
{
if (!\array_key_exists($code, $this->statusCodes)) {
throw new \Exception('Unknown HTTP status code');
}
return $this->statusCodes[$code];
}
/**
* Send Status Code
*
* @param int $statusCode
* @return void
*/
protected function sendStatus(int $statusCode): void
{
$this->swoole->status((string) $statusCode, $this->getStatusCodeReason($statusCode));
}
/**
* Send Header
*
* @param string $key
* @param string|array<string> $value
* @return void
*/
public function sendHeader(string $key, mixed $value): void
{
$this->swoole->header($key, $value);
}
/**
* Send Cookie
*
* Send a cookie
*
* @param string $name
* @param string $value
* @param array<string, mixed> $options
* @return void
*/
protected function sendCookie(string $name, string $value, array $options): void
{
$this->swoole->cookie(
$name,
value: $value,
expires: $options['expire'] ?? 0,
path: $options['path'] ?? '',
domain: $options['domain'] ?? '',
secure: $options['secure'] ?? false,
httponly: $options['httponly'] ?? false,
samesite: $options['samesite'] ?? false,
);
}
}