-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHlebAsyncBootstrap.php
More file actions
489 lines (443 loc) · 20.6 KB
/
HlebAsyncBootstrap.php
File metadata and controls
489 lines (443 loc) · 20.6 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<?php
/**
* @author Foma Tuturov <fomiash@yandex.ru>
*/
declare(strict_types=1);
namespace Hleb;
use App\Bootstrap\ContainerFactory;
use App\Middlewares\Hlogin\Registrar;
use AsyncExitException;
use Exception;
use Hleb\Base\RollbackInterface;
use Hleb\Constructor\Attributes\Accessible;
use Hleb\Constructor\Attributes\AvailableAsParent;
use Hleb\HttpMethods\{External\RequestUri, External\SystemRequest};
use Hleb\Constructor\Data\DebugAnalytics;
use Hleb\Helpers\ResetAndRollbackHelper;
use Hleb\HttpMethods\External\Response as SystemResponse;
use Hleb\HttpMethods\Intelligence\Cookies\AsyncCookies;
use Hleb\Init\ErrorLog;
use Hleb\Init\Headers\ParsePsrHeaders;
use Hleb\Init\Headers\ParseSwooleHeaders;
use Hleb\Main\Logger\LoggerInterface;
use Hleb\Static\Response;
use Throwable;
#[Accessible] #[AvailableAsParent]
class HlebAsyncBootstrap extends HlebBootstrap
{
private static int $processNumber = 0;
/**
* Constructor with initialization.
* In asynchronous mode, must be called once outside the loop.
*
* Конструктор с инициализацией.
* В асинхронном режиме должен быть вызван один раз вне цикла.
*
* @param string|null $publicPath - full path to the public directory of the project.
* - полный путь к публичной директории проекта.
*
* @param array $config - an array replacing the configuration data.
* - заменяющий конфигурационные данные массив.
*
* @throws Throwable
*/
public function __construct(?string $publicPath = null, array $config = [], ?LoggerInterface $logger = null)
{
$this->mode = self::ASYNC_MODE;
// In asynchronous mode, an initialization error should be logged.
// В асинхронном режиме ошибка инициализации должна быть отправлена в лог.
try {
parent::__construct($publicPath, $config, $logger);
} catch (\Throwable $t) {
$this->errorLog($t);
throw $t;
}
}
/**
* @inheritDoc
*/
#[\Override]
public function setLogger(LoggerInterface $logger): static
{
parent::setLogger($logger);
return $this;
}
/**
* Processing the request and displaying the result. In asynchronous mode, it is called inside the loop.
* May contain a PSR-7 like object.
* @param $request - with further initialization.
* If there is no such object, loading data from global variables.
*
* @param $session - parameter allows you to use an external
* session storage mechanism along with using
* $_SESSION array to manipulate session data and return it to the session handler.
* Standard session initialization will be disabled.
* @param $cookie - parameter works similarly to external sessions for Cookies.
*
*
* Обработка запроса и вывод результата. В асинхронном режиме вызывается внутри цикла.
* Может содержать PSR-7 подобный объект $request с дальнейшей инициализацией.
* Если такого объекта нет, загрузка данных из глобальных переменных.
*
* Параметр $session позволяет использовать внешний механизм хранения
* сессий вместе с использованием массива $_SESSION для оперирования данными сессий
* и возвращения его в обработчик сессий.
* Стандартная инициализация сессий при этом будет отключена.
* Параметр $cookies действует аналогично внешним сессиям для Cookies.
*/
#[\Override]
public function load(?object $request = null, ?array $session = null, ?array $cookie = null): HlebAsyncBootstrap
{
self::$processNumber++;
$this->session = $session;
$this->cookies = $cookie;
\ob_start();
try {
try {
$this->loadProject($request);
$this->requestCompletion((string)\ob_get_contents());
} catch (AsyncExitException $e) {
$this->asyncScriptExitEmulation($e, (string)\ob_get_contents());
} catch (HttpException $e) {
$this->scriptHttpError($e);
} catch (Throwable $t) {
$this->getPreviousErrorControl($t) or $this->scriptErrorHandling($t);
}
} catch (\Throwable) {
/*
* There is a possibility of an error in error handling,
* but an error should not be returned here.
*
* Существует вероятность ошибки в обработке ошибок,
* но ошибки вернуться здесь не должно.
*/
}
\ob_end_clean();
$this->logsPostProcessing();
$this->afterRequest();
return $this;
}
/**
* Ending an asynchronous request.
*
* Завершение асинхронного запроса.
*/
public function afterRequest(): void
{
try {
if (\class_exists(Response::class, false)) {
$this->response = Response::getInstance() ?? new SystemResponse();
}
if (\session_status() === PHP_SESSION_ACTIVE) {
AsyncCookies::setSessionName(\session_name());
\session_write_close();
\session_abort();
}
AsyncCookies::output();
} catch (\Throwable) {
if (\session_status() === PHP_SESSION_ACTIVE) {
\session_abort();
}
}
$_GET = $_POST = $_SERVER = $_SESSION = $_COOKIE = $_REQUEST = $_FILES = [];
self::prepareAsyncRequestData($this->config, self::$processNumber);
}
/**
* Returns the result of request processing in the load() method as an object with data,
* which can then be converted (substituted) into any PSR-7 object.
*
* Возвращает результат обработки запроса в методе load() в виде объекта с данными,
* которые могут быть затем преобразованы (подставлены) в любой объект по PSR-7.
*/
public function getResponse(): SystemResponse
{
return $this->response;
}
/**
* When adding the $session array in the load() method, it returns
* the result of using the $_SESSION variable.
* An alternative way to work with sessions.
*
* При добавлении массива $session в методе load() возвращает
* результат использования переменной $_SESSION.
* Альтернативный вариант работы с сессиями.
*/
public function getSession(): ?array
{
return $this->session;
}
/**
* When adding the $cookies array in the load() method, it returns
* the result of using the $_COOKIE variable.
* An alternative way to work with cookies.
*
* При добавлении массива $cookies в методе load() возвращает
* результат использования переменной $_COOKIE.
* Альтернативный вариант работы с Cookies.
*/
public function getCookies(): ?array
{
return $this->cookies;
}
/**
* Saving the error in prepared form if it occurred at the top level.
*
* Сохранение ошибки в подготовленном виде, если она возникла на самом верхнем уровне.
*/
public function errorLog(\Throwable $e): void
{
// The error may be in the error handler itself.
// Ошибка может быть в самом обработчике ошибок.
try {
\class_exists(ErrorLog::class, false) or require __DIR__ . '/Init/ErrorLog.php';
ErrorLog::log($e);
} catch (\Throwable $t) {
\error_log((string)$e);
\error_log((string)$t);
}
}
/**
* Preparing data to use an asynchronous request.
*
* Подготовка данных к использованию асинхронного запроса.
*
* @see HlebQueueBootstrap::prepareAsyncRequestData()
*
* @internal
*/
protected static function prepareAsyncRequestData(array $config, int $processNumber): void
{
// If your application does not use state storage, you can disable state cleanup.
// Если в приложении не используется хранение состояния, то можно отключить его очистку.
if ($config['system']['async.clear.state'] ?? true) {
foreach (\get_declared_classes() as $class) {
\is_a($class, RollbackInterface::class, true) and $class::rollback();
}
ContainerFactory::reset();
ResetAndRollbackHelper::rollbackClassState([Registrar::class, DebugAnalytics::class, ErrorLog::class]);
}
ResetAndRollbackHelper::rollbackClassState([ContainerFactory::class]);
/*
* Periodically clean up used memory and call GC. Will be applied to every $rate request.
* For example, if you pass HLEB_ASYNC_RE_CLEANING=3, then after every third request.
*
* Периодическая очистка используемой памяти и вызов GC. Будет применено к каждому $rate запросу.
* Например, если передать HLEB_ASYNC_RE_CLEANING=3, то после каждого третьего запроса.
*/
$rate = (int)get_env('HLEB_ASYNC_RE_CLEANING', get_constant('HLEB_ASYNC_RE_CLEANING', self::DEFAULT_RE_CLEANING));
if ($rate >= 0 && ($rate === 0 || $processNumber % $rate == 0)) {
\gc_collect_cycles();
\gc_mem_caches();
}
\memory_reset_peak_usage();
}
/**
* The framework can work with any incoming request, where the
* (or set before executing the load()) method the corresponding values of $_SERVER.
* In extreme cases, this data can be taken from a PSR-7 like object
* (Psr\Http\Message\ServerRequestInterface).
*
* Фреймворк может работать с любым входящим запросом, где определены
* (или заданы до выполнения метода load()) соответствующие значения $_SERVER.
* В крайнем случае эти данные могут быть взяты из объекта по подобию PSR-7
* (Psr\Http\Message\ServerRequestInterface).
*
* @param object|null $request
* @return SystemRequest
* @throws Exception
* @see self::load()
*/
#[\Override]
protected function buildRequest(?object &$request = null): SystemRequest
{
$headers = [];
if ($request !== null) {
if (\method_exists($request, 'getCookieParams')) {
[$body, $headers] = $this->parsePsr7Request($request);
$headers = $this->parseHeaders($headers, ParsePsrHeaders::class);
} else if (\method_exists($request, 'rawContent') ||
\method_exists($request, 'getContent')
) {
[$body, $headers] = $this->parseSwooleRequest($request);
$headers = $this->parseHeaders($headers, ParseSwooleHeaders::class);
} else if (\str_starts_with($request::class, "Workerman\\")) {
[$body, $headers] = $this->parseWorkermanRequest($request);
} else {
// Data will be received from $_SERVER.
// Данные будут получены из $_SERVER.
$body = null;
}
}
$_SERVER['HTTP_HOST'] = $this->convertHost($_SERVER['HTTP_HOST']);
$this->standardization();
$this->convertForcedMethod($_POST, $_SERVER, $request);
$protocol = \trim(\stristr($_SERVER["SERVER_PROTOCOL"] ?? '','/') ?: '', ' /') ?: '1.1';
$streamBody = isset($body) && \is_object($body) ? $body : null;
$rawBody = isset($body) && \is_string($body) ? $body : null;
$parsedBody = isset($body) && \is_array($body) ? $body : null;
$_SERVER['REMOTE_ADDR'] = \strip_tags((string)($_SERVER['REMOTE_ADDR'] ?? $_SERVER['HTTP_CLIENT_IP'] ?? $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null));
return new SystemRequest(
(array)$_COOKIE,
$rawBody,
$parsedBody,
$streamBody,
$_SERVER['REQUEST_METHOD'],
$headers,
$protocol,
new RequestUri(
(string)$_SERVER['HTTP_HOST'],
(string)$_SERVER['DOCUMENT_URI'],
$_SERVER['QUERY_STRING'],
$_SERVER['SERVER_PORT'],
$_SERVER['REQUEST_SCHEME'],
$_SERVER['REMOTE_ADDR'],
));
}
/**
* @inheritDoc
*/
protected function convertForcedMethod(array &$post, array &$server, ?object &$request = null): ?string
{
$forced = parent::convertForcedMethod($post, $server);
if ($forced && $request) {
try {
if (\method_exists($request, 'withMethod')) {
// PSR7
$request = $request->withMethod($forced);
} else if (\property_exists($request, 'server') && \is_array($request->server)) {
// Swoole
$request->server['request_method'] = \strtolower($forced);
} else if (\method_exists($request, 'method')) {
// Workerman
$request = new ($request::class)(\strtolower($forced), $request->uri(), $request->header(), $request->rawBody());
}
} catch (\Throwable $t) {
$this->getLogger()->warning($t);
}
}
return $forced;
}
/**
* An attempt to parse a PSR-7 object.
*
* Попытка разбора PSR-7 объекта.
*
* @see \Psr\Http\Message\ServerRequestInterface
*/
protected function parsePsr7Request(object $req): array
{
empty($_COOKIE) and $_COOKIE = $req->getCookieParams();
empty($_POST) and $_POST = (array)$req->getParsedBody();
$body = method_exists($req, 'getBody') ? (string)$req->getBody() : '';
empty($_GET) and $_GET = (array)$req->getQueryParams();
empty($_FILES) and $_FILES = $req->getUploadedFiles();
isset($_SERVER['REQUEST_METHOD']) or $_SERVER['REQUEST_METHOD'] = \strtoupper((string)$req->getMethod());
$headers = $req->getHeaders();
if (\method_exists($req, 'getProtocolVersion')) {
$_SERVER["SERVER_PROTOCOL"] = 'HTTP/' . $req->getProtocolVersion();
}
$params = \method_exists($req, 'getServerParams') ? $req->getServerParams() : null;
if (\method_exists($req, 'getUri') && \is_object($req->getUri())) {
/** @var object $uri */
$uri = $req->getUri();
isset($_SERVER['HTTP_HOST']) or $_SERVER['HTTP_HOST'] = $uri->getHost();
isset($_SERVER['DOCUMENT_URI']) or $_SERVER['DOCUMENT_URI'] = $uri->getPath();
isset($_SERVER['SERVER_NAME']) or $_SERVER['SERVER_NAME'] = $uri->getHost();
isset($_SERVER['QUERY_STRING']) or $_SERVER['QUERY_STRING'] = $uri->getQuery();
isset($_SERVER['SERVER_PORT']) or $_SERVER['SERVER_PORT'] = $uri->getPort();
isset($_SERVER['REQUEST_URI']) or $_SERVER['REQUEST_URI'] = $uri->getPath() . '?' .
\ltrim($uri->getQuery(), '?/');
if (empty($_SERVER['REMOTE_ADDR'])) {
if ($params !== null) {
$_SERVER['REMOTE_ADDR'] = (string)($params['REMOTE_ADDR'] ?? $params['HTTP_CLIENT_IP'] ?? $params['HTTP_X_FORWARDED_FOR'] ?? null);
} else if (\filter_var($_SERVER['HTTP_HOST'], FILTER_VALIDATE_IP)) {
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_HOST'];
}
}
}
return [$body, $headers];
}
/**
* An attempt to parse an object according to the PHP Swoole specification.
*
* Попытка разбора объекта по спецификации PHP Swoole.
*
* @see \Swoole\Http\Request
*/
protected function parseSwooleRequest(object $req): array
{
$headers = $req->header;
$server = $req->server;
$_COOKIE = $req->cookie ?? [];
$_POST = $req->post ?? [];
$_GET = $req->get ?? [];
$_FILES = $req->files ?? [];
$_SERVER['HTTP_HOST'] = $server['remote_addr'] ?? $headers['host'];
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
$_SERVER['REMOTE_ADDR'] = $server['remote_addr'] ?? '';
$_SERVER['REQUEST_METHOD'] = \strtoupper((string)$server['request_method']);
$_SERVER['DOCUMENT_URI'] = $server['path_info'] ?? '';
$_SERVER['SERVER_PORT'] = $server['server_port'] ?? null;
$_SERVER['QUERY_STRING'] = $server['query_string'] ?? '';
$_SERVER['REQUEST_URI'] = $server['request_uri'] ?? '';
$_SERVER["SERVER_PROTOCOL"] = $server['server_protocol'] ?? 'HTTP/1.1';
$_SERVER['HTTPS'] = $_SERVER['SERVER_PORT'] == 443 ? 'on' : 'off';
// An additional field by which you can get the type of HTTP connection scheme.
// Дополнительное поле по которому можно получить тип HTTP-схемы подключения.
if (isset($server['https'])) {
$_SERVER['HTTPS'] = $server['https'] === 'on' ? 'on' : 'off';
}
$body = method_exists($req, 'rawContent') ? $req->rawContent() : $req->getContent();
return [(string)$body, $headers];
}
/**
* An attempt to parse an object according to the PHP Workerman specification.
*
* Попытка разбора объекта по спецификации PHP Workerman.
*
* @param \Workerman\Protocols\Http\Request $req
*/
protected function parseWorkermanRequest(object $req): array
{
// Override $_SERVER['HTTP_HOST'] on initialization if it's different.
// Переопределите $_SERVER['HTTP_HOST'] при инициализации, если он отличается.
$_SERVER['HTTP_HOST'] = $_SERVER['HTTP_HOST'] ?? $req->host(true);
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'];
$_SERVER['REMOTE_ADDR'] = $req->connection?->getRemoteIp();
$get = $req->get() ?: [];
$_GET = $get;
$_POST = $req->post() ?: [];
$_SERVER['QUERY_STRING'] = $get ? '?' . \http_build_query($get) : '';
$_SERVER['REQUEST_METHOD'] = \strtoupper((string)$req->method());
$_SERVER['DOCUMENT_URI'] = $req->uri() ?: '';
$_SERVER['SERVER_PORT'] = $_SERVER['SERVER_PORT'] ?? $req->connection?->getLocalPort();
$_SERVER['REQUEST_URI'] = $_SERVER['DOCUMENT_URI'] . $_SERVER['QUERY_STRING'];
$_SERVER['HTTPS'] = $_SERVER['HTTPS'] ?? ($_SERVER['SERVER_PORT'] == 443 ? 'on' : 'off');
$_SERVER["SERVER_PROTOCOL"] = 'HTTP/' . $req->protocolVersion();
$body = $req->rawBody();
$headers = $req->header() ?: [];
$_SESSION = $req->session() ? $req->session()->all() : [];
$_COOKIE = $req->cookie() ?: [];
$_FILES = $req->file() ?: [];
return [$body, $headers];
}
/**
* Converts headings to standard format.
*
* Преобразует заголовки в стандартный вид.
*/
protected function parseHeaders(mixed $headers, string $class): array
{
$headers = (new $class())->update($headers);
return \array_change_key_case($headers, CASE_LOWER);
}
/**
* Execution exit handling for asynchronous mode.
*
* Обработка выхода из выполнения для асинхронного режима.
*/
private function asyncScriptExitEmulation(AsyncExitException $e, string $content): void
{
$this->output($content . $e->getMessage(), $e->getStatus(), $e->getHeaders());
}
}