Compare commits

...

46 Commits

Author SHA1 Message Date
as2252258 f230b03ce0 1 2021-12-11 17:52:19 +08:00
as2252258 beb7c57809 1 2021-12-11 17:49:51 +08:00
as2252258 82f046f38b 1 2021-12-11 17:31:21 +08:00
as2252258 080dbdcf0d 1 2021-12-11 17:30:02 +08:00
as2252258 04a1bc2fcb 1 2021-12-11 17:29:52 +08:00
as2252258 61606b2b13 1 2021-12-11 17:28:42 +08:00
as2252258 360c88b8e7 1 2021-12-11 17:27:18 +08:00
as2252258 6f0c564e97 1 2021-12-11 17:25:27 +08:00
as2252258 6b7284085d 1 2021-12-11 17:21:13 +08:00
as2252258 eb2659e777 1 2021-12-11 17:20:19 +08:00
as2252258 2f7b17e876 1 2021-12-11 17:20:10 +08:00
as2252258 8ac0595fab 1 2021-12-11 16:52:46 +08:00
as2252258 3784e25f64 1 2021-12-11 16:52:02 +08:00
as2252258 bd74702a21 1 2021-12-11 16:50:26 +08:00
as2252258 6098045ab3 1 2021-12-11 16:48:36 +08:00
as2252258 4b7ebb2572 1 2021-12-11 16:45:43 +08:00
as2252258 86c76a7587 1 2021-12-11 16:44:33 +08:00
as2252258 895533a04f 1 2021-12-11 16:43:44 +08:00
as2252258 9d24b3314b 1 2021-12-11 16:43:07 +08:00
as2252258 80a2db8974 1 2021-12-11 16:39:46 +08:00
as2252258 6434be4f52 1 2021-12-11 16:20:08 +08:00
as2252258 eec1efcb61 1 2021-12-11 16:19:09 +08:00
as2252258 3491ff8df2 1 2021-12-11 16:18:35 +08:00
as2252258 a21f1090d0 1 2021-12-11 16:17:33 +08:00
as2252258 65958a0ded 1 2021-12-11 16:14:27 +08:00
as2252258 b945c19200 1 2021-12-11 16:13:10 +08:00
as2252258 2165797410 1 2021-12-11 16:02:56 +08:00
as2252258 d604083162 1 2021-12-11 06:36:47 +08:00
as2252258 b59463c5e9 1 2021-12-11 06:31:16 +08:00
as2252258 8202c1861f 1 2021-12-11 06:27:18 +08:00
as2252258 d604d5f46a 1 2021-12-11 05:56:34 +08:00
as2252258 bf7d401158 1 2021-12-11 05:56:06 +08:00
as2252258 5e271a6e0a 1 2021-12-11 05:55:29 +08:00
as2252258 a3c89b419b 1 2021-12-11 05:54:52 +08:00
as2252258 37e96f2853 1 2021-12-11 05:53:12 +08:00
as2252258 64f0b9f9d0 1 2021-12-11 05:43:12 +08:00
as2252258 3693fd40f6 1 2021-12-11 05:41:45 +08:00
as2252258 b0fe5ab4c4 1 2021-12-11 05:40:26 +08:00
as2252258 791be944a3 1 2021-12-11 05:39:15 +08:00
as2252258 43eab7ae27 1 2021-12-11 05:24:21 +08:00
as2252258 0a54c021b9 改名 2021-11-29 11:51:19 +08:00
as2252258 af9b88f114 改名 2021-11-29 11:50:28 +08:00
as2252258 336237d338 改名 2021-11-29 11:28:01 +08:00
as2252258 d075dd73a4 改名 2021-11-29 11:22:49 +08:00
as2252258 40c946b58e 改名 2021-11-29 10:33:18 +08:00
as2252258 1d4554dbe7 改名 2021-11-18 15:54:44 +08:00
7 changed files with 1139 additions and 877 deletions
+1
View File
@@ -12,6 +12,7 @@
"php": ">=8.0", "php": ">=8.0",
"ext-json": "*", "ext-json": "*",
"ext-redis": "*", "ext-redis": "*",
"ext-swoole": "*",
"psr/http-client": "^1.0", "psr/http-client": "^1.0",
"psr/http-message": "^1.0" "psr/http-message": "^1.0"
}, },
+161
View File
@@ -0,0 +1,161 @@
<?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();
}
}
+2 -2
View File
@@ -12,7 +12,7 @@ class Client
{ {
private CoroutineClient|Curl $abstracts; private CoroutineClient|Curl|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 Curl($host, $port, $isSsl); $this->abstracts = new AsyncClient($host, $port, $isSsl);
} }
} }
+747 -673
View File
File diff suppressed because it is too large Load Diff
+17 -36
View File
@@ -10,8 +10,6 @@ declare(strict_types=1);
namespace Http\Client; namespace Http\Client;
use Exception; use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger; use Kiri\Abstracts\Logger;
use Kiri\Kiri; use Kiri\Kiri;
@@ -24,6 +22,8 @@ use Swoole\Coroutine\Http\Client as SwowClient;
class CoroutineClient extends ClientAbstracts class CoroutineClient extends ClientAbstracts
{ {
use TSwooleClient;
/** /**
* @param string $method * @param string $method
* @param $path * @param $path
@@ -41,6 +41,16 @@ class CoroutineClient extends ClientAbstracts
} }
/**
* @param $path
* @return $this
*/
public function withCAInfo($path): static
{
return $this;
}
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
@@ -53,15 +63,13 @@ class CoroutineClient extends ClientAbstracts
if ($this->client->statusCode < 0) { if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg); throw new Exception($this->client->errMsg);
} }
$body = (new Response())->withStatus($this->client->getStatusCode()) $this->setStatusCode($this->client->getStatusCode());
->withHeaders($this->client->getHeaders()) $this->setBody($this->client->getBody());
->withBody(new Stream($this->client->getBody())); $this->setResponseHeader($this->client->headers);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]); Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
$body = (new Response())->withStatus(-1)->withHeaders([]) $this->setStatusCode(-1);
->withBody(new Stream(jTraceEx($exception))); $this->setBody(jTraceEx($exception));
} finally {
$this->setBody($body);
} }
} }
@@ -111,7 +119,6 @@ class CoroutineClient extends ClientAbstracts
return $path; return $path;
} }
/** /**
* *
*/ */
@@ -119,30 +126,4 @@ class CoroutineClient extends ClientAbstracts
{ {
$this->client->close(); $this->client->close();
} }
/**
* @return array
*/
#[Pure] private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
$params = [];
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout();
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
$params['ssl_host_name'] = $this->getHost();
$params['ssl_cert_file'] = $this->getSslCertFile();
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = true;
$params['ssl_cafile'] = $sslCa;
return $params;
}
} }
+173 -166
View File
@@ -18,187 +18,194 @@ use Psr\Http\Message\ResponseInterface;
class Curl extends ClientAbstracts class Curl 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 ($method == self::GET) {
$path = $this->joinGetParams($path, $params); $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, $isHttps, $path] = $this->matchHost($path);
$host = $isHttps ? 'https://' . $host : 'http://' . $host; $host = $isHttps ? 'https://' . $host : 'http://' . $host;
if ($this->getPort() != 443 && $this->getPort() != 80) { if ($this->getPort() != 443 && $this->getPort() != 80) {
$host .= ':' . $this->getPort(); $host .= ':' . $this->getPort();
} }
$this->do(curl_init($host . $path), $host . $path, $method);
$this->do(curl_init($host . $path), $host . $path, $method); if ($isHttps !== FALSE) {
if ($isHttps !== false) { $this->curlHandlerSslSet();
$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);
} else if ($method === self::POST) {
if (!empty($contents)) { curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents); } else if ($method === self::UPLOAD) {
} else if ($method === self::POST) { curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($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->ssl_key)) { curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, $this->getVerifyPeer());
if (!file_exists($this->ssl_key)) { curl_setopt($this->client, CURLOPT_VERBOSE, TRUE);
throw new Exception('SSL protocol certificate not found.'); // 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()); curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
} }
if (!empty($this->ssl_cert)) { if (!empty($this->getSslCertFile()) && file_exists($this->getSslCertFile())) {
if (!!file_exists($this->ssl_cert)) { curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
throw new Exception('SSL protocol certificate not found.'); }
} }
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());
if (defined('CURLOPT_SSL_FALSESTART')) {
curl_setopt($resource, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); curl_setopt($resource, CURLOPT_SSL_FALSESTART, TRUE);
if (defined('CURLOPT_SSL_FALSESTART')) { }
curl_setopt($resource, CURLOPT_SSL_FALSESTART, true); curl_setopt($resource, CURLOPT_FORBID_REUSE, FALSE);
} curl_setopt($resource, CURLOPT_FRESH_CONNECT, FALSE);
curl_setopt($resource, CURLOPT_FORBID_REUSE, false); if (!empty($this->getAgent())) {
curl_setopt($resource, CURLOPT_FRESH_CONNECT, false); curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent());
}
if (!empty($this->getAgent())) { curl_setopt($resource, CURLOPT_NOBODY, FALSE);
curl_setopt($resource, CURLOPT_USERAGENT, $this->getAgent()); curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容
} curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($resource, CURLOPT_NOBODY, FALSE); if ($method === self::POST || $method == self::UPLOAD) {
curl_setopt($resource, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 }
curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate'); curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
if ($method === self::POST || $method == self::UPLOAD) { $this->client = $resource;
curl_setopt($resource, CURLOPT_POST, 1); if (!empty($this->caPath)) {
} curl_setopt($this->client,CURLOPT_CAINFO, $this->caPath);
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); }
}
$this->client = $resource;
}
/** private string $caPath = '';
* @throws Exception
*/
private function execute(): void
{
$output = curl_exec($this->client);
if ($output === false) {
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client)));
} else {
$response = $this->explode($output);
}
$this->setBody($response);
}
/** /**
* * @param $path
*/ * @return $this
public function close(): void */
{ public function withCAInfo($path): static
curl_close($this->client); {
} $this->caPath = $path;
return $this;
}
/** /**
* @param $output * @throws Exception
* @return ResponseInterface */
* @throws Exception private function execute(): void
*/ {
private function explode($output): ResponseInterface $output = curl_exec($this->client);
{ if ($output === FALSE) {
[$header, $body] = explode("\r\n\r\n", $output, 2); $this->setStatusCode(curl_errno($this->client));
if ($header == 'HTTP/1.1 100 Continue') { $this->setBody(curl_error($this->client));
[$header, $body] = explode("\r\n\r\n", $body, 2); } else {
} $this->explode($output);
}
$header = explode("\r\n", $header); }
$status = explode(' ', array_shift($header));
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
->withBody(new Stream($body));
}
/**
* @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 *
*/ */
#[Pure] private function parseHeaderMat(): array public function close(): void
{ {
$headers = []; curl_close($this->client);
foreach ($this->getHeader() as $key => $val) { }
$headers[$key] = $key . ': ' . $val;
}
return array_values($headers); /**
} * @param $output
* @return void
* @throws Exception
*/
private function explode($output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
[$header, $body] = explode("\r\n\r\n", $body, 2);
}
$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] ?? ''];
}
return $_tmp;
}
/**
* @return array
*/
#[Pure] private function parseHeaderMat(): array
{
$headers = [];
foreach ($this->getHeader() as $key => $val) {
$headers[$key] = $key . ': ' . $val;
}
return array_values($headers);
}
} }
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Http\Client;
use JetBrains\PhpStorm\Pure;
trait TSwooleClient
{
/**
* @return array
*/
#[Pure] private function settings(): array
{
$sslCert = $this->getSslCertFile();
$sslKey = $this->getSslKeyFile();
$sslCa = $this->getCa();
$params = [];
if ($this->getConnectTimeout() > 0) {
$params['timeout'] = $this->getConnectTimeout();
}
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
return $params;
}
$params['ssl_host_name'] = $this->getHost();
$params['ssl_cert_file'] = $this->getSslCertFile();
$params['ssl_key_file'] = $this->getSslKeyFile();
$params['ssl_verify_peer'] = TRUE;
$params['ssl_cafile'] = $sslCa;
return $params;
}
}