Skip to content

Commit

Permalink
Rename register_shutdown_function
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanguangcheng committed Dec 28, 2023
1 parent 96ddf0d commit 1378f4b
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion php.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion phpunit.xml → phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>
<testsuites>
<testsuite name="Test Suite">
Expand All @@ -11,7 +14,6 @@
</testsuites>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
Expand Down
4 changes: 3 additions & 1 deletion src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @author zhanguangcheng<[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @noinspection PhpRedeclarationStdlibFunctionInspection
* @noinspection PhpUnusedParameterInspection
* @noinspection PhpUnused
*/

use Linkerman\ExitException;
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'];
}
}
}
3 changes: 1 addition & 2 deletions src/Linkerman.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions tests/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1378f4b

Please sign in to comment.