-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathSymfonyHttpServer.php
More file actions
323 lines (264 loc) · 8.85 KB
/
SymfonyHttpServer.php
File metadata and controls
323 lines (264 loc) · 8.85 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
<?php
declare(strict_types=1);
namespace Pest\Browser\Drivers;
use Amp\ByteStream\ReadableResourceStream;
use Amp\Http\Server\DefaultErrorHandler;
use Amp\Http\Server\HttpServer as AmpHttpServer;
use Amp\Http\Server\HttpServerStatus;
use Amp\Http\Server\Request as AmpRequest;
use Amp\Http\Server\RequestHandler\ClosureRequestHandler;
use Amp\Http\Server\Response;
use Amp\Http\Server\SocketHttpServer;
use Exception;
use Pest\Browser\Contracts\HttpServer;
use Pest\Browser\Exceptions\ServerNotFoundException;
use Pest\Browser\Execution;
use Pest\Browser\GlobalState;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\Mime\MimeTypes;
use Throwable;
/**
* @internal
*
* @codeCoverageIgnore
*/
final class SymfonyHttpServer implements HttpServer
{
/**
* The underlying socket server instance, if any.
*/
private ?AmpHttpServer $socket = null;
/**
* The original asset URL, if set.
*/
private ?string $originalAssetUrl = null;
/**
* The last throwable that occurred during the server's execution.
*/
private ?Throwable $lastThrowable = null;
/**
* Creates a new Symfony http server instance.
*/
public function __construct(
public readonly string $host,
public readonly int $port,
) {
//
}
/**
* Destroy the server instance and stop listening for incoming connections.
*/
public function __destruct()
{
// @codeCoverageIgnoreStart
// $this->stop();
}
/**
* Rewrite the given URL to match the server's host and port.
*/
public function rewrite(string $url): string
{
if (! str_starts_with($url, 'http://') && ! str_starts_with($url, 'https://')) {
$url = mb_ltrim($url, '/');
$url = '/'.$url;
}
$parts = parse_url($url);
$path = $parts['path'] ?? '/';
$query = $parts['query'] ?? '';
$fragment = $parts['fragment'] ?? '';
return $this->url().$path.($query !== '' ? '?'.$query : '').($fragment !== '' ? '#'.$fragment : '');
}
/**
* Start the server and listen for incoming connections.
*/
public function start(): void
{
if ($this->socket instanceof AmpHttpServer) {
return;
}
$this->socket = $server = SocketHttpServer::createForDirectAccess(new NullLogger());
$server->expose("{$this->host}:{$this->port}");
$server->start(
new ClosureRequestHandler($this->handleRequest(...)),
new DefaultErrorHandler(),
);
}
/**
* Stop the server and close all connections.
*/
public function stop(): void
{
// @codeCoverageIgnoreStart
if ($this->socket instanceof AmpHttpServer) {
$this->flush();
if ($this->socket instanceof AmpHttpServer) {
if (in_array($this->socket->getStatus(), [HttpServerStatus::Starting, HttpServerStatus::Started], true)) {
$this->socket->stop();
}
$this->socket = null;
}
}
}
/**
* Flush pending requests and close all connections.
*/
public function flush(): void
{
if (! $this->socket instanceof AmpHttpServer) {
return;
}
Execution::instance()->tick();
$this->lastThrowable = null;
}
/**
* Bootstrap the server and set the application URL.
*/
public function bootstrap(): void
{
$this->start();
if (is_string($_ENV['DEFAULT_URI'] ?? null)) {
$this->setOriginalAssetUrl($_ENV['DEFAULT_URI']);
}
}
/**
* Get the last throwable that occurred during the server's execution.
*/
public function lastThrowable(): ?Throwable
{
return $this->lastThrowable;
}
/**
* Throws the last throwable if it should be thrown.
*
* @throws Throwable
*/
public function throwLastThrowableIfNeeded(): void
{
if (! $this->lastThrowable instanceof Throwable) {
return;
}
throw $this->lastThrowable;
}
/**
* Get the public path for the given path.
*/
private function url(): string
{
if (! $this->socket instanceof AmpHttpServer) {
throw new ServerNotFoundException('The HTTP server is not running.');
}
return sprintf('http://%s:%d', $this->host, $this->port);
}
/**
* Sets the original asset URL.
*/
private function setOriginalAssetUrl(string $url): void
{
$this->originalAssetUrl = mb_rtrim($url, '/');
}
/**
* Handle the incoming request and return a response.
*/
private function handleRequest(AmpRequest $request): Response
{
GlobalState::flush();
if (Execution::instance()->isWaiting() === false) {
Execution::instance()->tick();
}
$publicPath = getcwd().DIRECTORY_SEPARATOR.(is_string($_ENV['PUBLIC_PATH'] ?? null) ? $_ENV['PUBLIC_PATH'] : 'public');
$filepath = $publicPath.$request->getUri()->getPath();
if (file_exists($filepath) && ! is_dir($filepath)) {
return $this->asset($filepath);
}
$kernelClass = is_string($_ENV['KERNEL_CLASS'] ?? null) ? $_ENV['KERNEL_CLASS'] : 'App\Kernel';
if (class_exists($kernelClass) === false) {
$this->lastThrowable = new Exception('You must define the test kernel class environment variable: KERNEL_CLASS.');
throw $this->lastThrowable;
}
/** @var KernelInterface&TerminableInterface $kernel */
$kernel = new $kernelClass($_ENV['APP_ENV'], (bool) $_ENV['APP_DEBUG']);
$contentType = $request->getHeader('content-type') ?? '';
$method = mb_strtoupper($request->getMethod());
$rawBody = (string) $request->getBody();
$parameters = [];
if ($method !== 'GET' && str_starts_with(mb_strtolower($contentType), 'application/x-www-form-urlencoded')) {
parse_str($rawBody, $parameters);
}
$symfonyRequest = Request::create(
(string) $request->getUri(),
$method,
$parameters,
$request->getCookies(),
[], // @TODO files...
[], // @TODO server variables...
$rawBody
);
$symfonyRequest->headers->add($request->getHeaders());
try {
$response = $kernel->handle($symfonyRequest);
} catch (Throwable $e) {
$this->lastThrowable = $e;
throw $e;
}
$kernel->terminate($symfonyRequest, $response);
if (property_exists($response, 'exception') && $response->exception !== null) {
assert($response->exception instanceof Throwable);
$this->lastThrowable = $response->exception;
}
$content = $response->getContent();
if ($content === false) {
try {
ob_start();
$response->sendContent();
} finally {
// @phpstan-ignore-next-line
$content = mb_trim(ob_get_clean());
}
}
return new Response(
$response->getStatusCode(),
$response->headers->all(), // @phpstan-ignore-line
$content,
);
}
/**
* Return an asset response.
*/
private function asset(string $filepath): Response
{
$file = fopen($filepath, 'r');
if ($file === false) {
return new Response(404);
}
$mimeTypes = new MimeTypes();
$contentType = $mimeTypes->getMimeTypes(pathinfo($filepath, PATHINFO_EXTENSION));
$contentType = $contentType[0] ?? 'application/octet-stream';
if (str_ends_with($filepath, '.js') || str_ends_with($filepath, '.css')) {
$temporaryStream = fopen('php://temp', 'r+');
assert($temporaryStream !== false, 'Failed to open temporary stream.');
// @phpstan-ignore-next-line
$temporaryContent = fread($file, (int) filesize($filepath));
assert($temporaryContent !== false, 'Failed to open temporary stream.');
$content = $this->rewriteAssetUrl($temporaryContent);
fwrite($temporaryStream, $content);
rewind($temporaryStream);
$file = $temporaryStream;
}
return new Response(200, [
'Content-Type' => $contentType,
], new ReadableResourceStream($file));
}
/**
* Rewrite the asset URL in the given content.
*/
private function rewriteAssetUrl(string $content): string
{
if ($this->originalAssetUrl === null) {
return $content;
}
return str_replace($this->originalAssetUrl, $this->url(), $content);
}
}