-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathServer.php
More file actions
334 lines (282 loc) · 9.73 KB
/
Copy pathServer.php
File metadata and controls
334 lines (282 loc) · 9.73 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
declare(strict_types=1);
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Executor;
use Closure;
use Deployer\Exception\Exception;
use Symfony\Component\Console\Output\OutputInterface;
class Server
{
private string $host;
private int $port;
private OutputInterface $output;
private bool $stop = false;
/**
* @var ?resource
*/
private $socket;
/**
* Each client is tracked with its socket, read buffer, and connection timestamp.
* @var array<int, array{socket: resource, buffer: string, connectedAt: float}>
*/
private array $clients = [];
private int $nextClientId = 0;
private Closure $afterCallback;
private Closure $tickerCallback;
private Closure $routerCallback;
private ?string $authToken = null;
/**
* Timeout in seconds for idle client connections.
*/
private float $clientTimeout = 30.0;
private const REASON_PHRASES = [
200 => 'OK',
403 => 'Forbidden',
404 => 'Not Found',
500 => 'Internal Server Error',
];
public function __construct(string $host, int $port, OutputInterface $output)
{
$this->host = $host;
$this->port = $port;
$this->output = $output;
}
public function run(): void
{
try {
$this->socket = $this->createServerSocket();
$this->updatePort();
if ($this->output->isDebug()) {
$this->output->writeln("[master] Starting server at http://{$this->host}:{$this->port}");
}
($this->afterCallback)($this->port);
while (true) {
$this->acceptNewConnections();
$this->handleClientRequests();
// Prevent CPU exhaustion and 60fps ticker.
usleep(16_000); // 16ms
($this->tickerCallback)();
if ($this->stop) {
break;
}
}
if ($this->output->isDebug()) {
$this->output->writeln("[master] Stopping server at http://{$this->host}:{$this->port}");
}
} finally {
foreach ($this->clients as $id => $client) {
$this->closeClient($id);
}
if (isset($this->socket)) {
fclose($this->socket);
}
}
}
/**
* @return resource
* @throws Exception
*/
private function createServerSocket()
{
$server = stream_socket_server("tcp://{$this->host}:{$this->port}", $errno, $errstr);
if (!$server) {
throw new Exception("Socket creation failed: $errstr ($errno)");
}
if (!stream_set_blocking($server, false)) {
throw new Exception("Failed to set server socket to non-blocking mode");
}
return $server;
}
private function updatePort(): void
{
$name = stream_socket_get_name($this->socket, false);
if ($name) {
[, $port] = explode(':', $name);
$this->port = (int) $port;
} else {
throw new Exception("Failed to get the assigned port");
}
}
private function acceptNewConnections(): void
{
while ($newClientSocket = @stream_socket_accept($this->socket, 0)) {
if (!stream_set_blocking($newClientSocket, false)) {
fclose($newClientSocket);
continue;
}
$id = $this->nextClientId++;
$this->clients[$id] = [
'socket' => $newClientSocket,
'buffer' => '',
'connectedAt' => microtime(true),
];
}
}
private function handleClientRequests(): void
{
foreach ($this->clients as $id => $client) {
$socket = $client['socket'];
if (feof($socket)) {
$this->closeClient($id);
continue;
}
// Read available data into the buffer.
$data = @fread($socket, 65536);
if ($data !== false && $data !== '') {
$this->clients[$id]['buffer'] .= $data;
}
// Check if we have a complete request.
$buffer = $this->clients[$id]['buffer'];
if (!self::isCompleteRequest($buffer)) {
// Check for idle timeout.
if (microtime(true) - $client['connectedAt'] > $this->clientTimeout) {
$this->closeClient($id);
}
continue;
}
// Process the complete request.
try {
[$path, $payload, $headers] = self::parseRequest($buffer);
if ($this->authToken !== null) {
$provided = $headers['authorization'] ?? '';
if ($provided !== "Bearer {$this->authToken}") {
$this->sendResponse($socket, new Response(403, ['error' => 'Forbidden']));
continue;
}
}
$response = ($this->routerCallback)($path, $payload);
$this->sendResponse($socket, $response);
} catch (\Throwable $e) {
$errorResponse = new Response(500, ['error' => $e->getMessage()]);
try {
$this->sendResponse($socket, $errorResponse);
} catch (\Throwable) {
// Best effort — socket may be broken.
}
} finally {
$this->closeClient($id);
}
}
}
/**
* Check if the buffer contains a complete HTTP request
* (headers terminated by \r\n\r\n, and body length matches Content-Length).
*/
public static function isCompleteRequest(string $buffer): bool
{
$headerEnd = strpos($buffer, "\r\n\r\n");
if ($headerEnd === false) {
return false;
}
$headers = substr($buffer, 0, $headerEnd);
$bodyStart = $headerEnd + 4;
// Extract Content-Length from headers.
if (preg_match('/^Content-Length:\s*(\d+)/mi', $headers, $matches)) {
$contentLength = (int) $matches[1];
return strlen($buffer) - $bodyStart >= $contentLength;
}
// No Content-Length header — request is complete after headers.
return true;
}
/**
* Parse a complete HTTP request into path, payload, and headers.
*
* @return array{0: string, 1: mixed, 2: array<string, string>}
*/
public static function parseRequest(string $request): array
{
$headerEnd = strpos($request, "\r\n\r\n");
if ($headerEnd === false) {
throw new Exception("Malformed request: no header terminator found");
}
$headerSection = substr($request, 0, $headerEnd);
$body = substr($request, $headerEnd + 4);
$lines = explode("\r\n", $headerSection);
$requestLine = $lines[0];
$parts = explode(' ', $requestLine);
if (count($parts) !== 3) {
throw new Exception("Malformed request line: $requestLine");
}
$path = $parts[1];
$headers = [];
for ($i = 1; $i < count($lines); $i++) {
$line = $lines[$i];
if (str_contains($line, ':')) {
[$key, $value] = explode(':', $line, 2);
$headers[strtolower(trim($key))] = trim($value);
}
}
if (empty($headers['content-type']) || $headers['content-type'] !== 'application/json') {
throw new Exception("Malformed request: invalid Content-Type");
}
// Trim body to Content-Length if present (ignore trailing data).
if (isset($headers['content-length'])) {
$body = substr($body, 0, (int) $headers['content-length']);
}
$payload = json_decode($body, true, flags: JSON_THROW_ON_ERROR);
return [$path, $payload, $headers];
}
/**
* @param resource $socket
*/
private function sendResponse($socket, Response $response): void
{
$code = $response->getStatus();
$reason = self::REASON_PHRASES[$code] ?? 'Unknown';
$content = json_encode($response->getBody(), flags: JSON_PRETTY_PRINT);
$data = "HTTP/1.1 $code $reason\r\n"
. "Content-Type: application/json\r\n"
. "Content-Length: " . strlen($content) . "\r\n"
. "Connection: close\r\n\r\n"
. $content;
self::writeAll($socket, $data);
}
/**
* Write all data to socket, handling partial writes.
*
* @param resource $socket
*/
public static function writeAll($socket, string $data): void
{
$written = 0;
$len = strlen($data);
while ($written < $len) {
$bytes = @fwrite($socket, substr($data, $written));
if ($bytes === false || $bytes === 0) {
throw new Exception('Socket write failed');
}
$written += $bytes;
}
}
private function closeClient(int $id): void
{
if (isset($this->clients[$id])) {
fclose($this->clients[$id]['socket']);
unset($this->clients[$id]);
}
}
public function afterRun(Closure $callback): void
{
$this->afterCallback = $callback;
}
public function ticker(Closure $callback): void
{
$this->tickerCallback = $callback;
}
public function router(Closure $callback): void
{
$this->routerCallback = $callback;
}
public function setAuthToken(string $token): void
{
$this->authToken = $token;
}
public function stop(): void
{
$this->stop = true;
}
}