Compare commits

...

36 Commits

Author SHA1 Message Date
as2252258 2623deb63f Revert "改名"
This reverts commit fdf58326
2021-12-28 12:06:53 +08:00
as2252258 98d9cee8b0 改名 2021-12-24 14:53:05 +08:00
as2252258 cec004b670 改名 2021-12-24 14:51:38 +08:00
as2252258 f268b705f7 改名 2021-12-24 14:51:11 +08:00
as2252258 ac279a257a 改名 2021-12-24 14:47:36 +08:00
as2252258 89ba6effeb 改名 2021-12-24 14:47:12 +08:00
as2252258 29e520b412 改名 2021-12-24 14:45:57 +08:00
as2252258 eec7cb7dcc 改名 2021-12-24 14:45:20 +08:00
as2252258 6c71437e9e 改名 2021-12-24 14:39:21 +08:00
as2252258 33fc0ae396 改名 2021-12-24 14:38:26 +08:00
as2252258 23f85ef5f0 改名 2021-12-24 14:32:49 +08:00
as2252258 372dbbb424 改名 2021-12-24 14:31:41 +08:00
as2252258 d07dbf1c8d 改名 2021-12-24 14:31:14 +08:00
as2252258 6ffe1af6d2 改名 2021-12-24 14:29:48 +08:00
as2252258 ca7a4494d8 改名 2021-12-24 14:23:28 +08:00
as2252258 4f99ec5756 改名 2021-12-24 14:19:20 +08:00
as2252258 1d1b36155a 改名 2021-12-24 14:17:01 +08:00
as2252258 00da068b53 改名 2021-12-24 14:16:07 +08:00
as2252258 08793bcf76 改名 2021-12-24 14:14:09 +08:00
as2252258 f5ae86c3d2 改名 2021-12-24 14:13:34 +08:00
as2252258 4856e76e6f 改名 2021-12-24 11:54:30 +08:00
as2252258 09dc2b4980 改名 2021-12-24 11:52:29 +08:00
as2252258 510daa1bb6 改名 2021-12-24 11:51:05 +08:00
as2252258 4dc1120aaf 改名 2021-12-24 11:50:45 +08:00
as2252258 9076b65080 改名 2021-12-24 11:50:17 +08:00
as2252258 adf09f5695 改名 2021-12-24 11:49:23 +08:00
as2252258 06290f5c9b 改名 2021-12-24 11:46:07 +08:00
as2252258 196cb42e6e 改名 2021-12-24 11:44:08 +08:00
as2252258 a0f197d98d 改名 2021-12-24 11:42:58 +08:00
as2252258 c2d5ab5e1b 改名 2021-12-24 11:38:15 +08:00
as2252258 597cacd191 改名 2021-12-24 11:34:29 +08:00
as2252258 f48b6bd5d2 改名 2021-12-23 18:01:07 +08:00
as2252258 2baeb20b8d 1 2021-12-12 04:14:22 +08:00
as2252258 8d685b93e2 1 2021-12-12 04:10:15 +08:00
as2252258 90cada46c4 1 2021-12-12 04:07:34 +08:00
as2252258 0f5d277532 1 2021-12-12 04:06:37 +08:00
4 changed files with 140 additions and 126 deletions
+134 -117
View File
@@ -23,139 +23,156 @@ class AsyncClient extends ClientAbstracts
{ {
use TSwooleClient; use TSwooleClient;
/** /**
* @param string $method * @param string $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
public function request(string $method, $path, array $params = []): void public function request(string $method, $path, array $params = []): void
{ {
$this->withMethod($method) $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $this->matchHost($path),
$this->paramEncode($params) $this->paramEncode($params)
); );
} }
/** /**
* @param $path * @param $path
* @return $this * @return $this
*/ */
public function withCAInfo($path): static public function withCAInfo($path): static
{ {
return $this; return $this;
} }
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
* @throws Exception 使用swoole协程方式请求 * @throws Exception 使用swoole协程方式请求
*/ */
private function coroutine($url, array|string $data = []): void private function coroutine($url, array|string $data = []): void
{ {
try { try {
$this->generate_client($data, ...$url); $this->generate_client($data, ...$url);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
$this->setStatusCode(-1); $this->setStatusCode(-1);
$this->setBody(jTraceEx($exception)); $this->setBody(jTraceEx($exception));
} }
} }
/** /**
* @param $data * @param $data
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path * @param $path
* @throws Exception * @throws Exception
*/ */
private function generate_client($data, $host, $isHttps, $path): void private function generate_client($data, $host, $isHttps, $path): void
{ {
$this->client = new SwowClient(SWOOLE_TCP, FALSE); $this->client = new SwowClient(SWOOLE_TCP, FALSE);
if (!$this->client->connect($host, $this->getPort())) { if (!$this->client->connect($host, $this->getPort())) {
throw new Exception('链接失败'); throw new Exception('链接失败');
} }
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$this->client->enableSSL(); $this->client->enableSSL();
} }
$this->client->set($this->settings()); $this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent()); $this->withAddedHeader('User-Agent', $this->getAgent());
} else { }
$this->withAddedHeader('User-Agent', ' Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36');
}
$path = $this->setParams($path, $data); $path = $this->setParams($path, $data);
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,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, deflate'); // $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 $path
* @param string $content * @param string $content
* @return void * @return void
*/ */
private function execute(string $path, string $content) private function execute(string $path, string $content)
{ {
$array = []; $array = [];
$array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1'; $array[] = strtoupper($this->getMethod()) . ' ' . $path . ' HTTP/1.1';
if (!empty($this->getHeader())) { if (!empty($this->getHeader())) {
foreach ($this->getHeader() as $key => $value) { foreach ($this->getHeader() as $key => $value) {
$array[] = sprintf('%s: %s', $key, $value); $array[] = sprintf('%s: %s', $key, $value);
} }
} }
$array = implode("\r\n", $array) . "\r\n\r\n"; $this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content);
$this->client->send($array . $content); $receive = $this->client->recv();
$revice = $this->client->recv(); [$header, $body] = explode("\r\n\r\n", $receive);
[$header, $body] = explode("\r\n\r\n", $revice); $header = explode("\r\n", $header);
$status = array_shift($header);
$header = explode("\r\n", $header); $this->setStatusCode(intval(explode(' ', $status)[1]));
$this->parseResponseHeaders($header);
$status = array_shift($header); $this->setBody($body);
}
$this->setBody($body);
$this->setStatusCode(intval(explode(' ', $status)[1]));
$this->setResponseHeader($header);
}
/** private function chunked()
* @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 array $headers
*/ * @return void
public function close(): void */
{ private function parseResponseHeaders(array $headers)
$this->client->close(); {
} $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
{
$this->client->close();
}
} }
+3 -3
View File
@@ -6,13 +6,13 @@ use Kiri\Context;
/** /**
* @mixin CoroutineClient|Curl * @mixin CoroutineClient|CurlClient
*/ */
class Client class Client
{ {
private CoroutineClient|Curl|AsyncClient $abstracts; private CoroutineClient|CurlClient|AsyncClient $abstracts;
/** /**
@@ -25,7 +25,7 @@ class Client
if (Context::inCoroutine()) { if (Context::inCoroutine()) {
$this->abstracts = new CoroutineClient($host, $port, $isSsl); $this->abstracts = new CoroutineClient($host, $port, $isSsl);
} else { } else {
$this->abstracts = new AsyncClient($host, $port, $isSsl); $this->abstracts = new CurlClient($host, $port, $isSsl);
} }
} }
+1 -1
View File
@@ -68,7 +68,7 @@ abstract class ClientAbstracts implements IClient
/** /**
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client * @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
*/ */
protected mixed $client; protected mixed $client;
+2 -5
View File
@@ -12,10 +12,10 @@ use Psr\Http\Message\ResponseInterface;
/** /**
* Class Curl * Class CurlClient
* @package Http\Handler\Client * @package Http\Handler\Client
*/ */
class Curl extends ClientAbstracts class CurlClient extends ClientAbstracts
{ {
/** /**
@@ -74,9 +74,6 @@ class Curl extends ClientAbstracts
*/ */
private function curlHandlerSslSet(): void private function curlHandlerSslSet(): void
{ {
curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, $this->getVerifyPeer());
curl_setopt($this->client, CURLOPT_VERBOSE, TRUE);
// curl_setopt($this->client, CURLOPT_SSL_VERIFYHOST, $this->getVerifyPeer());
if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) { if (!empty($this->getSslKeyFile()) && file_exists($this->getSslKeyFile())) {
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile()); curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
} }