From 1378f4b1ec95634620265371dcd7b05086dbda86 Mon Sep 17 00:00:00 2001 From: zhanguangcheng <14712905@qq.com> Date: Thu, 28 Dec 2023 12:15:22 +0800 Subject: [PATCH] Rename register_shutdown_function --- README.md | 5 ++++- php.ini | 2 +- phpunit.xml => phpunit.xml.dist | 4 +++- src/Functions.php | 4 +++- src/Http.php | 5 +++++ src/Linkerman.php | 3 +-- tests/Server.php | 9 +++++---- 7 files changed, 22 insertions(+), 10 deletions(-) rename phpunit.xml => phpunit.xml.dist (77%) diff --git a/README.md b/README.md index 54f4580..6f1df69 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ function run($request): string Add to php.ini file ```ini -disable_functions=register_shutdown_function,set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset,session_destroy,is_uploaded_file,move_uploaded_file +disable_functions=set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset,session_destroy,is_uploaded_file,move_uploaded_file ``` Start the service @@ -108,6 +108,9 @@ php server.php start * Solution: Replace with function `exit_exception()` * `file_get_contents("php://input")` * Solution: Replace with function `request_raw_body()` +* `register_shutdown_function()` + * Why: Since the resident memory runs, the registered callback function is not actually executed, which may lead to memory leaks + * Solution: Replace with function `register_shutdown_function_user() ### How to access the Connection Object and Request Object of Workerman ```php diff --git a/php.ini b/php.ini index b3e073d..a9b751c 100644 --- a/php.ini +++ b/php.ini @@ -14,4 +14,4 @@ memory_limit = 512M opcache.jit_buffer_size=128M opcache.jit=tracing -disable_functions=register_shutdown_function,set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset,session_destroy,is_uploaded_file,move_uploaded_file +disable_functions=set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset,session_destroy,is_uploaded_file,move_uploaded_file diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 77% rename from phpunit.xml rename to phpunit.xml.dist index 7d0904f..7932ebf 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -3,6 +3,9 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" + cacheDirectory="./tests/_output/cache" + displayDetailsOnTestsThatTriggerErrors="true" + displayDetailsOnTestsThatTriggerWarnings="true" > @@ -11,7 +14,6 @@ - ./app ./src diff --git a/src/Functions.php b/src/Functions.php index 0883ebb..b87f7c6 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -6,6 +6,8 @@ * @author zhanguangcheng<14712905@qq.com> * @license http://www.opensource.org/licenses/mit-license.php MIT License * @noinspection PhpRedeclarationStdlibFunctionInspection + * @noinspection PhpUnusedParameterInspection + * @noinspection PhpUnused */ use Linkerman\ExitException; @@ -21,7 +23,7 @@ * @param mixed ...$args * @return void */ -function register_shutdown_function(callable $callback, mixed ...$args): void +function register_shutdown_function_user(callable $callback, mixed ...$args): void { if (!(\is_array($callback) && $callback[0] === '\\Workerman\\Worker')) { // Prevent memory leaks caused by callback functions that are not executed normally diff --git a/src/Http.php b/src/Http.php index d859959..5e94111 100644 --- a/src/Http.php +++ b/src/Http.php @@ -88,6 +88,8 @@ public static function registerGlobalVariables($connection): void 'REQUEST_URI' => $request->uri(), 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php', + 'REQUEST_TIME' => time(), + 'REQUEST_TIME_FLOAT' => microtime(true), ]; if (null !== ($contentType = $request->header('content-type'))) { $_SERVER['CONTENT_TYPE'] = $contentType; @@ -98,5 +100,8 @@ public static function registerGlobalVariables($connection): void foreach ($request->header() as $key => $value) { $_SERVER['HTTP_' . \strtoupper(\str_replace('-', '_', $key))] = $value; } + if (isset($_SERVER['HTTP_HTTPS'])) { + $_SERVER['HTTPS'] = $_SERVER['HTTP_HTTPS']; + } } } diff --git a/src/Linkerman.php b/src/Linkerman.php index 1379ef7..ca0a0f0 100644 --- a/src/Linkerman.php +++ b/src/Linkerman.php @@ -13,10 +13,9 @@ class Linkerman { - const VERSION = '0.2.1'; + const VERSION = '0.2.2'; const REWRITE_FUNCTIONS = [ - 'register_shutdown_function', 'set_time_limit', 'header', 'header_remove', diff --git a/tests/Server.php b/tests/Server.php index c971559..082ff62 100644 --- a/tests/Server.php +++ b/tests/Server.php @@ -188,18 +188,19 @@ function func_move_uploaded_file(): string { $result = []; if (isset($_FILES['file_test']['tmp_name'])) { - $result[] = move_uploaded_file($_FILES['file_test']['tmp_name'], "/tmp/test.file"); + $tmpfile = tempnam(sys_get_temp_dir(), 'linkerman'); + $result[] = move_uploaded_file($_FILES['file_test']['tmp_name'], $tmpfile); $result[] = is_uploaded_file($_FILES['file_test']['tmp_name']); $result[] = file_exists($_FILES['file_test']['tmp_name']); - $result[] = file_exists("/tmp/test.file"); - unlink("/tmp/test.file"); + $result[] = file_exists($tmpfile); + unlink($tmpfile); } return json_encode($result); } function func_register_shutdown_function(): string { - register_shutdown_function(static function ($name) { + register_shutdown_function_user(static function ($name) { header("x-shutdown: $name"); }, 'linkerman'); return (string)count(Http::$shutdownCallbacks);