Compare commits

...

39 Commits

Author SHA1 Message Date
as2252258 196db497c1 eee 2023-12-18 18:21:09 +08:00
as2252258 6da3575619 eee 2023-12-12 15:35:36 +08:00
as2252258 9814e06e8d eee 2023-12-12 14:08:01 +08:00
as2252258 df32502723 eee 2023-11-09 14:46:15 +08:00
as2252258 7f702e5784 eee 2023-11-09 14:45:59 +08:00
as2252258 fd61b6f1d9 eee 2023-11-07 16:16:34 +08:00
as2252258 9e062a7a33 eee 2023-11-07 16:15:48 +08:00
as2252258 43c8f9b8e6 eee 2023-10-27 22:36:05 +08:00
as2252258 7b14fa075e eee 2023-10-27 22:23:11 +08:00
as2252258 165f62113c eee 2023-10-24 17:22:30 +08:00
as2252258 3c3b6f2e65 qqq 2023-08-24 11:25:42 +08:00
as2252258 2ca20d3a96 qqq 2023-08-18 19:53:00 +08:00
as2252258 b2800b806c qqq 2023-08-18 19:50:43 +08:00
as2252258 092daa022a qqq 2023-08-18 17:33:06 +08:00
as2252258 57337cbe16 qqq 2023-08-18 16:37:47 +08:00
as2252258 ad8b20723b qqq 2023-08-18 16:37:18 +08:00
as2252258 f2421b71e7 qqq 2023-08-18 16:36:05 +08:00
as2252258 02685a9e61 qqq 2023-08-18 15:45:30 +08:00
as2252258 1b5232f5e7 qqq 2023-08-18 15:43:52 +08:00
as2252258 c76fb40689 qqq 2023-08-17 16:32:02 +08:00
as2252258 18b44b32f2 qqq 2023-07-12 21:38:27 +08:00
as2252258 14574e7a6c qqq 2023-06-12 17:51:10 +08:00
as2252258 13fc812ae4 变更 2022-12-12 17:31:12 +08:00
as2252258 edd1751f7a 变更 2022-09-08 14:07:22 +08:00
as2252258 7dbce8b1e8 modify plugin name 2022-07-08 17:40:25 +08:00
as2252258 eb7ffecdb9 modify plugin name 2022-06-22 16:29:42 +08:00
as2252258 dd85596907 变更 2022-05-31 11:48:20 +08:00
as2252258 e6b10bbd70 变更 2022-05-31 08:24:44 +08:00
as2252258 c149afa3f8 变更 2022-05-31 08:23:26 +08:00
as2252258 f195d001c6 变更 2022-05-31 07:46:45 +08:00
as2252258 b45666d9db 变更 2022-05-31 07:46:01 +08:00
as2252258 650aaee974 modify plugin name 2022-03-01 18:47:45 +08:00
as2252258 d589e096af modify plugin name 2022-03-01 18:46:08 +08:00
as2252258 f17a55f8d0 modify plugin name 2022-02-21 15:39:55 +08:00
as2252258 3cc98214c7 modify plugin name 2022-02-21 15:23:41 +08:00
as2252258 aaeb538849 modify plugin name 2022-02-21 15:21:34 +08:00
as2252258 0c7b8eb57f modify plugin name 2022-02-21 15:17:42 +08:00
as2252258 48808ded98 modify plugin name 2022-02-18 13:53:51 +08:00
as2252258 eb25fe06ed modify plugin name 2022-02-18 13:45:30 +08:00
11 changed files with 629 additions and 701 deletions
+37
View File
@@ -0,0 +1,37 @@
# Created by .ignore support plugin (hsz.mobi)
### Yii template
assets/*
!assets/.gitignore
protected/runtime/*
!protected/runtime/.gitignore
protected/data/*.db
themes/classic/views/
### Example user template template
### Example user template
# IntelliJ project files
.idea
*.iml
out
gen
db/
async-queue/
composer.lock
*.log
commands/result
config/setting.php
tests/
vendor/*
runtime/
*.xml
*.lock
oot
d
composer.lock
-220
View File
@@ -1,220 +0,0 @@
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/5/24 0024
* Time: 11:34
*/
declare(strict_types=1);
namespace Kiri;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Exception\ConfigException;
use Kiri\Message\Stream;
use Swoole\Client as SwowClient;
/**
* Class Client
* @package 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', [error_trigger_format($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);
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
if (!$this->client->connect($host, $this->getPort())) {
throw new Exception('链接失败');
}
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
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
* @throws ConfigException
*/
private function execute(string $path, string $content)
{
$array = $this->_parseHeaders($path);
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content . "\r\n\r\n");
$receive = '';
while ($this->client->isConnected()) {
$_tmp = $this->client->recv();
if (empty($_tmp)) {
break;
}
$receive .= $_tmp;
}
Kiri::getDi()->get(Logger::class)->debug(implode("\r\n", $array) . "\r\n\r\n" . $content);
Kiri::getDi()->get(Logger::class)->debug($receive);
[$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);
}
/**
* @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())) {
$headers = $this->getHeader();
foreach ($headers as $key => $value) {
$array[] = sprintf('%s: %s', $key, $value);
}
}
return $array;
}
/**
* @param $client
* @param $string
* @return mixed
*/
private function waite(&$client, $string): mixed
{
$tmp = $client->recv();
if (!empty($tmp)) {
return $this->waite($client, $string . $tmp);
}
return $string;
}
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
{
/** @var SwowClient $client */
$client = $this->client;
if (!$client || !$client->isConnected()) {
return;
}
$client->close();
}
}
+8 -8
View File
@@ -2,7 +2,7 @@
namespace Kiri;
use Kiri\Context;
use Swoole\Coroutine;
/**
@@ -12,17 +12,17 @@ class Client
{
private CoroutineClient|CurlClient|AsyncClient $abstracts;
private CoroutineClient|CurlClient $abstracts;
/**
* @param string $host
* @param int $port
* @param bool $isSsl
*/
/**
* @param string $host
* @param int $port
* @param bool $isSsl
*/
public function __construct(string $host, int $port, bool $isSsl = false)
{
if (Context::inCoroutine()) {
if (class_exists(Coroutine::class) && Coroutine::getCid() > -1) {
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
} else {
$this->abstracts = new CurlClient($host, $port, $isSsl);
+169 -191
View File
@@ -5,12 +5,7 @@ namespace Kiri;
use Closure;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Kiri\Context;
use Kiri\Core\Help;
use Psr\Http\Message\StreamInterface;
use Swoole\Coroutine\System;
use CurlHandle;
defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/');
@@ -22,38 +17,32 @@ defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(
abstract class ClientAbstracts implements IClient
{
const POST = 'post';
const UPLOAD = 'upload';
const GET = 'get';
const DELETE = 'delete';
const OPTIONS = 'options';
const HEAD = 'head';
const PUT = 'put';
const string POST = 'post';
const string UPLOAD = 'upload';
const string GET = 'get';
const string DELETE = 'delete';
const string OPTIONS = 'options';
const string HEAD = 'head';
const string PUT = 'put';
private string $host = '';
private array $header = [];
private int $timeout = 0;
private string $method = 'get';
private bool $isSSL = FALSE;
private string $agent = '';
private string $ssl_cert_file = '';
private string $ssl_key_file = '';
private string $ca = '';
private int $port = 80;
private array $_responseHeader = [];
private int $statusCode = 200;
private bool $VERIFYPEER = TRUE;
private string $host = '';
private array $header = [];
private int $timeout = 0;
private string $method = 'get';
private bool $isSSL = FALSE;
private string $agent = '';
private string $ssl_cert_file = '';
private string $ssl_key_file = '';
private string $ca = '';
private int $port = 80;
protected int $num = 0;
private ?array $_responseHeader = [];
private int $statusCode = 200;
protected int $retryNum = 0;
protected int $retryTimeout = 0;
private bool $verifyPeer = TRUE;
private string $proxyHost = '';
private int $proxyPort = 0;
/**
@@ -62,24 +51,104 @@ abstract class ClientAbstracts implements IClient
protected ?string $body;
private ?StreamInterface $_data = NULL;
private string|array|null $_data = NULL;
private int $connect_timeout = 1;
/**
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|CurlHandle
*/
protected mixed $client;
/**
* @param $bool
* @param int $retryNum
* @return $this
*/
public function withVerifyPeer($bool): static
public function withRetryNum(int $retryNum): static
{
$this->VERIFYPEER = $bool;
$this->retryNum = $retryNum;
return $this;
}
/**
* @return string
*/
public function getProxyHost(): string
{
return $this->proxyHost;
}
/**
* @return string
*/
public function getProxyPort(): string
{
return $this->proxyPort;
}
/**
* @param string $proxy
* @return ClientAbstracts
*/
public function withProxyHost(string $proxy): static
{
$this->proxyHost = $proxy;
return $this;
}
/**
* @param int $proxy
* @return ClientAbstracts
*/
public function withProxyPort(int $proxy): static
{
$this->proxyPort = $proxy;
return $this;
}
/**
* @param int $retryTimeout
* @return $this
*/
public function withRetryTimeout(int $retryTimeout): static
{
$this->retryTimeout = $retryTimeout;
return $this;
}
/**
* @return int
*/
public function getRetryNum(): int
{
return $this->retryNum;
}
/**
* @return int
*/
public function getRetryTimeout(): int
{
return $this->retryTimeout;
}
/**
* @param bool $bool
* @return $this
*/
public function withVerifyPeer(bool $bool): static
{
$this->verifyPeer = $bool;
return $this;
}
@@ -89,7 +158,7 @@ abstract class ClientAbstracts implements IClient
*/
public function getVerifyPeer(): bool
{
return $this->VERIFYPEER;
return $this->verifyPeer;
}
@@ -122,9 +191,9 @@ abstract class ClientAbstracts implements IClient
/**
* @param array $responseHeader
* @param null|array $responseHeader
*/
public function setResponseHeader(array $responseHeader): void
public function setResponseHeader(?array $responseHeader): void
{
$this->_responseHeader = $responseHeader;
}
@@ -158,11 +227,11 @@ abstract class ClientAbstracts implements IClient
/**
* @param $host
* @param $port
* @param string $host
* @param int $port
* @param false $isSSL
*/
public function __construct($host, $port, bool $isSSL = FALSE)
public function __construct(string $host, int $port, bool $isSSL = FALSE)
{
$this->withHost($host)->withPort($port)->withIsSSL($isSSL);
}
@@ -170,9 +239,9 @@ abstract class ClientAbstracts implements IClient
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function post(string $path, array $params = []): void
public function post(string $path, array|string $params = []): void
{
$this->request(self::POST, $path, $params);
}
@@ -180,9 +249,9 @@ abstract class ClientAbstracts implements IClient
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function put(string $path, array $params = []): void
public function put(string $path, array|string $params = []): void
{
$this->request(self::PUT, $path, $params);
}
@@ -201,9 +270,9 @@ abstract class ClientAbstracts implements IClient
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function head(string $path, array $params = []): void
public function head(string $path, array|string $params = []): void
{
$this->request(self::HEAD, $path, $params);
}
@@ -211,36 +280,39 @@ abstract class ClientAbstracts implements IClient
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function get(string $path, array $params = []): void
public function get(string $path, array|string $params = []): void
{
if (is_array($params)) {
$params = http_build_query($params);
}
$this->request(self::GET, $path, $params);
}
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function option(string $path, array $params = []): void
public function option(string $path, array|string $params = []): void
{
$this->request(self::OPTIONS, $path, $params);
}
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function delete(string $path, array $params = []): void
public function delete(string $path, array|string $params = []): void
{
$this->request(self::DELETE, $path, $params);
}
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function options(string $path, array $params = []): void
public function options(string $path, array|string $params = []): void
{
$this->request(self::OPTIONS, $path, $params);
@@ -248,9 +320,9 @@ abstract class ClientAbstracts implements IClient
/**
* @param string $path
* @param array $params
* @param array|string $params
*/
public function upload(string $path, array $params = []): void
public function upload(string $path, array|string $params = []): void
{
$this->request(self::UPLOAD, $path, $params);
}
@@ -267,7 +339,7 @@ abstract class ClientAbstracts implements IClient
/**
* @return int
*/
#[Pure] protected function getHostPort(): int
protected function getHostPort(): int
{
if (!empty($this->getPort())) {
return $this->getPort();
@@ -282,13 +354,10 @@ abstract class ClientAbstracts implements IClient
* @param string $host
* @return ClientAbstracts
*/
private function withHost(string $host): static
protected function withHost(string $host): static
{
$this->host = $host;
if (Context::inCoroutine()) {
$this->host = System::gethostbyname($host);
}
return $this->withAddedHeader('Host', $host);
return $this;
}
/**
@@ -336,11 +405,11 @@ abstract class ClientAbstracts implements IClient
}
/**
* @param $key
* @param $value
* @param string $key
* @param string|array $value
* @return ClientAbstracts
*/
public function withAddedHeader($key, $value): static
public function withAddedHeader(string $key, string|array $value): static
{
$this->header[$key] = $value;
return $this;
@@ -486,7 +555,7 @@ abstract class ClientAbstracts implements IClient
/**
* @return int
*/
#[Pure] public function getPort(): int
public function getPort(): int
{
if ($this->isSSL()) {
return 443;
@@ -509,25 +578,19 @@ abstract class ClientAbstracts implements IClient
/**
* @return StreamInterface
* @return string|null
*/
public function getData(): StreamInterface
public function getData(): ?string
{
if (!$this->_data) {
$this->_data = new Stream();
}
return $this->_data;
}
/**
* @param string|StreamInterface $data
* @param string|null $data
* @return ClientAbstracts
*/
public function withBody(string|StreamInterface $data): static
public function withBody(?string $data): static
{
if (is_string($data)) {
$data = new Stream($data);
}
$this->_data = $data;
return $this;
}
@@ -552,10 +615,10 @@ abstract class ClientAbstracts implements IClient
/**
* @param $host
* @param string $host
* @return string|string[]
*/
protected function replaceHost($host): array|string
protected function replaceHost(string $host): array|string
{
if ($this->isHttp($host)) {
return str_replace('http://', '', $host);
@@ -568,101 +631,51 @@ abstract class ClientAbstracts implements IClient
/**
* @param $url
* @param string $url
* @return false|int
*/
protected function checkIsIp($url): bool|int
protected function checkIsIp(string $url): bool|int
{
return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
}
/**
* @param $url
* @param string $url
* @return bool
*/
protected function isHttp($url): bool
protected function isHttp(string $url): bool
{
return str_starts_with($url, 'http://');
}
/**
* @param $url
* @param string $url
* @return bool
*/
protected function isHttps($url): bool
protected function isHttps(string $url): bool
{
return str_starts_with($url, 'https://');
}
/**
* @param $newData
* @return string
* @param array|string $newData
* @return string|null
*/
protected function mergeParams($newData): string
protected function mergeParams(array|string $newData): ?string
{
if (empty($data)) return '';
if (!is_string($newData)) {
return $this->toRequest($newData);
if (is_array($newData)) {
return json_encode($newData, JSON_UNESCAPED_UNICODE);
}
return $newData;
}
/**
* @param $data
* @return string
*/
protected function toRequest($data): string
{
if (is_string($data)) {
return $data;
}
$contentType = 'application/x-www-form-urlencoded';
if (isset($this->header['Content-Type'])) {
$contentType = $this->header['Content-Type'];
} else if (isset($this->header['content-type'])) {
$contentType = $this->header['content-type'];
}
if (str_contains($contentType, 'json')) {
return Help::toJson($data);
} else if (str_contains($contentType, 'xml')) {
return Help::toXml($data);
} else {
return http_build_query($data);
}
}
/**
* @param $data
* @param $body
* @return array|string|null
*/
protected function resolve($data, $body): array|string|null
{
if (is_array($body)) {
return $body;
}
$type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html';
if (str_contains($type, 'text/html')) {
return $body;
} else if (str_contains($type, 'json')) {
return json_decode($body, TRUE);
} else if (str_contains($type, 'xml')) {
return Help::xmlToArray($body);
} else if (str_contains($type, 'plain')) {
return Help::toArray($body);
}
return $body;
}
/**
* @return bool
* check isPost Request
*/
#[Pure] protected function isPost(): bool
protected function isPost(): bool
{
return strtolower($this->method) === self::POST;
}
@@ -671,7 +684,7 @@ abstract class ClientAbstracts implements IClient
* @return bool
* check isPost Request
*/
#[Pure] protected function isUpload(): bool
protected function isUpload(): bool
{
return strtolower($this->method) === self::UPLOAD;
}
@@ -682,18 +695,18 @@ abstract class ClientAbstracts implements IClient
*
* check isGet Request
*/
#[Pure] protected function isGet(): bool
protected function isGet(): bool
{
return strtolower($this->method) === self::GET;
}
/**
* @param $arr
* @param array|string $arr
*
* @return array|string
* 将请求参数进行编码
*/
#[Pure] protected function paramEncode($arr): array|string
protected function paramEncode(array|string $arr): array|string
{
if (!is_array($arr)) {
return $arr;
@@ -715,51 +728,16 @@ abstract class ClientAbstracts implements IClient
*/
protected function matchHost(string $string): array
{
if (($parse = isUrl($string, TRUE)) === FALSE) {
return $this->defaultString($string);
}
[$isHttps, $domain, $port, $path] = $parse;
if (str_contains($domain, ':' . $port)) {
$domain = str_replace(':' . $port, '', $domain);
}
$this->port = $isHttps ? 443 : $this->port;
if (isIp($domain)) {
$this->host = $domain;
} else if (Context::inCoroutine()) {
$this->host = System::gethostbyname($domain) ?? $domain;
} else {
$this->host = $domain;
}
$this->header['Host'] = $domain;
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
return [$this->host, $isHttps, $path];
return [$this->host, $this->isSSL(), $string];
}
/**
* @param $string
* @return array
*/
private function defaultString($string): array
{
$host = $this->getHost();
if ($string == '/') {
$string = '/';
} else if (!str_starts_with($string, '/')) {
$string = '/' . $string;
}
return [$host, $this->isSSL(), $string];
}
/**
* @param $path
* @param $params
* @param string $path
* @param array|string $params
* @return string
*/
protected function joinGetParams($path, $params): string
protected function joinGetParams(string $path, array|string $params): string
{
if (empty($params)) {
return $path;
+121 -90
View File
@@ -10,9 +10,6 @@ declare(strict_types=1);
namespace Kiri;
use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri;
use Swoole\Coroutine\Http\Client as SwowClient;
/**
@@ -24,106 +21,140 @@ class CoroutineClient 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 string $method
* @param $path
* @param array|string $params
* @return void
* @throws
*/
public function request(string $method, $path, array|string $params = []): void
{
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
$host = $this->getHost();
if (!preg_match('/(\d{1,3}\.){3}\d{1,3}/', $host)) {
$this->withAddedHeader('Host', $host);
}
$this->withMethod($method)
->coroutine(
$path,
$this->paramEncode($params)
);
}
/**
* @param $path
* @param string $path
* @return $this
*/
public function withCAInfo($path): static
public function withCAInfo(string $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);
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
/**
* @param string $url
* @param array|string $data
*/
private function coroutine(string $url, array|string $data = []): void
{
try {
$this->generate_client($this->getHost(), $this->isSSL());
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
$this->execute($url, $data);
} catch (\Throwable $exception) {
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
/**
* @param $data
* @param $host
* @param $isHttps
* @param $path
*/
private function generate_client($data, $host, $isHttps, $path): void
{
if ($isHttps || $this->isSSL()) {
$this->client = new SwowClient($host, 443, true);
} else {
$this->client = new SwowClient($host, $this->getPort(), false);
}
$this->client->set($this->settings());
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
$this->client->execute($this->setParams($path, $data));
}
/**
* @param string $path
* @param array|string $data
* @return void
*/
private function execute(string $path, array|string $data): void
{
$this->client->execute($this->setParams($path, $data));
if (in_array($this->client->getStatusCode(), [502, 404])) {
$this->retry($path, $data);
} else {
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
}
}
/**
* @param $path
* @param $data
* @return string
*/
private function setParams($path, $data): string
{
$content = $this->getData()->getContents();
if (!empty($content)) {
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else {
$data = $this->mergeParams($data);
if (!empty($data)) {
$this->client->setData($data);
}
}
return $path;
}
/**
* @param string $path
* @param array|string $data
* @return void
*/
private function retry(string $path, array|string $data): void
{
if (($this->num += 1) <= $this->retryNum) {
sleep($this->retryTimeout);
/**
*
*/
public function close(): void
{
$this->client->close();
}
$this->execute($path, $data);
} else {
$this->setStatusCode($this->client->statusCode);
$this->setBody($this->client->errMsg);
}
}
/**
* @param string $host
* @param bool $isHttps
*/
private function generate_client(string $host, bool $isHttps): void
{
if ($isHttps || $this->isSSL()) {
$this->client = new SwowClient($host, 443, true);
} else {
$this->client = new SwowClient($host, $this->getPort(), false);
}
$this->client->set($this->settings());
if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent());
}
$this->client->setHeaders($this->getHeader());
$this->client->setMethod(strtoupper($this->getMethod()));
}
/**
* @param string $path
* @param mixed $data
* @return string
*/
private function setParams(string $path, mixed $data): string
{
$content = $this->getData();
if (!empty($content)) {
$this->client->setData($content);
}
if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data;
} else if (!empty($data)) {
$this->client->setData($data);
}
return $path;
}
/**
*
*/
public function close(): void
{
$this->client->close();
}
}
+72 -56
View File
@@ -4,13 +4,6 @@ declare(strict_types=1);
namespace Kiri;
use Exception;
use Kiri\Message\Response;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/**
* Class CurlClient
* @package Http\Handler\Client
@@ -18,14 +11,18 @@ use Psr\Http\Message\ResponseInterface;
class CurlClient extends ClientAbstracts
{
/**
* @param $method
* @param $path
* @param array $params
* @throws Exception
* @param string $method
* @param string $path
* @param array|string $params
* @return void
*/
public function request($method, $path, array $params = []): void
public function request(string $method, string $path, array|string $params = []): void
{
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
if ($method == self::GET) {
$path = $this->joinGetParams($path, $params);
}
@@ -37,40 +34,41 @@ class CurlClient extends ClientAbstracts
/**
* @param $path
* @param $method
* @param string $path
* @param string $method
* @param $params
* @throws Exception
* @return void
*/
private function getCurlHandler($path, $method, $params): void
private function getCurlHandler(string $path, string $method, $params): void
{
[$host, $isHttps, $path] = $this->matchHost($path);
$host = $isHttps ? 'https://' . $host : 'http://' . $host;
$host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost();
if ($this->getPort() != 443 && $this->getPort() != 80) {
$host .= ':' . $this->getPort();
}
$this->do(curl_init($host . $path), $host . $path, $method);
if ($isHttps !== FALSE) {
if ($this->isSSL()) {
$this->curlHandlerSslSet();
}
$contents = $this->getData()->getContents();
$contents = $this->getData();
if (empty($params) && empty($contents)) {
return;
}
if (!empty($contents)) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
} else if ($method === self::POST) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::UPLOAD) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} else if ($method === self::POST) {
if (is_array($params)) {
$params = http_build_query($params);
}
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
}
}
/**
* @return void
* @throws Exception
* @throws
*/
private function curlHandlerSslSet(): void
{
@@ -80,16 +78,19 @@ class CurlClient extends ClientAbstracts
if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
}
if (!empty($this->getCa()) && file_exists($this->getCa())) {
curl_setopt($this->client, CURLOPT_CAINFO, $this->getCa());
}
}
/**
* @param $resource
* @param $path
* @param $method
* @throws Exception
* @param mixed $resource
* @param string $path
* @param string $method
* @return void
*/
private function do($resource, $path, $method): void
private function do(mixed $resource, string $path, string $method): void
{
curl_setopt($resource, CURLOPT_URL, $path);
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
@@ -112,22 +113,30 @@ class CurlClient extends ClientAbstracts
if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_POST, 1);
}
[$proxy, $port] = [$this->getProxyHost(), $this->getProxyPort()];
if (!empty($proxy) && $port > 0) {
curl_setopt($resource, CURLOPT_PROXYPORT, $port);
curl_setopt($resource, CURLOPT_PROXY, $proxy);
}
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$this->client = $resource;
if (!empty($this->caPath)) {
curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath);
curl_setopt($this->client, CURLOPT_CAINFO, $this->caPath);
}
}
/**
* @var string
*/
private string $caPath = '';
/**
* @param $path
* @param string $path
* @return $this
*/
public function withCAInfo($path): static
public function withCAInfo(string $path): static
{
$this->caPath = $path;
return $this;
@@ -135,16 +144,33 @@ class CurlClient extends ClientAbstracts
/**
* @throws Exception
* @throws
*/
private function execute(): void
{
$output = curl_exec($this->client);
if ($output === FALSE) {
if ($output !== FALSE) {
$this->explode($output);
} else {
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
}
}
/**
* @return void
* @throws
*/
private function retry(): void
{
if (($this->num += 1) <= $this->retryNum) {
sleep($this->retryTimeout);
$this->execute();
} else {
$this->explode($output);
$this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client));
}
}
@@ -159,11 +185,11 @@ class CurlClient extends ClientAbstracts
/**
* @param $output
* @param string $output
* @return void
* @throws Exception
* @throws
*/
private function explode($output): void
private function explode(string $output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
@@ -173,31 +199,21 @@ class CurlClient extends ClientAbstracts
$header = explode("\r\n", $header);
$status = explode(' ', array_shift($header));
$this->setStatusCode(intval($status[1]));
$this->setBody($body);
$this->setResponseHeader($header);
}
/**
* @param $headers
* @return array
*/
private function headerFormat($headers): array
{
$_tmp = [];
foreach ($headers as $val) {
$trim = explode(': ', trim($val));
$_tmp[strtolower($trim[0])] = [$trim[1] ?? ''];
$statusCode = intval($status[1]);
if (in_array($statusCode, [502, 404])) {
$this->retry();
} else {
$this->setStatusCode($statusCode);
$this->setBody($body);
$this->setResponseHeader($header);
}
return $_tmp;
}
/**
* @return array
*/
#[Pure] private function parseHeaderMat(): array
private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->getHeader() as $key => $val) {
+88 -89
View File
@@ -11,99 +11,98 @@ use Exception;
*/
class HttpParse
{
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $key => $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
$value = key($value);
}
if ($first === '') {
$first = $value;
} else {
$tp[] = $value;
}
}
$key = $first . '[' . implode('][', $tp) . ']';
if (count($tp) < 1) {
$key = $first;
}
return $key;
}
/**
* @param mixed ...$object
* @return string
*/
private static function getKey(...$object): string
{
$first = '';
$tp = [];
foreach ($object as $value) {
if ($value === null) {
continue;
}
if (is_array($value)) {
$value = key($value);
}
if ($first === '') {
$first = $value;
} else {
$tp[] = $value;
}
}
$key = $first . '[' . implode('][', $tp) . ']';
if (count($tp) < 1) {
$key = $first;
}
return $key;
}
/**
* @param $data
* @return string
* @throws Exception
*/
public static function parse($data): string
{
$tmp = [];
if (is_string($data)) {
return $data;
}
foreach ($data as $key => $datum) {
if ($datum === null) {
continue;
}
$tmp[] = static::ifElse($key, $datum);
}
return implode('&', $tmp);
}
/**
* @param array|string $data
* @return string
*/
public static function parse(array|string $data): string
{
$tmp = [];
if (is_string($data)) {
return $data;
}
foreach ($data as $key => $datum) {
if ($datum === null) {
continue;
}
$tmp[] = static::ifElse($key, $datum);
}
return implode('&', $tmp);
}
/**
* @param $t
* @param $qt
* @return string
* @throws Exception
*/
private static function ifElse($t, $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;
}
if (is_string($qt)) {
$string = $t . '=' . urlencode($qt);
} else {
$string = static::encode($t, $qt);
}
return $string;
}
/**
* @param $t
* @param $qt
* @return string
* @throws
*/
private static function ifElse(string $t, mixed $qt): string
{
if (is_numeric($qt)) {
return $t . '=' . $qt;
}
if (is_string($qt)) {
$string = $t . '=' . urlencode($qt);
} else {
$string = static::encode($t, $qt);
}
return $string;
}
/**
* @param mixed ...$object
* @return string
* @throws Exception
*/
private static function encode(...$object): string
{
$ret = [];
/**
* @param mixed ...$object
* @return string
* @throws
*/
private static function encode(...$object): string
{
$ret = [];
$data = $object[count($object) - 1];
$key = static::getKey(...$object);
foreach ($data as $s => $datum) {
if (is_array($datum)) {
$object[count($object) - 1] = $s;
$object[] = $datum;
$string = static::encode(...$object);
} else {
if (is_object($datum)) {
throw new Exception('Http body con\'t object.');
}
$string = $key . '=' . urlencode($datum);
}
$ret[] = $string;
}
return implode('&', $ret);
}
$data = $object[count($object) - 1];
$key = static::getKey(...$object);
foreach ($data as $s => $datum) {
if (is_array($datum)) {
$object[count($object) - 1] = $s;
$object[] = $datum;
$string = static::encode(...$object);
} else {
if (is_object($datum)) {
throw new Exception('Http body con\'t object.');
}
$string = $key . '=' . urlencode($datum);
}
$ret[] = $string;
}
return implode('&', $ret);
}
}
+39 -41
View File
@@ -5,25 +5,23 @@ namespace Kiri;
use Closure;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
interface IClient
{
/**
* @param string $path
* @param array $params
*/
public function get(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function get(string $path, array|string $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function post(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function post(string $path, array|string $params = []): void;
/**
@@ -32,47 +30,47 @@ interface IClient
public function close(): void;
/**
* @param string $path
* @param array $params
*/
public function delete(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function delete(string $path, array|string $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function options(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function options(string $path, array|string $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function upload(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function upload(string $path, array|string $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function put(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function put(string $path, array|string $params = []): void;
/**
* @param string $path
* @param array $params
*/
public function head(string $path, array $params = []): void;
/**
* @param string $path
* @param array|string $params
*/
public function head(string $path, array|string $params = []): void;
/**
* @param string $method
* @param string $path
* @param array $params
* @param array|string $params
*/
public function request(string $method, string $path, array $params = []): void;
public function request(string $method, string $path, array|string $params = []): void;
/**
@@ -154,10 +152,10 @@ interface IClient
/**
* @param string|StreamInterface $data
* @param string $data
* @return static
*/
public function withBody(string|StreamInterface $data): static;
public function withBody(string $data): static;
/**
+7 -3
View File
@@ -2,8 +2,6 @@
namespace Kiri;
use JetBrains\PhpStorm\Pure;
trait TSwooleClient
{
@@ -11,7 +9,7 @@ trait TSwooleClient
/**
* @return array
*/
#[Pure] private function settings(): array
private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
@@ -21,6 +19,12 @@ trait TSwooleClient
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout();
}
[$proxy, $port] = [$this->getProxyHost(), $this->getProxyPort()];
if (!empty($proxy) && $port > 0) {
$params['http_proxy_host'] = $proxy;
$params['http_proxy_port'] = $port;
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
+87
View File
@@ -0,0 +1,87 @@
<?php
namespace Kiri;
use Exception;
use Kiri\Di\Context;
use Swoole\Coroutine\Client as CoroutineClient;
use Swoole\Client as AsyncClient;
class TcpClient
{
/**
* @var AsyncClient|CoroutineClient
*/
protected AsyncClient|CoroutineClient $client;
/**
* @param string $host
* @param int $port
* @param int $socket
* @throws
*/
public function __construct(readonly public string $host, readonly public int $port, readonly public int $socket = SWOOLE_SOCK_TCP)
{
$this->reconnect();
}
/**
* @return void
* @throws
*/
public function reconnect(): void
{
$this->client?->close();
if (Context::inCoroutine()) {
$this->client = new CoroutineClient($this->socket);
} else {
$this->client = new AsyncClient($this->socket);
}
if (!$this->client->connect($this->host, $this->port, 1)) {
throw new Exception('Connect ' . $this->host . '::' . $this->port . ' fail');
}
}
/**
* @param string $data
* @return int|bool
*/
public function send(string $data): int|bool
{
return $this->client->send($data);
}
/**
* @return bool|string
*/
public function read(): bool|string
{
return $this->client->recv();
}
/**
* @return bool
*/
public function isConnected(): bool
{
return $this->client->isConnected();
}
/**
* @return bool
*/
public function close(): bool
{
return $this->client->close();
}
}
+1 -3
View File
@@ -11,9 +11,7 @@
"require": {
"php": ">=8.0",
"ext-json": "*",
"ext-swoole": "*",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
"ext-swoole": "*"
},
"autoload": {
"psr-4": {