Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae3b0c411b | |||
| f838795983 | |||
| 1db430f997 | |||
| fdb8816b5e | |||
| 2623deb63f | |||
| 98d9cee8b0 | |||
| cec004b670 | |||
| f268b705f7 | |||
| ac279a257a | |||
| 89ba6effeb | |||
| 29e520b412 | |||
| eec7cb7dcc | |||
| 6c71437e9e | |||
| 33fc0ae396 | |||
| 23f85ef5f0 | |||
| 372dbbb424 | |||
| d07dbf1c8d | |||
| 6ffe1af6d2 | |||
| ca7a4494d8 | |||
| 4f99ec5756 | |||
| 1d1b36155a | |||
| 00da068b53 | |||
| 08793bcf76 | |||
| f5ae86c3d2 | |||
| 4856e76e6f | |||
| 09dc2b4980 | |||
| 510daa1bb6 | |||
| 4dc1120aaf | |||
| 9076b65080 | |||
| adf09f5695 | |||
| 06290f5c9b | |||
| 196cb42e6e | |||
| a0f197d98d | |||
| c2d5ab5e1b | |||
| 597cacd191 | |||
| f48b6bd5d2 | |||
| 2baeb20b8d | |||
| 8d685b93e2 | |||
| 90cada46c4 | |||
| 0f5d277532 |
+177
@@ -0,0 +1,177 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/5/24 0024
|
||||
* Time: 11:34
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Message\Stream;
|
||||
use Kiri\Abstracts\Logger;
|
||||
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(array_merge($this->settings(), ['open_http_protocol' => true]));
|
||||
if (!empty($this->getAgent())) {
|
||||
$this->withAddedHeader('User-Agent', $this->getAgent());
|
||||
}
|
||||
|
||||
$path = $this->setParams($path, $data);
|
||||
|
||||
$this->withAddedHeader('Accept', ' text/html,application/xhtml+xml,application/json,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');
|
||||
$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);
|
||||
}
|
||||
}
|
||||
$this->client->send(implode("\r\n", $array) . "\r\n\r\n" . $content);
|
||||
$receive = $this->client->recv();
|
||||
|
||||
[$header, $body] = explode("\r\n\r\n", $receive);
|
||||
|
||||
$header = explode("\r\n", $header);
|
||||
$status = array_shift($header);
|
||||
|
||||
$this->setStatusCode(intval(explode(' ', $status)[1]));
|
||||
$this->parseResponseHeaders($header);
|
||||
$this->setBody($body);
|
||||
}
|
||||
|
||||
|
||||
private function chunked()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @return void
|
||||
*/
|
||||
private function parseResponseHeaders(array $headers)
|
||||
{
|
||||
$array = [];
|
||||
foreach ($headers as $header) {
|
||||
[$key, $value] = explode(': ', $header);
|
||||
|
||||
$array[$key] = trim($value);
|
||||
}
|
||||
$this->setResponseHeader($array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Kiri\Context;
|
||||
|
||||
|
||||
/**
|
||||
* @mixin CoroutineClient|Curl
|
||||
* @mixin CoroutineClient|CurlClient
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
|
||||
|
||||
private CoroutineClient|Curl|AsyncClient $abstracts;
|
||||
private CoroutineClient|CurlClient|AsyncClient $abstracts;
|
||||
|
||||
|
||||
/**
|
||||
@@ -25,7 +25,7 @@ class Client
|
||||
if (Context::inCoroutine()) {
|
||||
$this->abstracts = new CoroutineClient($host, $port, $isSsl);
|
||||
} else {
|
||||
$this->abstracts = new AsyncClient($host, $port, $isSsl);
|
||||
$this->abstracts = new CurlClient($host, $port, $isSsl);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -68,7 +68,7 @@ abstract class ClientAbstracts implements IClient
|
||||
|
||||
|
||||
/**
|
||||
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client
|
||||
* @var resource|\Swoole\Coroutine\Http\Client|\Swoole\Client|\CurlHandle
|
||||
*/
|
||||
protected mixed $client;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
@@ -1,21 +1,21 @@
|
||||
<?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;
|
||||
|
||||
|
||||
/**
|
||||
* Class Curl
|
||||
* Class CurlClient
|
||||
* @package Http\Handler\Client
|
||||
*/
|
||||
class Curl extends ClientAbstracts
|
||||
class CurlClient extends ClientAbstracts
|
||||
{
|
||||
|
||||
/**
|
||||
@@ -74,9 +74,6 @@ class Curl extends ClientAbstracts
|
||||
*/
|
||||
private function curlHandlerSslSet(): void
|
||||
{
|
||||
curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, $this->getVerifyPeer());
|
||||
curl_setopt($this->client, CURLOPT_VERBOSE, TRUE);
|
||||
// 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());
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use Exception;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
|
||||
use Closure;
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Http\Client;
|
||||
namespace Kiri;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
+1
-2
@@ -11,14 +11,13 @@
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"ext-json": "*",
|
||||
"ext-redis": "*",
|
||||
"ext-swoole": "*",
|
||||
"psr/http-client": "^1.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Http\\Client\\": "src/"
|
||||
"Kiri\\": "./"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user