Compare commits
48 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 | |||
| ae3b0c411b | |||
| f838795983 |
+61
-19
@@ -7,17 +7,18 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
use Http\Message\Stream;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Message\Stream;
|
||||
use Swoole\Client as SwowClient;
|
||||
|
||||
/**
|
||||
* Class Client
|
||||
* @package Kiri\Kiri\Http
|
||||
* @package Kiri\Http
|
||||
*/
|
||||
class AsyncClient extends ClientAbstracts
|
||||
{
|
||||
@@ -61,7 +62,7 @@ class AsyncClient extends ClientAbstracts
|
||||
try {
|
||||
$this->generate_client($data, ...$url);
|
||||
} 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->setBody(jTraceEx($exception));
|
||||
}
|
||||
@@ -78,13 +79,11 @@ class AsyncClient extends ClientAbstracts
|
||||
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();
|
||||
}
|
||||
$this->client->set(array_merge($this->settings(), ['open_http_protocol' => true]));
|
||||
if ($isHttps || $this->isSSL()) $this->client->enableSSL();
|
||||
if (!empty($this->getAgent())) {
|
||||
$this->withAddedHeader('User-Agent', $this->getAgent());
|
||||
}
|
||||
@@ -103,19 +102,24 @@ class AsyncClient extends ClientAbstracts
|
||||
* @param string $path
|
||||
* @param string $content
|
||||
* @return void
|
||||
* @throws ConfigException
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content);
|
||||
$receive = $this->client->recv();
|
||||
$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);
|
||||
@@ -127,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()
|
||||
{
|
||||
|
||||
@@ -173,6 +210,11 @@ class AsyncClient extends ClientAbstracts
|
||||
*/
|
||||
public function close(): void
|
||||
{
|
||||
$this->client->close();
|
||||
/** @var SwowClient $client */
|
||||
$client = $this->client;
|
||||
if (!$client || !$client->isConnected()) {
|
||||
return;
|
||||
}
|
||||
$client->close();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Kiri\Context;
|
||||
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Http\Message\Stream;
|
||||
use Kiri\Message\Stream;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Context;
|
||||
use Kiri\Core\Help;
|
||||
|
||||
+4
-4
@@ -7,17 +7,17 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Kiri;
|
||||
use Kiri;
|
||||
use Swoole\Coroutine\Http\Client as SwowClient;
|
||||
|
||||
/**
|
||||
* Class Client
|
||||
* @package Kiri\Kiri\Http
|
||||
* @package Kiri\Http
|
||||
*/
|
||||
class CoroutineClient extends ClientAbstracts
|
||||
{
|
||||
@@ -67,7 +67,7 @@ class CoroutineClient extends ClientAbstracts
|
||||
$this->setBody($this->client->getBody());
|
||||
$this->setResponseHeader($this->client->headers);
|
||||
} 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->setBody(jTraceEx($exception));
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,12 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Http\Message\Response;
|
||||
use Http\Message\Stream;
|
||||
use Kiri\Message\Response;
|
||||
use Kiri\Message\Stream;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
|
||||
use Closure;
|
||||
|
||||
+1
-2
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
@@ -8,7 +8,6 @@ trait TSwooleClient
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Http\\Client\\": "./"
|
||||
"Kiri\\": "./"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
Reference in New Issue
Block a user