|
14 | 14 | // Data received |
15 | 15 | $http_worker->onMessage = static function ($connection, $request) { |
16 | 16 |
|
17 | | - return match($request->path()) { |
18 | | - |
19 | | - '/echo' => $connection->send( new Response( |
20 | | - 200, |
21 | | - ['Content-Type' => 'text/plain'], |
22 | | - implode("\n", array_map(fn($name, $value) => "$name: $value", $request->header(), $request->header()))) |
23 | | - ), |
24 | | - |
25 | | - '/cookie' => $connection->send( new Response( |
26 | | - 200, |
27 | | - ['Content-Type' => 'text/plain'], |
28 | | - implode("\n", array_map(fn($name, $value) => "$name=$value", $request->cookie(), $request->cookie()))) |
29 | | - ), |
30 | | - |
31 | | - '/' => $connection->send( new Response( |
32 | | - 200, |
33 | | - ['Content-Type' => 'text/plain'], |
34 | | - $request->method() === 'POST' ? $request->rawBody() : 'OK') |
35 | | - ), |
| 17 | + $path = $request->path(); |
| 18 | + |
| 19 | + if ($path === '/echo') { |
| 20 | + $body = ''; |
| 21 | + foreach ($request->header() as $name => $value) { |
| 22 | + $body .= "$name: $value\n"; |
| 23 | + } |
| 24 | +; |
| 25 | + return $connection->send( new Response( |
| 26 | + 200, |
| 27 | + ['Content-Type' => 'text/plain'], |
| 28 | + $body |
| 29 | + )); |
| 30 | + } |
| 31 | + |
| 32 | + if ($path === '/cookie') { |
| 33 | + $body = ''; |
| 34 | + foreach ($request->cookie() as $name => $value) { |
| 35 | + $body .= "$name=$value\n"; |
| 36 | + } |
36 | 37 |
|
37 | | - default => null, |
38 | | - }; |
| 38 | + return $connection->send( new Response( |
| 39 | + 200, |
| 40 | + ['Content-Type' => 'text/plain'], |
| 41 | + $body |
| 42 | + )); |
| 43 | + } |
| 44 | + |
| 45 | + if ($path === '/') { |
| 46 | + $body = $request->method() === 'POST' ? $request->rawBody() : 'OK'; |
| 47 | + |
| 48 | + return $connection->send( new Response( |
| 49 | + 200, |
| 50 | + ['Content-Type' => 'text/plain'], |
| 51 | + $body |
| 52 | + )); |
| 53 | + } |
39 | 54 | }; |
40 | 55 |
|
41 | 56 | // Run all workers |
|
0 commit comments