Compare commits

...

40 Commits

Author SHA1 Message Date
as2252258 ae3b0c411b Revert "改名"
This reverts commit fdf58326
2022-01-10 11:39:56 +08:00
as2252258 f838795983 e 2022-01-09 13:54:34 +08:00
as2252258 1db430f997 1 2022-01-09 02:44:07 +08:00
as2252258 fdb8816b5e Revert "改名"
This reverts commit fdf58326
2022-01-08 18:49:06 +08:00
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
11 changed files with 1710 additions and 1698 deletions
+177
View File
@@ -0,0 +1,177 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use Kiri\Message\Stream;
use Kiri\Abstracts\Logger;
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) {
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(array_merge($this->settings(), ['open_http_protocol' => true]));
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');
// $this->withAddedHeader('Accept-Encoding', 'gzip');
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
$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);
$receive = $this->client->recv();
[$header, $body] = explode("\r\n\r\n", $receive);
$header = explode("\r\n", $header);
$status = array_shift($header);
$this->setStatusCode(intval(explode(' ', $status)[1]));
$this->parseResponseHeaders($header);
$this->setBody($body);
}
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
{
$this->client->close();
}
}
+4 -4
View File
@@ -1,18 +1,18 @@
<?php
namespace Http\Client;
namespace Kiri;
use Kiri\Context;
/**
* @mixin CoroutineClient|Curl
* @mixin CoroutineClient|CurlClient
*/
class Client
{
private CoroutineClient|Curl|AsyncClient $abstracts;
private CoroutineClient|CurlClient|AsyncClient $abstracts;
/**
@@ -25,7 +25,7 @@ class Client
if (Context::inCoroutine()) {
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
} else {
$this->abstracts = new AsyncClient($host, $port, $isSsl);
$this->abstracts = new CurlClient($host, $port, $isSsl);
}
}
@@ -1,11 +1,11 @@
<?php
namespace Http\Client;
namespace Kiri;
use Closure;
use Http\Message\Stream;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Kiri\Context;
use Kiri\Core\Help;
@@ -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;
@@ -7,7 +7,7 @@
*/
declare(strict_types=1);
namespace Http\Client;
namespace Kiri;
use Exception;
use JetBrains\PhpStorm\Pure;
+5 -8
View File
@@ -1,21 +1,21 @@
<?php
declare(strict_types=1);
namespace Http\Client;
namespace Kiri;
use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use Kiri\Message\Response;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/**
* Class Curl
* Class CurlClient
* @package Http\Handler\Client
*/
class Curl extends ClientAbstracts
class CurlClient extends ClientAbstracts
{
/**
@@ -74,9 +74,6 @@ class Curl extends ClientAbstracts
*/
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())) {
curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
}
+1 -1
View File
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);
namespace Http\Client;
namespace Kiri;
use Exception;
+1 -1
View File
@@ -1,7 +1,7 @@
<?php
namespace Http\Client;
namespace Kiri;
use Closure;
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Http\Client;
namespace Kiri;
use JetBrains\PhpStorm\Pure;
+1 -2
View File
@@ -11,14 +11,13 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-redis": "*",
"ext-swoole": "*",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
"autoload": {
"psr-4": {
"Http\\Client\\": "src/"
"Kiri\\": "./"
}
},
"require-dev": {
-161
View File
@@ -1,161 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Http\Client;
use Exception;
use Http\Message\Stream;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
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) {
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());
} 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);
$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-Encoding', 'gzip, deflate');
$this->withAddedHeader('Content-Length', $this->getData()->getSize());
$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);
}
}
$array = implode("\r\n", $array) . "\r\n\r\n";
$this->client->send($array . $content);
$revice = $this->client->recv();
[$header, $body] = explode("\r\n\r\n", $revice);
$header = explode("\r\n", $header);
$status = array_shift($header);
$this->setBody($body);
$this->setStatusCode(intval(explode(' ', $status)[1]));
$this->setResponseHeader($header);
}
/**
* @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();
}
}