2022-01-09 13:54:34 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/5/24 0024
|
|
|
|
|
* Time: 11:34
|
|
|
|
|
*/
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2022-01-10 11:39:56 +08:00
|
|
|
namespace Kiri;
|
2022-01-09 13:54:34 +08:00
|
|
|
|
|
|
|
|
use Exception;
|
2022-01-11 15:24:22 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2022-01-09 13:54:34 +08:00
|
|
|
use Kiri\Abstracts\Logger;
|
2022-01-11 15:04:16 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
2022-01-10 18:44:20 +08:00
|
|
|
use Kiri\Message\Stream;
|
2022-01-09 13:54:34 +08:00
|
|
|
use Swoole\Client as SwowClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Client
|
|
|
|
|
* @package Kiri\Kiri\Http
|
|
|
|
|
*/
|
|
|
|
|
class AsyncClient extends ClientAbstracts
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 $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) {
|
2022-01-11 16:16:04 +08:00
|
|
|
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
|
2022-01-09 13:54:34 +08:00
|
|
|
$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);
|
2022-01-11 15:28:30 +08:00
|
|
|
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
|
2022-01-10 18:36:35 +08:00
|
|
|
if (!$this->client->connect($host, $this->getPort())) {
|
|
|
|
|
throw new Exception('链接失败');
|
|
|
|
|
}
|
2022-01-10 18:51:56 +08:00
|
|
|
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
|
2022-01-09 13:54:34 +08:00
|
|
|
if (!empty($this->getAgent())) {
|
|
|
|
|
$this->withAddedHeader('User-Agent', $this->getAgent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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');
|
2022-01-11 14:44:37 +08:00
|
|
|
// $this->withAddedHeader('Accept-Encoding', 'gzip');
|
2022-01-09 13:54:34 +08:00
|
|
|
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
|
|
|
|
|
|
|
|
|
|
$this->execute($path, $this->getData()->getContents());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $content
|
|
|
|
|
* @return void
|
2022-01-11 15:04:16 +08:00
|
|
|
* @throws ConfigException
|
2022-01-09 13:54:34 +08:00
|
|
|
*/
|
|
|
|
|
private function execute(string $path, string $content)
|
|
|
|
|
{
|
2022-01-11 15:24:22 +08:00
|
|
|
$array = $this->_parseHeaders($path);
|
|
|
|
|
|
2022-01-11 15:44:07 +08:00
|
|
|
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content . "\r\n\r\n");
|
|
|
|
|
$receive = '';
|
2022-01-11 15:44:56 +08:00
|
|
|
while ($this->client->isConnected()) {
|
2022-01-11 15:44:07 +08:00
|
|
|
$_tmp = $this->client->recv();
|
|
|
|
|
if (empty($_tmp)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$receive .= $_tmp;
|
|
|
|
|
}
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-01-11 15:41:53 +08:00
|
|
|
Kiri::getDi()->get(Logger::class)->debug(implode("\r\n", $array) . "\r\n\r\n" . $content);
|
2022-01-11 15:35:14 +08:00
|
|
|
Kiri::getDi()->get(Logger::class)->debug($receive);
|
|
|
|
|
[$header, $body] = explode("\r\n\r\n", $receive);
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-01-11 15:35:14 +08:00
|
|
|
$header = explode("\r\n", $header);
|
|
|
|
|
$status = array_shift($header);
|
2022-01-09 13:54:34 +08:00
|
|
|
|
2022-01-11 15:35:14 +08:00
|
|
|
$this->setStatusCode(intval(explode(' ', $status)[1]));
|
|
|
|
|
$this->parseResponseHeaders($header);
|
|
|
|
|
$this->setBody($body);
|
2022-01-09 13:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-11 15:24:22 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] private function _parseHeaders(string $path): array
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
|
|
|
|
|
if (!empty($this->getHeader())) {
|
2022-01-11 15:40:55 +08:00
|
|
|
$headers = $this->getHeader();
|
|
|
|
|
foreach ($headers as $key => $value) {
|
2022-01-11 15:24:22 +08:00
|
|
|
$array[] = sprintf('%s: %s', $key, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-01-11 15:31:37 +08:00
|
|
|
* @param $client
|
2022-01-11 15:24:22 +08:00
|
|
|
* @param $string
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2022-01-11 15:32:19 +08:00
|
|
|
private function waite(&$client, $string): mixed
|
2022-01-11 15:24:22 +08:00
|
|
|
{
|
2022-01-11 15:31:37 +08:00
|
|
|
$tmp = $client->recv();
|
|
|
|
|
if (!empty($tmp)) {
|
|
|
|
|
return $this->waite($client, $string . $tmp);
|
2022-01-11 15:24:22 +08:00
|
|
|
}
|
2022-01-11 15:31:37 +08:00
|
|
|
return $string;
|
2022-01-11 15:24:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-09 13:54:34 +08:00
|
|
|
private function chunked()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $headers
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function parseResponseHeaders(array $headers)
|
|
|
|
|
{
|
|
|
|
|
$array = [];
|
|
|
|
|
foreach ($headers as $header) {
|
|
|
|
|
[$key, $value] = explode(': ', $header);
|
|
|
|
|
|
|
|
|
|
$array[$key] = trim($value);
|
|
|
|
|
}
|
|
|
|
|
$this->setResponseHeader($array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
|
|
{
|
2022-01-10 18:47:31 +08:00
|
|
|
/** @var SwowClient $client */
|
|
|
|
|
$client = $this->client;
|
|
|
|
|
if (!$client || !$client->isConnected()) {
|
2022-01-10 18:44:20 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-01-10 18:47:31 +08:00
|
|
|
$client->close();
|
2022-01-09 13:54:34 +08:00
|
|
|
}
|
|
|
|
|
}
|