Skip to content

Commit d7cc297

Browse files
committed
Fix bugs and add execution time measureing in PHP worker
1 parent 24e0506 commit d7cc297

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

assets/php/index.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
* Created at: 2022-10-06
99
* Updated at: 2024-11-26
1010
*/
11-
12-
define("PHP_HTTPPROXY_VERSION", "0.1.6.5");
11+
define("PERF_START_TIME", microtime(true));
12+
define("PHP_HTTPPROXY_VERSION", "0.1.6.6");
1313
define("DEFAULT_SOCKET_TIMEOUT", 1);
1414
define("STATEFUL_SOCKET_TIMEOUT", 30);
1515
define("MAX_EXECUTION_TIME", 0);
@@ -30,6 +30,11 @@
3030
exit('<!DOCTYPE html><html><head><title>It works!</title><meta charset="utf-8"></head><body><h1>It works!</h1><p><a href="https://github.com/gnh1201/caterpillar">Download the client</a></p><p>' . $_SERVER['HTTP_USER_AGENT'] . '</p><hr><p>' . DEFAULT_USER_AGENT . '</p></body></html>');
3131
}
3232

33+
function get_current_execution_time() {
34+
$end_time = microtime(true);
35+
return $end_time - PERF_START_TIME;
36+
}
37+
3338
function jsonrpc2_cast_to_array($data) {
3439
return is_array($data) ? $data : array($data);
3540
}
@@ -39,7 +44,8 @@ function jsonrpc2_encode($method, $params, $id = '') {
3944
"jsonrpc" => "2.0",
4045
"method" => $method,
4146
"params" => $params,
42-
"id" => $id
47+
"id" => $id,
48+
"_execution_time" => get_current_execution_time()
4349
);
4450
return json_encode($data);
4551
}
@@ -48,7 +54,8 @@ function jsonrpc2_result_encode($result, $id = '') {
4854
$data = array(
4955
"jsonrpc" => "2.0",
5056
"result" => $result,
51-
"id" => $id
57+
"id" => $id,
58+
"_execution_time" => get_current_execution_time()
5259
);
5360
return json_encode($data);
5461
}
@@ -57,7 +64,8 @@ function jsonrpc2_error_encode($error, $id = '') {
5764
$data = array(
5865
"jsonrpc" => "2.0",
5966
"error" => $error,
60-
"id" => $id
67+
"id" => $id,
68+
"_execution_time" => get_current_execution_time()
6169
);
6270
return json_encode($data);
6371
}
@@ -425,13 +433,13 @@ function relay_dns_get_record($params) {
425433
}
426434

427435
function relay_fetch_url($params) {
428-
$method = $params['method'];
429-
$url = $params['url'];
430-
$headers = $params['headers'];
431-
$data = $params['data'];
436+
$url = $params['url'];
437+
$method = (array_key_exists("method", $params) ? $params['method'] : "GET");
438+
$headers = (array_key_exists("headers", $params) ? $params['headers'] : array());
439+
$data = (array_key_exists("data", $params) ? $params['data'] : "");
432440

433441
$_headers = array();
434-
if (is_array($headers) && count($header) > 0) {
442+
if (is_array($headers) && count($headers) > 0) {
435443
foreach ($headers as $header_line) {
436444
$pos = strpos($header_line, ':');
437445
if ($pos !== false) {

0 commit comments

Comments
 (0)