modify plugin name

This commit is contained in:
2022-02-21 15:17:42 +08:00
parent 48808ded98
commit 0c7b8eb57f
3 changed files with 868 additions and 900 deletions
+691 -722
View File
File diff suppressed because it is too large Load Diff
+13 -12
View File
@@ -10,9 +10,8 @@ declare(strict_types=1);
namespace Kiri; namespace Kiri;
use Exception; use Exception;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri; use Kiri;
use Kiri\Abstracts\Logger;
use Swoole\Coroutine\Http\Client as SwowClient; use Swoole\Coroutine\Http\Client as SwowClient;
/** /**
@@ -22,7 +21,7 @@ use Swoole\Coroutine\Http\Client as SwowClient;
class CoroutineClient extends ClientAbstracts class CoroutineClient extends ClientAbstracts
{ {
use TSwooleClient; use TSwooleClient;
/** /**
* @param string $method * @param string $method
@@ -33,6 +32,9 @@ class CoroutineClient extends ClientAbstracts
*/ */
public function request(string $method, $path, array $params = []): void public function request(string $method, $path, array $params = []): void
{ {
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}
$this->withMethod($method) $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $this->matchHost($path),
@@ -41,15 +43,14 @@ class CoroutineClient extends ClientAbstracts
} }
/**
/** * @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
+164 -166
View File
@@ -5,10 +5,7 @@ namespace Kiri;
use Exception; use Exception;
use Kiri\Message\Response;
use Kiri\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/** /**
@@ -18,191 +15,192 @@ use Psr\Http\Message\ResponseInterface;
class CurlClient extends ClientAbstracts class CurlClient extends ClientAbstracts
{ {
/** /**
* @param $method * @param $method
* @param $path * @param $path
* @param array $params * @param array $params
* @throws Exception * @throws Exception
*/ */
public function request($method, $path, array $params = []): void public function request($method, $path, array $params = []): void
{ {
if ($method == self::GET) { if (!str_starts_with($path, '/')) {
$path = $this->joinGetParams($path, $params); $path = '/' . $path;
} }
if ($method == self::GET) {
$path = $this->joinGetParams($path, $params);
}
$this->getCurlHandler($path, $method, $params); $this->getCurlHandler($path, $method, $params);
$this->execute(); $this->execute();
} }
/** /**
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
* @throws Exception * @throws Exception
*/ */
private function getCurlHandler($path, $method, $params): void private function getCurlHandler($path, $method, $params): void
{ {
[$host, $isHttps, $path] = $this->matchHost($path); $host = $this->isSSL() ? 'https://' . $this->getHost() : 'http://' . $this->getHost();
if ($this->getPort() != 443 && $this->getPort() != 80) {
$host = $isHttps ? 'https://' . $host : 'http://' . $host; $host .= ':' . $this->getPort();
if ($this->getPort() != 443 && $this->getPort() != 80) { }
$host .= ':' . $this->getPort(); $this->do(curl_init($host . $path), $host . $path, $method);
} if ($this->isSSL()) {
$this->do(curl_init($host . $path), $host . $path, $method); $this->curlHandlerSslSet();
if ($isHttps !== FALSE) { }
$this->curlHandlerSslSet(); $contents = $this->getData()->getContents();
} if (empty($params) && empty($contents)) {
$contents = $this->getData()->getContents(); return;
if (empty($params) && empty($contents)) { }
return; if (!empty($contents)) {
} curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
if (!empty($contents)) { } else if ($method === self::POST) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents); curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::POST) { } else if ($method === self::UPLOAD) {
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params)); curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} else if ($method === self::UPLOAD) { }
curl_setopt($this->client, CURLOPT_POSTFIELDS, $params); }
}
}
/** /**
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function curlHandlerSslSet(): void private function curlHandlerSslSet(): void
{ {
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());
} }
if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) { if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile()); curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
} }
} }
/** /**
* @param $resource * @param $resource
* @param $path * @param $path
* @param $method * @param $method
* @throws Exception * @throws Exception
*/ */
private function do($resource, $path, $method): void private function do($resource, $path, $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()); // 超时设置
curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置 curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, $this->getConnectTimeout()); // 超时设置
curl_setopt($resource, CURLOPT_HEADER, TRUE); curl_setopt($resource, CURLOPT_HEADER, TRUE);
curl_setopt($resource, CURLOPT_FAILONERROR, TRUE); curl_setopt($resource, CURLOPT_FAILONERROR, TRUE);
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat());
if (defined('CURLOPT_SSL_FALSESTART')) { if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE); curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
} }
curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE); curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE);
curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE); curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE);
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent());
} }
curl_setopt($resource, CURLOPT_NOBODY, FALSE); curl_setopt($resource, CURLOPT_NOBODY, FALSE);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
if ($method === self::POST || $method == self::UPLOAD) { if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_POST, 1); curl_setopt($resource, CURLOPT_POST, 1);
} }
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
$this->client = $resource; $this->client = $resource;
if (!empty($this->caPath)) { if (!empty($this->caPath)) {
curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath); curl_setopt($this->client, CURLOPT_CAINFO, $this->caPath);
} }
} }
private string $caPath = ''; private string $caPath = '';
/** /**
* @param $path * @param $path
* @return $this * @return $this
*/ */
public function withCAInfo($path): static public function withCAInfo($path): static
{ {
$this->caPath = $path; $this->caPath = $path;
return $this; return $this;
} }
/** /**
* @throws Exception * @throws Exception
*/ */
private function execute(): void private function execute(): void
{ {
$output = curl_exec($this->client); $output = curl_exec($this->client);
if ($output === FALSE) { if ($output === FALSE) {
$this->setStatusCode(curl_errno($this->client)); $this->setStatusCode(curl_errno($this->client));
$this->setBody(curl_error($this->client)); $this->setBody(curl_error($this->client));
} else { } else {
$this->explode($output); $this->explode($output);
} }
} }
/** /**
* *
*/ */
public function close(): void public function close(): void
{ {
curl_close($this->client); curl_close($this->client);
} }
/** /**
* @param $output * @param $output
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function explode($output): void private function explode($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') {
[$header, $body] = explode("\r\n\r\n", $body, 2); [$header, $body] = explode("\r\n\r\n", $body, 2);
} }
$header = explode("\r\n", $header); $header = explode("\r\n", $header);
$status = explode(' ', array_shift($header)); $status = explode(' ', array_shift($header));
$this->setStatusCode(intval($status[1])); $this->setStatusCode(intval($status[1]));
$this->setBody($body); $this->setBody($body);
$this->setResponseHeader($header); $this->setResponseHeader($header);
} }
/** /**
* @param $headers * @param $headers
* @return array * @return array
*/ */
private function headerFormat($headers): array private function headerFormat($headers): array
{ {
$_tmp = []; $_tmp = [];
foreach ($headers as $val) { foreach ($headers as $val) {
$trim = explode(': ', trim($val)); $trim = explode(': ', trim($val));
$_tmp[strtolower($trim[0])] = [$trim[1] ?? '']; $_tmp[strtolower($trim[0])] = [$trim[1] ?? ''];
} }
return $_tmp; return $_tmp;
} }
/** /**
* @return array * @return array
*/ */
#[Pure] private function parseHeaderMat(): array #[Pure] private function parseHeaderMat(): array
{ {
$headers = []; $headers = [];
foreach ($this->getHeader() as $key => $val) { foreach ($this->getHeader() as $key => $val) {
$headers[$key] = $key . ': ' . $val; $headers[$key] = $key . ': ' . $val;
} }
return array_values($headers); return array_values($headers);
} }
} }