This commit is contained in:
2021-09-29 15:41:13 +08:00
parent be4cb23bb9
commit 42abe70f99
9 changed files with 189 additions and 615 deletions
+22 -29
View File
@@ -7,11 +7,14 @@
*/ */
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Http\Handler\Client;
use Exception; use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Swoole\Coroutine\Http\Client as SClient; use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Http\Client as SwowClient;
/** /**
* Class Client * Class Client
@@ -24,12 +27,12 @@ class Client extends ClientAbstracts
* @param string $method * @param string $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return array|string|Result * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function request(string $method, $path, array $params = []): array|string|Result public function request(string $method, $path, array $params = []): ResponseInterface
{ {
return $this->setMethod($method) return $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $this->matchHost($path),
$this->paramEncode($params) $this->paramEncode($params)
@@ -40,33 +43,23 @@ class Client extends ClientAbstracts
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
* @return array|string|Result * @return ResponseInterface
* @throws Exception 使用swoole协程方式请求 * @throws Exception 使用swoole协程方式请求
*/ */
private function coroutine($url, array|string $data = []): array|string|Result private function coroutine($url, array|string $data = []): ResponseInterface
{ {
try { try {
$client = $this->generate_client($data, ...$url); $client = $this->generate_client($data, ...$url);
$this->setData('');
if ($client->statusCode < 0) { if ($client->statusCode < 0) {
throw new Exception($client->errMsg); throw new Exception($client->errMsg);
} }
$body = $this->resolve($client->getHeaders(), $client->body); return (new Response())->withStatus($client->getStatusCode())
if (in_array($client->getStatusCode(), [200, 201])) { ->withHeaders($client->getHeaders())
return $this->structure($body, $data, $client->getHeaders()); ->withBody(new Stream($client->getBody()));
}
if (is_string($body)) {
$message = 'Request error code ' . $client->getStatusCode();
} else {
$message = $this->searchMessageByData($body);
}
return $this->fail($client->getStatusCode(), $message, $body, $client->getHeaders());
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception, 'rpc'); $this->addError($exception, 'rpc');
return $this->fail(500, $exception->getMessage(), [ return (new Response())->withStatus(-1)->withHeaders([])
'file' => $exception->getFile(), ->withBody(new Stream(jTraceEx($exception)));
'line' => $exception->getLine()
], []);
} }
} }
@@ -76,18 +69,18 @@ class Client extends ClientAbstracts
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path * @param $path
* @return SClient * @return SwowClient
*/ */
private function generate_client($data, $host, $isHttps, $path): SClient private function generate_client($data, $host, $isHttps, $path): SwowClient
{ {
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$client = new SClient($host, 443, true); $client = new SwowClient($host, 443, true);
} else { } else {
$client = new SClient($host, $this->getPort(), false); $client = new SwowClient($host, $this->getPort(), false);
} }
$client->set($this->settings()); $client->set($this->settings());
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
$this->addHeader('User-Agent', $this->getAgent()); $this->withAddedHeader('User-Agent', $this->getAgent());
} }
$client->setHeaders($this->getHeader()); $client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod())); $client->setMethod(strtoupper($this->getMethod()));
@@ -98,12 +91,12 @@ class Client extends ClientAbstracts
/** /**
* @param SClient $client * @param SwowClient $client
* @param $path * @param $path
* @param $data * @param $data
* @return string * @return string
*/ */
private function setParams(SClient $client, $path, $data): string private function setParams(SwowClient $client, $path, $data): string
{ {
if ($this->isGet()) { if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data; if (!empty($data)) $path .= '?' . $data;
+84 -224
View File
@@ -1,13 +1,17 @@
<?php <?php
namespace Http\Client; namespace Http\Handler\Client;
use Closure; use Closure;
use Http\Handler\Context;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Core\Help; use Kiri\Core\Help;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Swoole\Coroutine\System; use Swoole\Coroutine\System;
defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/'); defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(:\d+)?)((\/[a-zA-Z0-9\-]+)+[\/]?(\?[a-zA-Z]+=.*)?)?/');
@@ -15,7 +19,7 @@ defined('SPLIT_URL') or define('SPLIT_URL', '/(http[s]?:\/\/)?(([\w\-_]+\.)+\w+(
/** /**
* Class ClientAbstracts * Class ClientAbstracts
* @package Http\Client * @package Http\Handler\Client
*/ */
abstract class ClientAbstracts extends Component implements IClient abstract class ClientAbstracts extends Component implements IClient
{ {
@@ -34,23 +38,18 @@ abstract class ClientAbstracts extends Component implements IClient
private int $timeout = 0; private int $timeout = 0;
private ?Closure $callback = null;
private string $method = 'get'; private string $method = 'get';
private bool $isSSL = false; private bool $isSSL = false;
private string $agent = ''; private string $agent = '';
private string $errorCodeField = '';
private string $errorMsgField = '';
private bool $use_swoole = false;
private string $ssl_cert_file = ''; private string $ssl_cert_file = '';
private string $ssl_key_file = ''; private string $ssl_key_file = '';
private string $ca = ''; private string $ca = '';
private int $port = 80; private int $port = 80;
/** @var string $_message 错误信息 */
private string $_message = ''; private StreamInterface $_data;
private string $_data = '';
private int $connect_timeout = 1; private int $connect_timeout = 1;
@@ -58,24 +57,18 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @return static * @return static
*/ */
#[Pure] public static function NewRequest(): static public static function NewRequest(): static
{ {
return new static(); return new static();
} }
protected function cleanData(): void
{
$this->_data = '';
}
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function post(string $path, array $params = []): array|int|string|Result public function post(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::POST, $path, $params); return $this->request(self::POST, $path, $params);
} }
@@ -84,9 +77,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function put(string $path, array $params = []): array|int|string|Result public function put(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::PUT, $path, $params); return $this->request(self::PUT, $path, $params);
} }
@@ -94,19 +87,21 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $contentType * @param string $contentType
* @return ClientAbstracts
*/ */
public function setContentType(string $contentType): void public function withContentType(string $contentType): static
{ {
$this->header['Content-Type'] = $contentType; $this->header['Content-Type'] = $contentType;
return $this;
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function head(string $path, array $params = []): array|int|string|Result public function head(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::HEAD, $path, $params); return $this->request(self::HEAD, $path, $params);
} }
@@ -115,9 +110,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function get(string $path, array $params = []): array|int|string|Result public function get(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::GET, $path, $params); return $this->request(self::GET, $path, $params);
} }
@@ -125,9 +120,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function option(string $path, array $params = []): array|int|string|Result public function option(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::OPTIONS, $path, $params); return $this->request(self::OPTIONS, $path, $params);
} }
@@ -135,9 +130,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): array|int|string|Result public function delete(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::DELETE, $path, $params); return $this->request(self::DELETE, $path, $params);
} }
@@ -145,9 +140,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function options(string $path, array $params = []): array|int|string|Result public function options(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::OPTIONS, $path, $params); return $this->request(self::OPTIONS, $path, $params);
@@ -156,9 +151,9 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|int|string|Result * @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): array|int|string|Result public function upload(string $path, array $params = []): ResponseInterface
{ {
return $this->request(self::UPLOAD, $path, $params); return $this->request(self::UPLOAD, $path, $params);
} }
@@ -175,7 +170,7 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @return int * @return int
*/ */
protected function getHostPort(): int #[Pure] protected function getHostPort(): int
{ {
if (!empty($this->getPort())) { if (!empty($this->getPort())) {
return $this->getPort(); return $this->getPort();
@@ -188,14 +183,15 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $host * @param string $host
* @return ClientAbstracts
*/ */
public function setHost(string $host): void public function withHost(string $host): static
{ {
$this->host = $host; $this->host = $host;
if ($this->use_swoole) { if (Context::inCoroutine()) {
$this->host = System::gethostbyname($host); $this->host = System::gethostbyname($host);
} }
$this->addHeader('Host', $host); return $this->withAddedHeader('Host', $host);
} }
/** /**
@@ -208,35 +204,39 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param array $header * @param array $header
* @return ClientAbstracts
*/ */
public function setHeader(array $header): void public function withHeader(array $header): static
{ {
$this->header = $header; $this->header = $header;
return $this;
} }
/** /**
* @param array $header * @param array $header
* @return array * @return ClientAbstracts
*/ */
public function setHeaders(array $header): array public function withHeaders(array $header): static
{ {
if (empty($header)) { if (empty($header)) {
return []; return $this;
} }
foreach ($header as $key => $val) { foreach ($header as $key => $val) {
$this->header[$key] = $val; $this->header[$key] = $val;
} }
return $this->header; return $this;
} }
/** /**
* @param $key * @param $key
* @param $value * @param $value
* @return ClientAbstracts
*/ */
public function addHeader($key, $value): void public function withAddedHeader($key, $value): static
{ {
$this->header[$key] = $value; $this->header[$key] = $value;
return $this;
} }
/** /**
@@ -249,26 +249,22 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param int $value * @param int $value
* @return ClientAbstracts
*/ */
public function setTimeout(int $value): void public function withTimeout(int $value): static
{ {
$this->timeout = $value; $this->timeout = $value;
return $this;
} }
/**
* @return Closure|null
*/
public function getCallback(): ?Closure
{
return $this->callback;
}
/** /**
* @param Closure|null $value * @param Closure|null $value
* @return ClientAbstracts
*/ */
public function setCallback(?Closure $value): void public function withCallback(?Closure $value): static
{ {
$this->callback = $value; return $this;
} }
/** /**
@@ -283,7 +279,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param string $value * @param string $value
* @return static * @return static
*/ */
public function setMethod(string $value): static public function withMethod(string $value): static
{ {
$this->method = $value; $this->method = $value;
return $this; return $this;
@@ -299,10 +295,12 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param bool $isSSL * @param bool $isSSL
* @return ClientAbstracts
*/ */
public function setIsSSL(bool $isSSL): void public function withIsSSL(bool $isSSL): static
{ {
$this->isSSL = $isSSL; $this->isSSL = $isSSL;
return $this;
} }
/** /**
@@ -315,59 +313,14 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $agent * @param string $agent
* @return ClientAbstracts
*/ */
public function setAgent(string $agent): void public function withAgent(string $agent): static
{ {
$this->agent = $agent; $this->agent = $agent;
return $this;
} }
/**
* @return string
*/
public function getErrorCodeField(): string
{
return $this->errorCodeField;
}
/**
* @param string $errorCodeField
*/
public function setErrorCodeField(string $errorCodeField): void
{
$this->errorCodeField = $errorCodeField;
}
/**
* @return string
*/
public function getErrorMsgField(): string
{
return $this->errorMsgField;
}
/**
* @param string $errorMsgField
*/
public function setErrorMsgField(string $errorMsgField): void
{
$this->errorMsgField = $errorMsgField;
}
/**
* @return bool
*/
public function isUseSwoole(): bool
{
return $this->use_swoole;
}
/**
* @param bool $use_swoole
*/
public function setUseSwoole(bool $use_swoole): void
{
$this->use_swoole = $use_swoole;
}
/** /**
* @return string * @return string
@@ -379,10 +332,12 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $ssl_cert_file * @param string $ssl_cert_file
* @return ClientAbstracts
*/ */
public function setSslCertFile(string $ssl_cert_file): void public function withSslCertFile(string $ssl_cert_file): static
{ {
$this->ssl_cert_file = $ssl_cert_file; $this->ssl_cert_file = $ssl_cert_file;
return $this;
} }
/** /**
@@ -395,10 +350,12 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $ssl_key_file * @param string $ssl_key_file
* @return ClientAbstracts
*/ */
public function setSslKeyFile(string $ssl_key_file): void public function withSslKeyFile(string $ssl_key_file): static
{ {
$this->ssl_key_file = $ssl_key_file; $this->ssl_key_file = $ssl_key_file;
return $this;
} }
/** /**
@@ -411,16 +368,18 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param string $ssl_key_file * @param string $ssl_key_file
* @return static
*/ */
public function setCa(string $ssl_key_file): void public function withCa(string $ssl_key_file): static
{ {
$this->ca = $ssl_key_file; $this->ca = $ssl_key_file;
return $this;
} }
/** /**
* @return int * @return int
*/ */
public function getPort(): int #[Pure] public function getPort(): int
{ {
if ($this->isSSL()) { if ($this->isSSL()) {
return 443; return 443;
@@ -433,42 +392,34 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param int $port * @param int $port
* @return ClientAbstracts
*/ */
public function setPort(int $port): void public function withPort(int $port): static
{ {
$this->port = $port; $this->port = $port;
return $this;
} }
/**
* @return string
*/
public function getMessage(): string
{
return $this->_message;
}
/** /**
* @param string $message * @return StreamInterface
*/ */
public function setMessage(string $message): void public function getData(): StreamInterface
{
$this->_message = $message;
}
/**
* @return string
*/
public function getData(): string
{ {
return $this->_data; return $this->_data;
} }
/** /**
* @param string $data * @param string|StreamInterface $data
* @return ClientAbstracts
*/ */
public function setData(string $data): void public function withBody(string|StreamInterface $data): static
{ {
if (is_string($data)) {
$data = new Stream($data);
}
$this->_data = $data; $this->_data = $data;
return $this;
} }
/** /**
@@ -482,7 +433,7 @@ abstract class ClientAbstracts extends Component implements IClient
/** /**
* @param int $connect_timeout * @param int $connect_timeout
*/ */
public function setConnectTimeout(int $connect_timeout): void public function withConnectTimeout(int $connect_timeout): void
{ {
$this->connect_timeout = $connect_timeout; $this->connect_timeout = $connect_timeout;
} }
@@ -517,7 +468,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url * @param $url
* @return bool * @return bool
*/ */
#[Pure] protected function isHttp($url): bool protected function isHttp($url): bool
{ {
return str_starts_with($url, 'http://'); return str_starts_with($url, 'http://');
} }
@@ -526,7 +477,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $url * @param $url
* @return bool * @return bool
*/ */
#[Pure] protected function isHttps($url): bool protected function isHttps($url): bool
{ {
return str_starts_with($url, 'https://'); return str_starts_with($url, 'https://');
} }
@@ -594,78 +545,6 @@ abstract class ClientAbstracts extends Component implements IClient
} }
/**
* @param $body
* @param $_data
* @param array $header
* @param int $statusCode
* @return mixed 构建返回体
* 构建返回体
*/
protected function structure($body, $_data, $header = [], $statusCode = 200): mixed
{
if ($this->callback instanceof Closure) {
$result = call_user_func($this->callback, $body, $_data, $header);
} else {
$result = $this->parseResult($body, $header, $statusCode);
}
return $result;
}
/**
* @param $body
* @param $header
* @param $statusCode
* @return Result
*/
private function parseResult($body, $header, $statusCode): Result
{
if (is_string($body)) {
$result['code'] = 0;
$result['message'] = '';
} else {
$result['code'] = $body[$this->errorCodeField] ?? 0;
$result['message'] = $this->searchMessageByData($body);
}
$result['data'] = $body;
$result['header'] = $header;
$result['httpStatus'] = $statusCode;
return new Result($result);
}
/**
* @param $body
* @return mixed
*/
protected function searchMessageByData($body): mixed
{
$parent = [];
if (empty($this->errorMsgField)) {
return 'system success.';
}
$explode = explode('.', $this->errorMsgField);
if (!isset($body[$explode[0]])) {
return 'system success.';
}
foreach ($explode as $item) {
if (empty($item)) {
continue;
}
if (empty($parent)) {
$parent = $body[$item];
continue;
}
if (is_string($parent) || !isset($parent[$item])) {
break;
}
$parent = $parent[$item];
}
return !empty($parent) ? $parent : 'system success.';
}
/** /**
* @return bool * @return bool
* check isPost Request * check isPost Request
@@ -755,7 +634,7 @@ abstract class ClientAbstracts extends Component implements IClient
$host = $this->getHost(); $host = $this->getHost();
if ($string == '/') { if ($string == '/') {
$string = ''; $string = '';
} else if (strpos($string, '/') !== 0) { } else if (!str_starts_with($string, '/')) {
$string = '/' . $string; $string = '/' . $string;
} }
return [$host, $this->isSSL(), $string]; return [$host, $this->isSSL(), $string];
@@ -767,7 +646,7 @@ abstract class ClientAbstracts extends Component implements IClient
* @param $params * @param $params
* @return string * @return string
*/ */
#[Pure] protected function joinGetParams($path, $params): string protected function joinGetParams($path, $params): string
{ {
if (empty($params)) { if (empty($params)) {
return $path; return $path;
@@ -778,29 +657,10 @@ abstract class ClientAbstracts extends Component implements IClient
if (str_contains($path, '?')) { if (str_contains($path, '?')) {
[$path, $getParams] = explode('?', $path); [$path, $getParams] = explode('?', $path);
} }
if (!isset($getParams) || empty($getParams)) { if (empty($getParams)) {
return $path . '?' . $params; return $path . '?' . $params;
} }
return $path . '?' . $params . '&' . $getParams; return $path . '?' . $params . '&' . $getParams;
} }
/**
* @param $code
* @param $message
* @param $data
* @param $header
* @return Result
*/
protected function fail($code, $message, $data = [], $header = []): Result
{
return new Result([
'code' => $code,
'message' => $message,
'data' => $data,
'header' => $header,
]);
}
} }
+18 -43
View File
@@ -1,17 +1,20 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Http\Handler\Client;
use CurlHandle; use CurlHandle;
use Exception; use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\ResponseInterface;
/** /**
* Class Curl * Class Curl
* @package Http\Client * @package Http\Handler\Client
*/ */
class Curl extends ClientAbstracts class Curl extends ClientAbstracts
{ {
@@ -20,10 +23,10 @@ class Curl extends ClientAbstracts
* @param $method * @param $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return Result|array|string * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function request($method, $path, array $params = []): Result|array|string public function request($method, $path, array $params = []): ResponseInterface
{ {
if ($method == self::GET) { if ($method == self::GET) {
$path = $this->joinGetParams($path, $params); $path = $this->joinGetParams($path, $params);
@@ -131,67 +134,39 @@ class Curl extends ClientAbstracts
/** /**
* @param $curl * @param $curl
* @return Result|bool|array|string * @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
private function execute($curl): Result|bool|array|string private function execute($curl): ResponseInterface
{ {
$output = curl_exec($curl); $output = curl_exec($curl);
curl_close($curl);
if ($output === false) { if ($output === false) {
$response = $this->fail(400, curl_error($curl)); $response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($curl)));
} else { } else {
$response = $this->parseResponse($curl, $output); $response = $this->explode($output);
} }
$this->cleanData();
return $response; return $response;
} }
/** /**
* @param $curl
* @param $output * @param $output
* @param array $params * @return ResponseInterface
* @return mixed
* @throws Exception * @throws Exception
*/ */
private function parseResponse($curl, $output, array $params = []): mixed private function explode($output): ResponseInterface
{ {
curl_close($curl);
if ($output === FALSE) {
return $this->fail(500, $output);
}
[$header, $body, $status] = $this->explode($output);
if ($status != 200 && $status != 201) {
$data = $this->fail($status, $body, [], $header);
} else {
$data = $this->structure($body, $params, $header);
}
return $data;
}
/**
* @param $output
* @return array
* @throws Exception
*/
private function explode($output): array
{
if (empty($output) || !str_contains($output, "\r\n\r\n")) {
throw new Exception('Get data null.');
}
[$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 = (int)explode(' ', trim($header[0]))[1]; return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
$header = $this->headerFormat($header); ->withBody(new Stream($body));
return [$header, $this->resolve($header, $body), $status];
} }
/** /**
@@ -204,7 +179,7 @@ class Curl extends ClientAbstracts
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;
} }
-64
View File
@@ -1,64 +0,0 @@
<?php
namespace Http\Client;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component;
use ReflectionException;
use Swoole\Coroutine;
/**
* Class ClientDriver
* @package Http\Client
* @mixin Client
*/
class HttpClient extends Component
{
/**
* @return IClient
*/
public static function NewRequest(): IClient
{
if (Coroutine::getCid() > -1) {
return Client::NewRequest();
}
return Curl::NewRequest();
}
/**
* @return Curl
*/
#[Pure] public function getCurl(): Curl
{
return Curl::NewRequest();
}
/**
* @return Client
*/
#[Pure] public function getCoroutine(): Client
{
return Client::NewRequest();
}
/**
* @param string $name
* @param array $arguments
* @return void
*/
public function __call(string $name, array $arguments)
{
if (!method_exists($this, $name)) {
return static::NewRequest()->{$name}(...$arguments);
}
return $this->{$name}(...$arguments);
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace Http\Client; namespace Http\Handler\Client;
use Exception; use Exception;
+51 -77
View File
@@ -1,10 +1,12 @@
<?php <?php
namespace Http\Client; namespace Http\Handler\Client;
use Closure; use Closure;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
interface IClient interface IClient
{ {
@@ -13,205 +15,177 @@ interface IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function get(string $path, array $params = []): Result|int|array|string; public function get(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function post(string $path, array $params = []): Result|int|array|string; public function post(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): Result|int|array|string; public function delete(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function options(string $path, array $params = []): Result|int|array|string; public function options(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): Result|int|array|string; public function upload(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function put(string $path, array $params = []): Result|int|array|string; public function put(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function head(string $path, array $params = []): Result|int|array|string; public function head(string $path, array $params = []): ResponseInterface;
/** /**
* @param string $method * @param string $method
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return array|Result|int|string * @return ResponseInterface
*/ */
public function request(string $method, string $path, array $params = []): Result|array|int|string; public function request(string $method, string $path, array $params = []): ResponseInterface;
/** /**
* @param string $host * @param string $host
* @return mixed * @return static
*/ */
public function setHost(string $host): void; public function withHost(string $host): static;
/** /**
* @param array $header * @param array $header
* @return mixed * @return static
*/ */
public function setHeader(array $header): void; public function withHeader(array $header): static;
/** /**
* @param array $header * @param array $header
* @return mixed * @return static
*/ */
public function setHeaders(array $header): array; public function withHeaders(array $header): static;
/** /**
* @param string $key * @param string $key
* @param string $value * @param string $value
* @return mixed * @return static
*/ */
public function addHeader(string $key, string $value): void; public function withAddedHeader(string $key, string $value): static;
/** /**
* @param int $value * @param int $value
* @return mixed * @return static
*/ */
public function setTimeout(int $value): void; public function withTimeout(int $value): static;
/** /**
* @param Closure|null $value * @param Closure|null $value
* @return mixed * @return static
*/ */
public function setCallback(?Closure $value): void; public function withCallback(?Closure $value): static;
/** /**
* @param string $value * @param string $value
* @return static * @return static
*/ */
public function setMethod(string $value): static; public function withMethod(string $value): static;
/** /**
* @param bool $isSSL * @param bool $isSSL
* @return mixed * @return static
*/ */
public function setIsSSL(bool $isSSL): void; public function withIsSSL(bool $isSSL): static;
/** /**
* @param string $agent * @param string $agent
* @return mixed * @return static
*/ */
public function setAgent(string $agent): void; public function withAgent(string $agent): static;
/**
* @param string $errorCodeField
* @return mixed
*/
public function setErrorCodeField(string $errorCodeField): void;
/**
* @param string $errorMsgField
* @return mixed
*/
public function setErrorMsgField(string $errorMsgField): void;
/**
* @param bool $use_swoole
* @return mixed
*/
public function setUseSwoole(bool $use_swoole): void;
/** /**
* @param string $ssl_cert_file * @param string $ssl_cert_file
* @return mixed * @return static
*/ */
public function setSslCertFile(string $ssl_cert_file): void; public function withSslCertFile(string $ssl_cert_file): static;
/** /**
* @param string $ssl_key_file * @param string $ssl_key_file
* @return mixed * @return static
*/ */
public function setSslKeyFile(string $ssl_key_file): void; public function withSslKeyFile(string $ssl_key_file): static;
/** /**
* @param string $ssl_key_file * @param string $ssl_key_file
* @return mixed * @return static
*/ */
public function setCa(string $ssl_key_file): void; public function withCa(string $ssl_key_file): static;
/** /**
* @param int $port * @param int $port
* @return mixed * @return static
*/ */
public function setPort(int $port): void; public function withPort(int $port): static;
/** /**
* @param string $message * @param string|StreamInterface $data
* @return mixed * @return static
*/ */
public function setMessage(string $message): void; public function withBody(string|StreamInterface $data): static;
/**
* @param string $data
* @return mixed
*/
public function setData(string $data): void;
/** /**
* @param int $connect_timeout * @param int $connect_timeout
* @return mixed * @return static
*/ */
public function setConnectTimeout(int $connect_timeout): void; public function withConnectTimeout(int $connect_timeout): static;
/** /**
* @param string $contentType * @param string $contentType
* @return mixed * @return static
*/ */
public function setContentType(string $contentType): void; public function withContentType(string $contentType): static;
} }
-175
View File
@@ -1,175 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\Client;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Result
*
* @package app\components
*
* @property $code
* @property $message
* @property $count
* @property $data
*/
class Result
{
public int|string $code;
public string $message;
public int $count = 0;
public mixed $data;
public ?array $header;
public int $httpStatus = 200;
public int $startTime = 0;
public int $requestTime = 0;
public float $runTime = 0;
/**
* Result constructor.
* @param array $data
*/
public function __construct(array $data)
{
$this->setAssignment($data);
}
/**
* @param $data
* @return $this
*/
public function setAssignment($data): static
{
foreach ($data as $key => $val) {
if (!property_exists($this, $key)) {
continue;
}
$this->$key = $val;
}
return $this;
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
return $this->$name;
}
/**
* @param $name
* @param $value
*/
public function __set($name, $value)
{
$this->$name = $value;
}
/**
* @return array
*/
public function getHeaders(): array
{
$_tmp = [];
if (!is_array($this->header)) {
return $_tmp;
}
foreach ($this->header as $key => $val) {
if ($key == 0) {
$_tmp['pro'] = $val;
} else {
if (str_contains($val, ': ')) {
$trim = explode(': ', $val);
$_tmp[strtolower($trim[0])] = $trim[1];
} else {
$_tmp[$key] = $val;
}
}
}
return $_tmp;
}
/**
* @return array
*/
public function getTime(): array
{
return [
'startTime' => $this->startTime,
'requestTime' => $this->requestTime,
'runTime' => $this->runTime,
];
}
/**
* @param $key
* @param $data
* @return $this
* @throws Exception
*/
public function setAttr($key, $data): static
{
if (!property_exists($this, $key)) {
throw new Exception('未查找到相应对象属性');
}
$this->$key = $data;
return $this;
}
/**
* @param int $status
* @return bool
*/
#[Pure] public function isResultsOK(int $status = 0): bool
{
if (!$this->httpIsOk()) {
return false;
}
return $this->code === $status;
}
/**
* @return bool
*/
public function httpIsOk(): bool
{
return in_array($this->httpStatus, [100, 101, 200, 201, 202, 203, 204, 205, 206]);
}
/**
* @return mixed
*/
public function getBody(): mixed
{
return $this->data;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* @return string|int
*/
public function getCode(): string|int
{
return $this->code;
}
}
+11
View File
@@ -194,6 +194,17 @@ trait Message
} }
/**
* @param array $headers
* @return static
*/
public function withHeaders(array $headers): static
{
$this->headers = $headers;
return $this;
}
/** /**
* @param $name * @param $name
* @param $value * @param $value
+2 -2
View File
@@ -7,8 +7,8 @@ namespace Kiri\Abstracts;
use Annotation\Annotation as SAnnotation; use Annotation\Annotation as SAnnotation;
use Database\Connection; use Database\Connection;
use Database\DatabasesProviders; use Database\DatabasesProviders;
use Http\Client\Client; use Http\Handler\Client\Client;
use Http\Client\Curl; use Http\Handler\Client\Curl;
use Http\Handler\Router; use Http\Handler\Router;
use Server\Server; use Server\Server;
use Kiri\Crontab\Producer; use Kiri\Crontab\Producer;