diff --git a/HttpServer/Client/Client.php b/HttpServer/Client/Client.php index f7b7499a..f4d81ef5 100644 --- a/HttpServer/Client/Client.php +++ b/HttpServer/Client/Client.php @@ -45,6 +45,25 @@ class Client private $_message = ''; private $_data = ''; + private $connect_timeout = 1; + + /** + * @return int + */ + public function getConnectTimeout(): int + { + return $this->connect_timeout; + } + + /** + * @param int $connect_timeout + */ + public function setConnectTimeout(int $connect_timeout): void + { + $this->connect_timeout = $connect_timeout; + } + + /** * @return string */ @@ -212,7 +231,7 @@ class Client if (empty($this->host) || empty($this->port)) { return new Result(['code' => 500, 'message' => 'Host and port is null']); } - if (!$client->connect($this->host, $this->port)) { + if (!$client->connect($this->host, $this->port, $this->connect_timeout)) { return new Result(['code' => 500, 'message' => $client->errMsg]); } @@ -601,8 +620,8 @@ class Client $sslCa = $this->getCa(); $params = []; - if ($this->timeout > 0) { - $params['timeout'] = $this->timeout; + if ($this->connect_timeout > 0) { + $params['timeout'] = $this->connect_timeout; } if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { return $params; diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php index 8c3c7938..13a99003 100644 --- a/HttpServer/Events/OnRequest.php +++ b/HttpServer/Events/OnRequest.php @@ -36,21 +36,8 @@ class OnRequest extends Callback public function onHandler(Request $request, Response $response) { try { - register_shutdown_function(function ($response) { - $error = error_get_last(); - if (!isset($error['type'])) { - return; - } - $types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR]; - if (!in_array($error['type'], $types)) { - return; - } - if ($response instanceof Response) { - $response->status(500); - $response->end($error['message']); - } - unset($response); - }, $response); + register_shutdown_function([static::class, 'shutdown'], $response); + /** @var HRequest $sRequest */ [$sRequest, $sResponse] = static::setContext($request, $response); if ($sRequest->is('favicon.ico')) { @@ -69,6 +56,27 @@ class OnRequest extends Callback } + /** + * @param $response + */ + public static function shutdown($response) + { + $error = error_get_last(); + if (!isset($error['type'])) { + return; + } + $types = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR]; + if (!in_array($error['type'], $types)) { + return; + } + if ($response instanceof Response) { + $response->status(500); + $response->end($error['message']); + } + unset($response); + } + + /** * @param $sResponse * @param $exception