Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4d51f6e5d6 | |||
| 21fbbe52e5 | |||
| a0a753d8b4 | |||
| dcc5e67f0a | |||
| b8e7a5f086 | |||
| 88a414f0bb | |||
| cba27222fa | |||
| 593f05754d | |||
| 044c5c4a28 | |||
| 121642e825 | |||
| 5eecf66b43 | |||
| a88787a530 | |||
| aed0e61531 | |||
| c94883375a | |||
| 6565f1d7a3 | |||
| cefe7ae010 | |||
| 56f42ac97b | |||
| 9c7355d7f6 | |||
| 2ff2e3cdb8 | |||
| 18c6f9594e | |||
| a5c2467cdf | |||
| 698868b6ee | |||
| cbee38ae54 | |||
| e19536ce99 | |||
| b92ab0bf1d | |||
| bc049a3c77 | |||
| a026a610a1 | |||
| e71a17cbce | |||
| 86c09f3b25 | |||
| 788ecee65d | |||
| 98cbb07845 | |||
| 93bab6d5eb | |||
| 99f79b7616 | |||
| 47dc988e49 | |||
| 072de12cc1 | |||
| 77f755d07c | |||
| bc5f9b1085 | |||
| 4660025775 | |||
| 336cc875a1 | |||
| d943b2ebc2 | |||
| f982b51ff6 | |||
| 5b7a33f48b | |||
| 753e521a41 | |||
| d479f36662 | |||
| 6984f78746 | |||
| 030f337e74 |
+60
-17
@@ -10,13 +10,15 @@ declare(strict_types=1);
|
|||||||
namespace Kiri;
|
namespace Kiri;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Message\Stream;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Abstracts\Logger;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Message\Stream;
|
||||||
use Swoole\Client as SwowClient;
|
use Swoole\Client as SwowClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Client
|
* Class Client
|
||||||
* @package Kiri\Kiri\Http
|
* @package Kiri\Http
|
||||||
*/
|
*/
|
||||||
class AsyncClient extends ClientAbstracts
|
class AsyncClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
@@ -60,7 +62,7 @@ class AsyncClient extends ClientAbstracts
|
|||||||
try {
|
try {
|
||||||
$this->generate_client($data, ...$url);
|
$this->generate_client($data, ...$url);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
|
Kiri::getDi()->get(Logger::class)->error('rpc', [error_trigger_format($exception)]);
|
||||||
$this->setStatusCode(-1);
|
$this->setStatusCode(-1);
|
||||||
$this->setBody(jTraceEx($exception));
|
$this->setBody(jTraceEx($exception));
|
||||||
}
|
}
|
||||||
@@ -77,13 +79,11 @@ class AsyncClient extends ClientAbstracts
|
|||||||
private function generate_client($data, $host, $isHttps, $path): void
|
private function generate_client($data, $host, $isHttps, $path): void
|
||||||
{
|
{
|
||||||
$this->client = new SwowClient(SWOOLE_TCP, FALSE);
|
$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())) {
|
if (!$this->client->connect($host, $this->getPort())) {
|
||||||
throw new Exception('链接失败');
|
throw new Exception('链接失败');
|
||||||
}
|
}
|
||||||
if ($isHttps || $this->isSSL()) {
|
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
|
||||||
$this->client->enableSSL();
|
|
||||||
}
|
|
||||||
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
|
|
||||||
if (!empty($this->getAgent())) {
|
if (!empty($this->getAgent())) {
|
||||||
$this->withAddedHeader('User-Agent', $this->getAgent());
|
$this->withAddedHeader('User-Agent', $this->getAgent());
|
||||||
}
|
}
|
||||||
@@ -102,19 +102,24 @@ class AsyncClient extends ClientAbstracts
|
|||||||
* @param string $path
|
* @param string $path
|
||||||
* @param string $content
|
* @param string $content
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
private function execute(string $path, string $content)
|
private function execute(string $path, string $content)
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = $this->_parseHeaders($path);
|
||||||
$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();
|
|
||||||
|
|
||||||
|
$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, $body] = explode("\r\n\r\n", $receive);
|
||||||
|
|
||||||
$header = explode("\r\n", $header);
|
$header = explode("\r\n", $header);
|
||||||
@@ -126,6 +131,39 @@ class AsyncClient extends ClientAbstracts
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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()
|
private function chunked()
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -172,6 +210,11 @@ class AsyncClient extends ClientAbstracts
|
|||||||
*/
|
*/
|
||||||
public function close(): void
|
public function close(): void
|
||||||
{
|
{
|
||||||
$this->client->close();
|
/** @var SwowClient $client */
|
||||||
|
$client = $this->client;
|
||||||
|
if (!$client || !$client->isConnected()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$client->close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -12,12 +12,12 @@ namespace Kiri;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Logger;
|
use Kiri\Abstracts\Logger;
|
||||||
use Kiri\Kiri;
|
use Kiri;
|
||||||
use Swoole\Coroutine\Http\Client as SwowClient;
|
use Swoole\Coroutine\Http\Client as SwowClient;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Client
|
* Class Client
|
||||||
* @package Kiri\Kiri\Http
|
* @package Kiri\Http
|
||||||
*/
|
*/
|
||||||
class CoroutineClient extends ClientAbstracts
|
class CoroutineClient extends ClientAbstracts
|
||||||
{
|
{
|
||||||
@@ -67,7 +67,7 @@ class CoroutineClient extends ClientAbstracts
|
|||||||
$this->setBody($this->client->getBody());
|
$this->setBody($this->client->getBody());
|
||||||
$this->setResponseHeader($this->client->headers);
|
$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', [error_trigger_format($exception)]);
|
||||||
$this->setStatusCode(-1);
|
$this->setStatusCode(-1);
|
||||||
$this->setBody(jTraceEx($exception));
|
$this->setBody(jTraceEx($exception));
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-23
@@ -8,31 +8,30 @@ trait TSwooleClient
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[Pure] private function settings(): array
|
||||||
|
{
|
||||||
|
$sslCert = $this->getSslCertFile();
|
||||||
|
$sslKey = $this->getSslKeyFile();
|
||||||
|
$sslCa = $this->getCa();
|
||||||
|
|
||||||
/**
|
$params = [];
|
||||||
* @return array
|
if ($this->getConnectTimeout() > 0) {
|
||||||
*/
|
$params['timeout'] = $this->getConnectTimeout();
|
||||||
#[Pure] private function settings(): array
|
}
|
||||||
{
|
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
||||||
$sslCert = $this->getSslCertFile();
|
return $params;
|
||||||
$sslKey = $this->getSslKeyFile();
|
}
|
||||||
$sslCa = $this->getCa();
|
|
||||||
|
|
||||||
$params = [];
|
$params['ssl_host_name'] = $this->getHost();
|
||||||
if ($this->getConnectTimeout() > 0) {
|
$params['ssl_cert_file'] = $this->getSslCertFile();
|
||||||
$params['timeout'] = $this->getConnectTimeout();
|
$params['ssl_key_file'] = $this->getSslKeyFile();
|
||||||
}
|
$params['ssl_verify_peer'] = TRUE;
|
||||||
if (empty($sslCert) || empty($sslKey) || empty($sslCa)) {
|
$params['ssl_cafile'] = $sslCa;
|
||||||
return $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
$params['ssl_host_name'] = $this->getHost();
|
return $params;
|
||||||
$params['ssl_cert_file'] = $this->getSslCertFile();
|
}
|
||||||
$params['ssl_key_file'] = $this->getSslKeyFile();
|
|
||||||
$params['ssl_verify_peer'] = TRUE;
|
|
||||||
$params['ssl_cafile'] = $sslCa;
|
|
||||||
|
|
||||||
return $params;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user