This commit is contained in:
2023-12-18 18:21:09 +08:00
parent 6da3575619
commit 196db497c1
4 changed files with 71 additions and 89 deletions
+31 -31
View File
@@ -17,13 +17,13 @@ defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(
abstract class ClientAbstracts implements IClient abstract class ClientAbstracts implements IClient
{ {
const POST = 'post'; const string POST = 'post';
const UPLOAD = 'upload'; const string UPLOAD = 'upload';
const GET = 'get'; const string GET = 'get';
const DELETE = 'delete'; const string DELETE = 'delete';
const OPTIONS = 'options'; const string OPTIONS = 'options';
const HEAD = 'head'; const string HEAD = 'head';
const PUT = 'put'; const string PUT = 'put';
private string $host = ''; private string $host = '';
private array $header = []; private array $header = [];
@@ -143,10 +143,10 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param $bool * @param bool $bool
* @return $this * @return $this
*/ */
public function withVerifyPeer($bool): static public function withVerifyPeer(bool $bool): static
{ {
$this->verifyPeer = $bool; $this->verifyPeer = $bool;
return $this; return $this;
@@ -227,11 +227,11 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param $host * @param string $host
* @param $port * @param int $port
* @param false $isSSL * @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); $this->withHost($host)->withPort($port)->withIsSSL($isSSL);
} }
@@ -405,11 +405,11 @@ abstract class ClientAbstracts implements IClient
} }
/** /**
* @param $key * @param string $key
* @param $value * @param string|array $value
* @return ClientAbstracts * @return ClientAbstracts
*/ */
public function withAddedHeader($key, $value): static public function withAddedHeader(string $key, string|array $value): static
{ {
$this->header[$key] = $value; $this->header[$key] = $value;
return $this; return $this;
@@ -615,10 +615,10 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param $host * @param string $host
* @return string|string[] * @return string|string[]
*/ */
protected function replaceHost($host): array|string protected function replaceHost(string $host): array|string
{ {
if ($this->isHttp($host)) { if ($this->isHttp($host)) {
return str_replace('http://', '', $host); return str_replace('http://', '', $host);
@@ -631,43 +631,43 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param $url * @param string $url
* @return false|int * @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); return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url);
} }
/** /**
* @param $url * @param string $url
* @return bool * @return bool
*/ */
protected function isHttp($url): bool protected function isHttp(string $url): bool
{ {
return str_starts_with($url, 'http://'); return str_starts_with($url, 'http://');
} }
/** /**
* @param $url * @param string $url
* @return bool * @return bool
*/ */
protected function isHttps($url): bool protected function isHttps(string $url): bool
{ {
return str_starts_with($url, 'https://'); return str_starts_with($url, 'https://');
} }
/** /**
* @param $newData * @param array|string $newData
* @return string|null * @return string|null
*/ */
protected function mergeParams($newData): ?string protected function mergeParams(array|string $newData): ?string
{ {
if (is_array($newData)) { if (is_array($newData)) {
return json_encode($newData, JSON_UNESCAPED_UNICODE); return json_encode($newData, JSON_UNESCAPED_UNICODE);
} }
return (string)$newData; return $newData;
} }
@@ -701,12 +701,12 @@ abstract class ClientAbstracts implements IClient
} }
/** /**
* @param $arr * @param array|string $arr
* *
* @return array|string * @return array|string
* 将请求参数进行编码 * 将请求参数进行编码
*/ */
protected function paramEncode($arr): array|string protected function paramEncode(array|string $arr): array|string
{ {
if (!is_array($arr)) { if (!is_array($arr)) {
return $arr; return $arr;
@@ -733,11 +733,11 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param $path * @param string $path
* @param $params * @param array|string $params
* @return string * @return string
*/ */
protected function joinGetParams($path, $params): string protected function joinGetParams(string $path, array|string $params): string
{ {
if (empty($params)) { if (empty($params)) {
return $path; return $path;
+16 -19
View File
@@ -47,20 +47,19 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $path * @param string $path
* @return $this * @return $this
*/ */
public function withCAInfo($path): static public function withCAInfo(string $path): static
{ {
return $this; return $this;
} }
/** /**
* @param $url * @param string $url
* @param array|string $data * @param array|string $data
* @throws
*/ */
private function coroutine($url, array|string $data = []): void private function coroutine(string $url, array|string $data = []): void
{ {
try { try {
$this->generate_client($this->getHost(), $this->isSSL()); $this->generate_client($this->getHost(), $this->isSSL());
@@ -78,12 +77,11 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $path * @param string $path
* @param $data * @param array|string $data
* @return void * @return void
* @throws
*/ */
private function execute($path, $data): void private function execute(string $path, array|string $data): void
{ {
$this->client->execute($this->setParams($path, $data)); $this->client->execute($this->setParams($path, $data));
if (in_array($this->client->getStatusCode(), [502, 404])) { if (in_array($this->client->getStatusCode(), [502, 404])) {
@@ -97,12 +95,11 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $path * @param string $path
* @param $data * @param array|string $data
* @return void * @return void
* @throws
*/ */
private function retry($path, $data): void private function retry(string $path, array|string $data): void
{ {
if (($this->num += 1) <= $this->retryNum) { if (($this->num += 1) <= $this->retryNum) {
sleep($this->retryTimeout); sleep($this->retryTimeout);
@@ -115,10 +112,10 @@ class CoroutineClient extends ClientAbstracts
} }
/** /**
* @param $host * @param string $host
* @param $isHttps * @param bool $isHttps
*/ */
private function generate_client($host, $isHttps): void private function generate_client(string $host, bool $isHttps): void
{ {
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$this->client = new SwowClient($host, 443, true); $this->client = new SwowClient($host, 443, true);
@@ -135,11 +132,11 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $path * @param string $path
* @param $data * @param mixed $data
* @return string * @return string
*/ */
private function setParams($path, $data): string private function setParams(string $path, mixed $data): string
{ {
$content = $this->getData(); $content = $this->getData();
if (!empty($content)) { if (!empty($content)) {
+21 -35
View File
@@ -4,9 +4,6 @@ declare(strict_types=1);
namespace Kiri; namespace Kiri;
use Exception;
/** /**
* Class CurlClient * Class CurlClient
* @package Http\Handler\Client * @package Http\Handler\Client
@@ -14,13 +11,14 @@ use Exception;
class CurlClient extends ClientAbstracts class CurlClient extends ClientAbstracts
{ {
/** /**
* @param $method * @param string $method
* @param $path * @param string $path
* @param array|string $params * @param array|string $params
* @throws * @return void
*/ */
public function request($method, $path, array|string $params = []): void public function request(string $method, string $path, array|string $params = []): void
{ {
if (!str_starts_with($path, '/')) { if (!str_starts_with($path, '/')) {
$path = '/' . $path; $path = '/' . $path;
@@ -36,12 +34,12 @@ class CurlClient extends ClientAbstracts
/** /**
* @param $path * @param string $path
* @param $method * @param string $method
* @param $params * @param $params
* @throws * @return void
*/ */
private function getCurlHandler($path, $method, $params): void private function getCurlHandler(string $path, string $method, $params): void
{ {
$host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost(); $host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost();
if ($this->getPort() != 443 && $this->getPort() != 80) { if ($this->getPort() != 443 && $this->getPort() != 80) {
@@ -87,12 +85,12 @@ class CurlClient extends ClientAbstracts
/** /**
* @param $resource * @param mixed $resource
* @param $path * @param string $path
* @param $method * @param string $method
* @throws * @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_URL, $path);
curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_TIMEOUT, $this->getTimeout()); // 超时设置
@@ -128,14 +126,17 @@ class CurlClient extends ClientAbstracts
} }
/**
* @var string
*/
private string $caPath = ''; private string $caPath = '';
/** /**
* @param $path * @param string $path
* @return $this * @return $this
*/ */
public function withCAInfo($path): static public function withCAInfo(string $path): static
{ {
$this->caPath = $path; $this->caPath = $path;
return $this; return $this;
@@ -184,11 +185,11 @@ class CurlClient extends ClientAbstracts
/** /**
* @param $output * @param string $output
* @return void * @return void
* @throws * @throws
*/ */
private function explode($output): void private function explode(string $output): void
{ {
[$header, $body] = explode("\r\n\r\n", $output, 2); [$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') { if ($header == 'HTTP/1.1 100 Continue') {
@@ -208,21 +209,6 @@ class CurlClient extends ClientAbstracts
} }
} }
/**
* @param $headers
* @return array
*/
private function headerFormat($headers): array
{
$_tmp = [];
foreach ($headers as $val) {
$trim = explode(': ', trim($val));
$_tmp[strtolower($trim[0])] = [$trim[1] ?? ''];
}
return $_tmp;
}
/** /**
* @return array * @return array
+3 -4
View File
@@ -40,11 +40,10 @@ class HttpParse
} }
/** /**
* @param $data * @param array|string $data
* @return string * @return string
* @throws
*/ */
public static function parse($data): string public static function parse(array|string $data): string
{ {
$tmp = []; $tmp = [];
if (is_string($data)) { if (is_string($data)) {
@@ -65,7 +64,7 @@ class HttpParse
* @return string * @return string
* @throws * @throws
*/ */
private static function ifElse($t, $qt): string private static function ifElse(string $t, mixed $qt): string
{ {
if (is_numeric($qt)) { if (is_numeric($qt)) {
return $t . '=' . $qt; return $t . '=' . $qt;