From f5ae86c3d2d9986591141eac57b91b0cc961b8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 24 Dec 2021 14:13:34 +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 --- src/AsyncClient.php | 231 +++++++++++++++++++++++--------------------- 1 file changed, 119 insertions(+), 112 deletions(-) diff --git a/src/AsyncClient.php b/src/AsyncClient.php index 335cb9b..26cd10f 100644 --- a/src/AsyncClient.php +++ b/src/AsyncClient.php @@ -23,136 +23,143 @@ class AsyncClient extends ClientAbstracts { - use TSwooleClient; + use TSwooleClient; - /** - * @param string $method - * @param $path - * @param array $params - * @return void - * @throws Exception - */ - public function request(string $method, $path, array $params = []): void - { - $this->withMethod($method) - ->coroutine( - $this->matchHost($path), - $this->paramEncode($params) - ); - } + /** + * @param string $method + * @param $path + * @param array $params + * @return void + * @throws Exception + */ + public function request(string $method, $path, array $params = []): void + { + $this->withMethod($method) + ->coroutine( + $this->matchHost($path), + $this->paramEncode($params) + ); + } - /** - * @param $path - * @return $this - */ - public function withCAInfo($path): static - { - return $this; - } + /** + * @param $path + * @return $this + */ + public function withCAInfo($path): static + { + return $this; + } - /** - * @param $url - * @param array|string $data - * @throws Exception 使用swoole协程方式请求 - */ - private function coroutine($url, array|string $data = []): void - { - try { - $this->generate_client($data, ...$url); - } catch (\Throwable $exception) { - Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); - $this->setStatusCode(-1); - $this->setBody(jTraceEx($exception)); - } - } + /** + * @param $url + * @param array|string $data + * @throws Exception 使用swoole协程方式请求 + */ + private function coroutine($url, array|string $data = []): void + { + try { + $this->generate_client($data, ...$url); + } catch (\Throwable $exception) { + Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); + $this->setStatusCode(-1); + $this->setBody(jTraceEx($exception)); + } + } - /** - * @param $data - * @param $host - * @param $isHttps - * @param $path - * @throws Exception - */ - private function generate_client($data, $host, $isHttps, $path): void - { - $this->client = new SwowClient(SWOOLE_TCP, FALSE); - if (!$this->client->connect($host, $this->getPort())) { - throw new Exception('链接失败'); - } - if ($isHttps || $this->isSSL()) { - $this->client->enableSSL(); - } - $this->client->set($this->settings()); - if (!empty($this->getAgent())) { - $this->withAddedHeader('User-Agent', $this->getAgent()); - } + /** + * @param $data + * @param $host + * @param $isHttps + * @param $path + * @throws Exception + */ + private function generate_client($data, $host, $isHttps, $path): void + { + $this->client = new SwowClient(SWOOLE_TCP, FALSE); + if (!$this->client->connect($host, $this->getPort())) { + throw new Exception('链接失败'); + } + if ($isHttps || $this->isSSL()) { + $this->client->enableSSL(); + } + $this->client->set($this->settings()); + if (!empty($this->getAgent())) { + $this->withAddedHeader('User-Agent', $this->getAgent()); + } - $path = $this->setParams($path, $data); + $path = $this->setParams($path, $data); - $this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'); + $this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'); // $this->withAddedHeader('Accept-Encoding', 'gzip'); - $this->withAddedHeader('Content-Length', $this->getData()->getSize()); + $this->withAddedHeader('Content-Length', $this->getData()->getSize()); - $this->execute($path, $this->getData()->getContents()); - } + $this->execute($path, $this->getData()->getContents()); + } - /** - * @param string $path - * @param string $content - * @return void - */ - private function execute(string $path, string $content) - { - $array = []; - $array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1'; - if (!empty($this->getHeader())) { - foreach ($this->getHeader() as $key => $value) { - $array[] = sprintf('%s: %s', $key, $value); - } - } - $this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content); - $revice = $this->client->recv(); + /** + * @param string $path + * @param string $content + * @return void + */ + private function execute(string $path, string $content) + { + $array = []; + $array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1'; + if (!empty($this->getHeader())) { + foreach ($this->getHeader() as $key => $value) { + $array[] = sprintf('%s: %s', $key, $value); + } + } + $this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content); + $revice = $this->client->recv(); - var_dump($revice); + [$header, $body] = explode("\r\n\r\n", $revice); - [$header, $body] = explode("\r\n\r\n", $revice); + $header = explode("\r\n", $header); + $status = array_shift($header); - $header = explode("\r\n", $header); - $status = array_shift($header); + $this->setStatusCode(intval(explode(' ', $status)[1])); + $this->setResponseHeader($header); - $this->setBody($body); - $this->setStatusCode(intval(explode(' ', $status)[1])); - $this->setResponseHeader($header); - } + if ($this->getResponseHeader('Transfer-Encoding') == 'chunked') { + $explode = explode("\r\n\r\n", str_replace("0\r\n\r\n", '', $body)); + $string = []; + foreach ($explode as $value) { + $string[] = explode("\r\n", $value)[1]; + } + $body = implode($string); + } + $this->setBody($body); + } - /** - * @param $path - * @param $data - * @return string - */ - private function setParams($path, $data): string - { - if ($this->isGet()) { - if (!empty($data)) $path .= '?' . $data; - } else { - $data = $this->mergeParams($data); - if (!empty($data)) { - $this->withBody(new Stream($data)); - } - } - return $path; - } + /** + * @param $path + * @param $data + * @return string + */ + private function setParams($path, $data): string + { + if ($this->isGet()) { + if (!empty($data)) $path .= '?' . $data; + } else { + $data = $this->mergeParams($data); + if (!empty($data)) { + $this->withBody(new Stream($data)); + } + } + return $path; + } - /** - * - */ - public function close(): void - { - $this->client->close(); - } + /** + * + */ + public function close(): void + { + $this->client->close(); + } }