From 1e8dbecbf526da820077efffaac7163260e764c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 13 Aug 2021 14:13:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Client/{Help => }/Client.php | 2 +- .../Client/{Help => }/ClientAbstracts.php | 2 +- HttpServer/Client/{Help => }/Curl.php | 20 +- HttpServer/Client/Http2.php | 327 ------------------ HttpServer/Client/HttpClient.php | 31 +- HttpServer/Client/{Help => }/IClient.php | 2 +- HttpServer/Client/{Help => }/Result.php | 20 +- System/Abstracts/BaseApplication.php | 5 - System/Abstracts/TraitApplication.php | 13 +- 9 files changed, 48 insertions(+), 374 deletions(-) rename HttpServer/Client/{Help => }/Client.php (99%) rename HttpServer/Client/{Help => }/ClientAbstracts.php (99%) rename HttpServer/Client/{Help => }/Curl.php (92%) delete mode 100644 HttpServer/Client/Http2.php rename HttpServer/Client/{Help => }/IClient.php (99%) rename HttpServer/Client/{Help => }/Result.php (88%) diff --git a/HttpServer/Client/Help/Client.php b/HttpServer/Client/Client.php similarity index 99% rename from HttpServer/Client/Help/Client.php rename to HttpServer/Client/Client.php index 9cc8ee53..4cec6a24 100644 --- a/HttpServer/Client/Help/Client.php +++ b/HttpServer/Client/Client.php @@ -7,7 +7,7 @@ */ declare(strict_types=1); -namespace HttpServer\Client\Help; +namespace HttpServer\Client; use Exception; use JetBrains\PhpStorm\Pure; diff --git a/HttpServer/Client/Help/ClientAbstracts.php b/HttpServer/Client/ClientAbstracts.php similarity index 99% rename from HttpServer/Client/Help/ClientAbstracts.php rename to HttpServer/Client/ClientAbstracts.php index 38644083..023468cc 100644 --- a/HttpServer/Client/Help/ClientAbstracts.php +++ b/HttpServer/Client/ClientAbstracts.php @@ -1,7 +1,7 @@ matchHost($path); @@ -68,10 +69,10 @@ class Curl extends ClientAbstracts /** * @param $resource - * @return bool + * @return void * @throws Exception */ - private function curlHandlerSslSet($resource): mixed + private function curlHandlerSslSet($resource): void { if (!empty($this->ssl_key)) { if (!file_exists($this->ssl_key)) { @@ -85,7 +86,6 @@ class Curl extends ClientAbstracts } curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile()); } - return $resource; } @@ -93,10 +93,10 @@ class Curl extends ClientAbstracts * @param $resource * @param $path * @param $method - * @return resource + * @return CurlHandle * @throws Exception */ - private function do($resource, $path, $method) + private function do($resource, $path, $method): CurlHandle { curl_setopt($resource, CURLOPT_URL, $path); curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 @@ -152,7 +152,7 @@ class Curl extends ClientAbstracts * @return mixed * @throws Exception */ - private function parseResponse($curl, $output, $params = []): mixed + private function parseResponse($curl, $output, array $params = []): mixed { curl_close($curl); if ($output === FALSE) { @@ -199,7 +199,7 @@ class Curl extends ClientAbstracts private function headerFormat($headers): array { $_tmp = []; - foreach ($headers as $key => $val) { + foreach ($headers as $val) { $trim = explode(': ', trim($val)); $_tmp[strtolower($trim[0])] = $trim[1] ?? ''; diff --git a/HttpServer/Client/Http2.php b/HttpServer/Client/Http2.php deleted file mode 100644 index 21aeaeeb..00000000 --- a/HttpServer/Client/Http2.php +++ /dev/null @@ -1,327 +0,0 @@ -_clients = []; - } - - - /** - * 清空 - */ - public function clean() - { - foreach ($this->_clients as $client) { - /** @var H2Client $client */ - $client->close(); - } - $this->_clients = []; - } - - - /** - * @param bool $isRev - * @return Http2 - */ - public function setIsRev(bool $isRev): static - { - Context::setContext('http2isRev', $isRev); - return $this; - } - - - /** - * @param int $timeout - * @return Http2 - */ - public function setTimeout(int $timeout): static - { - Context::setContext('http2timeout', $timeout); - return $this; - } - - - /** - * @param array $headers - * @return Http2 - */ - public function setHeader(array $headers): static - { - Context::setContext('http2Headers', $headers); - return $this; - } - - - /** - * @param $domain - * @param $path - * @param array $params - * @param int $timeout - * @return Result - * @throws Exception - */ - public function get($domain, $path, array $params = [], int $timeout = -1): Result - { - $request = $this->dispatch($domain, $path, 'GET', $params, $timeout); - - return new Result(['code' => 0, 'data' => $request]); - } - - - /** - * @param $domain - * @param $path - * @param array $params - * @param int $timeout - * @return Result - * @throws Exception - */ - public function post($domain, $path, array $params = [], int $timeout = -1): Result - { - $request = $this->dispatch($domain, $path, 'POST', $params, $timeout); - - return new Result(['code' => 0, 'data' => $request]); - } - - - /** - * @param $domain - * @param $path - * @param array $params - * @param int $timeout - * @return Result - * @throws Exception - */ - public function upload($domain, $path, array $params = [], int $timeout = -1): Result - { - $request = $this->dispatch($domain, $path, 'POST', $params, $timeout, true); - - return new Result(['code' => 0, 'data' => $request]); - } - - - /** - * @param $domain - * @param $path - * @param array $params - * @param int $timeout - * @return Result - * @throws Exception - */ - public function delete($domain, $path, array $params = [], int $timeout = -1): Result - { - $request = $this->dispatch($domain, $path, 'DELETE', $params, $timeout); - - return new Result(['code' => 0, 'data' => $request]); - } - - - /** - * @param $domain - * @param $path - * @param $method - * @param array $params - * @param int $timeout - * @param bool $isUpload - * @return mixed - * @throws Exception - */ - private function dispatch($domain, $path, $method, array $params = [], int $timeout = -1, bool $isUpload = false): mixed - { - [$domain, $isSsl] = $this->clear($domain); - - $request = $this->getRequest($path, $method, $params, $isUpload); - $request->headers = array_merge($request->headers, [ - 'Host' => $domain - ]); - return $this->doRequest($request, $domain, $isSsl, $timeout); - } - - - /** - * @param $domain - * @return array - */ - private function clear($domain): array - { - if (str_starts_with($domain, 'https://')) { - return [str_replace('https://', '', $domain), true]; - } else { - return [str_replace('http://', '', $domain), false]; - } - } - - - /** - * @param Request $request - * @param $domain - * @param $ssl - * @param $timeout - * @return mixed - * @throws Exception - */ - private function doRequest(Request $request, $domain, $ssl, $timeout): mixed - { - $client = $this->getClient($domain, $ssl, $timeout); - $client->send($request); - if (Context::getContext('http2isRev') === false) { - return null; - } - return $this->rev($client); - } - - - /** - * @param $client - * @return mixed - * @throws Exception - */ - private function rev($client): mixed - { - /** @var Response $response */ - if (!Context::hasContext('http2timeout')) { - $response = $client->recv(); - } else { - $response = $client->recv((int)Context::getContext('http2timeout')); - } - if ($response === false || $response->statusCode > 200) { - throw new Exception($client->errMsg, $client->errCode); - } - $header = $response->headers['content-type']; - if (str_starts_with($header, 'application/json;')) { - return Json::decode($response->data); - } else if (str_starts_with($header, 'application/xml;')) { - return Xml::toArray($response->data); - } else { - return $response->data; - } - } - - - /** - * @param $domain - * @param $path - * @param array $params - * @param int $timeout - * @return mixed - * @throws Exception - */ - public function put($domain, $path, array $params = [], int $timeout = -1): Result - { - $request = $this->dispatch($domain, $path, 'PUT', $params, $timeout); - - return new Result(['code' => 0, 'data' => $request]); - } - - - /** - * @param $path - * @param $method - * @param $params - * @param bool $isUpload - * @return Request - * @throws Exception - */ - public function getRequest($path, $method, $params, bool $isUpload = false): Request - { - if (!str_starts_with($path, '/')) { - $path = '/' . $path; - } - - $request = new Request(); - $request->method = $method; - $request->path = $path; - if ($method === 'GET') { - $request->path .= '?' . http_build_query($params); - } else { - $request->data = !is_string($params) && !$isUpload ? Json::encode($params) : $params; - } - $request->headers = Context::getContext('http2Headers'); - return $request; - } - - - /** - * @param $domain - * @param bool $isSsl - * @param int $timeout - * @return H2Client - * @throws Exception - */ - private function getClient($domain, bool $isSsl = false, int $timeout = -1): H2Client - { - if (isset($this->_clients[$domain])) { - return $this->_clients[$domain]; - } - $client = $this->newRequest($domain, $isSsl, $timeout); - if ((!$client->connected || !$client->ping()) && !$client->connect()) { - throw new Exception($client->errMsg, $client->errCode); - } - return $this->_clients[$domain] = $client; - } - - - /** - * @param $domain - * @param $isSsl - * @param $timeout - * @return H2Client - */ - public function newRequest($domain, $isSsl, $timeout): H2Client - { - $domain = rtrim($domain, '/'); - if (str_contains($domain, ':')) { - [$domain, $port] = explode(':', $domain); - } else { - $port = $isSsl === true ? 443 : 80; - } - $client = new H2Client($domain, (int)$port, $isSsl); - $client->set(['timeout' => $timeout, 'ssl_host_name' => $domain]); - return $client; - } - - -} diff --git a/HttpServer/Client/HttpClient.php b/HttpServer/Client/HttpClient.php index bbc2da7d..272182d8 100644 --- a/HttpServer/Client/HttpClient.php +++ b/HttpServer/Client/HttpClient.php @@ -4,13 +4,12 @@ namespace HttpServer\Client; -use Exception; +use HttpServer\Client\Client; +use HttpServer\Client\Curl; +use HttpServer\Client\IClient; +use JetBrains\PhpStorm\Pure; use Kiri\Abstracts\Component; -use Kiri\Kiri; use Swoole\Coroutine; -use HttpServer\Client\Help\IClient; -use HttpServer\Client\Help\Client; -use HttpServer\Client\Help\Curl; /** * Class ClientDriver @@ -24,17 +23,29 @@ class HttpClient extends Component */ public static function NewRequest(): IClient { - return Coroutine::getCid() > -1 ? Client::NewRequest() : Curl::NewRequest(); + if (Coroutine::getCid() > -1) { + return Client::NewRequest(); + } + return Curl::NewRequest(); } /** - * @return Http2 - * @throws Exception + * @return Curl */ - public static function http2(): Http2 + #[Pure] public function getCurl(): Curl { - return Kiri::app()->get('http2'); + return Curl::NewRequest(); } + + /** + * @return Client + */ + #[Pure] public function getCoroutine(): Client + { + return Client::NewRequest(); + } + + } diff --git a/HttpServer/Client/Help/IClient.php b/HttpServer/Client/IClient.php similarity index 99% rename from HttpServer/Client/Help/IClient.php rename to HttpServer/Client/IClient.php index e6c34f91..80d4d8ed 100644 --- a/HttpServer/Client/Help/IClient.php +++ b/HttpServer/Client/IClient.php @@ -1,7 +1,7 @@ httpIsOk()) { return false; @@ -158,17 +158,17 @@ class Result } /** - * @return mixed + * @return string */ - public function getMessage(): mixed + public function getMessage(): string { return $this->message; } /** - * @return mixed + * @return string|int */ - public function getCode(): mixed + public function getCode(): string|int { return $this->code; } diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index 7795f934..5f5c8af8 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -13,12 +13,10 @@ namespace Kiri\Abstracts; use Annotation\Annotation as SAnnotation; use Database\Connection; use Exception; -use HttpServer\Client\Http2; use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; use HttpServer\Http\Response; -use HttpServer\HttpFilter; use HttpServer\Route\Router; use HttpServer\Server; use HttpServer\Shutdown; @@ -477,9 +475,6 @@ abstract class BaseApplication extends Component 'kafka-container' => ['class' => KafkaProvider::class], 'response' => ['class' => Response::class], 'request' => ['class' => Request::class], - 'rpc' => ['class' => Producer::class], - 'rpc-service' => ['class' => Service::class], - 'http2' => ['class' => Http2::class], 'shutdown' => ['class' => Shutdown::class], ]); } diff --git a/System/Abstracts/TraitApplication.php b/System/Abstracts/TraitApplication.php index 366c0044..b6aa9688 100644 --- a/System/Abstracts/TraitApplication.php +++ b/System/Abstracts/TraitApplication.php @@ -6,23 +6,20 @@ namespace Kiri\Abstracts; use Annotation\Annotation as SAnnotation; use Database\DatabasesProviders; -use HttpServer\Client\Help\Client; -use HttpServer\Client\Help\Curl; -use HttpServer\Client\Http2; +use HttpServer\Client\Client; +use HttpServer\Client\Curl; use HttpServer\Http\Request; use HttpServer\Http\Response; use HttpServer\HttpFilter; use HttpServer\Route\Router; use HttpServer\Server; use HttpServer\Shutdown; -use Rpc\Producer as RPCProducer; +use Kiri\Crontab\Producer; use Kiri\Async; use Kiri\Cache\Redis; use Kiri\Error\Logger; use Kiri\Event; use Kiri\Jwt\Jwt; -use Kiri\Pool\Connection; -use Kiri\Pool\Pool; /** * Trait TraitApplication @@ -38,14 +35,12 @@ use Kiri\Pool\Pool; * @property Logger $logger * @property Jwt $jwt * @property SAnnotation $annotation - * @property Http2 $http2 * @property BaseGoto $goto * @property Client $client * @property \Database\Connection $databases * @property Curl $curl - * @property \Kiri\Crontab\Producer $crontab + * @property Producer $crontab * @property HttpFilter $filter - * @property RPCProducer $rpc * @property Shutdown $shutdown */ trait TraitApplication