Compare commits

...

7 Commits

Author SHA1 Message Date
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
as2252258 70b4400f95 改名 2021-11-18 10:46:24 +08:00
4 changed files with 185 additions and 103 deletions
+98 -25
View File
@@ -9,7 +9,6 @@ use Http\Message\Stream;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Context; use Kiri\Context;
use Kiri\Core\Help; use Kiri\Core\Help;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface; use Psr\Http\Message\StreamInterface;
use Swoole\Coroutine\System; use Swoole\Coroutine\System;
@@ -48,11 +47,93 @@ abstract class ClientAbstracts implements IClient
private int $port = 80; private int $port = 80;
private array $_responseHeader = [];
private int $statusCode = 200;
/**
* @var string|null
*/
protected ?string $body;
private ?StreamInterface $_data = null; private ?StreamInterface $_data = null;
private int $connect_timeout = 1; private int $connect_timeout = 1;
/**
* @var resource|\Swoole\Coroutine\Http\Client
*/
protected mixed $client;
/**
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* @return array
*/
public function getResponseHeaders(): array
{
return $this->_responseHeader;
}
/**
* @param string $key
* @return string|int|null
*/
public function getResponseHeader(string $key): null|string|int
{
return $this->_responseHeader[$key] ?? null;
}
/**
* @param array $responseHeader
*/
public function setResponseHeader(array $responseHeader): void
{
$this->_responseHeader = $responseHeader;
}
/**
* @param int $statusCode
*/
public function setStatusCode(int $statusCode): void
{
$this->statusCode = $statusCode;
}
/**
* @return string|null
*/
public function getBody(): string|null
{
return $this->body;
}
/**
* @param ?string $body
*/
public function setBody(?string $body): void
{
$this->body = $body;
}
/** /**
* @param $host * @param $host
* @param $port * @param $port
@@ -67,22 +148,20 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function post(string $path, array $params = []): ResponseInterface public function post(string $path, array $params = []): void
{ {
return $this->request(self::POST, $path, $params); $this->request(self::POST, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function put(string $path, array $params = []): ResponseInterface public function put(string $path, array $params = []): void
{ {
return $this->request(self::PUT, $path, $params); $this->request(self::PUT, $path, $params);
} }
@@ -100,63 +179,57 @@ abstract class ClientAbstracts implements IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function head(string $path, array $params = []): ResponseInterface public function head(string $path, array $params = []): void
{ {
return $this->request(self::HEAD, $path, $params); $this->request(self::HEAD, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function get(string $path, array $params = []): ResponseInterface public function get(string $path, array $params = []): void
{ {
return $this->request(self::GET, $path, $params); $this->request(self::GET, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function option(string $path, array $params = []): ResponseInterface public function option(string $path, array $params = []): void
{ {
return $this->request(self::OPTIONS, $path, $params); $this->request(self::OPTIONS, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): ResponseInterface public function delete(string $path, array $params = []): void
{ {
return $this->request(self::DELETE, $path, $params); $this->request(self::DELETE, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function options(string $path, array $params = []): ResponseInterface public function options(string $path, array $params = []): void
{ {
return $this->request(self::OPTIONS, $path, $params); $this->request(self::OPTIONS, $path, $params);
} }
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): ResponseInterface public function upload(string $path, array $params = []): void
{ {
return $this->request(self::UPLOAD, $path, $params); $this->request(self::UPLOAD, $path, $params);
} }
+31 -30
View File
@@ -10,12 +10,9 @@ 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;
use Psr\Http\Message\ResponseInterface;
use Swoole\Coroutine\Http\Client as SwowClient; use Swoole\Coroutine\Http\Client as SwowClient;
/** /**
@@ -29,12 +26,12 @@ class CoroutineClient extends ClientAbstracts
* @param string $method * @param string $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return ResponseInterface * @return void
* @throws Exception * @throws Exception
*/ */
public function request(string $method, $path, array $params = []): ResponseInterface public function request(string $method, $path, array $params = []): void
{ {
return $this->withMethod($method) $this->withMethod($method)
->coroutine( ->coroutine(
$this->matchHost($path), $this->matchHost($path),
$this->paramEncode($params) $this->paramEncode($params)
@@ -45,23 +42,22 @@ class CoroutineClient extends ClientAbstracts
/** /**
* @param $url * @param $url
* @param array|string $data * @param array|string $data
* @return ResponseInterface
* @throws Exception 使用swoole协程方式请求 * @throws Exception 使用swoole协程方式请求
*/ */
private function coroutine($url, array|string $data = []): ResponseInterface private function coroutine($url, array|string $data = []): void
{ {
try { try {
$client = $this->generate_client($data, ...$url); $this->generate_client($data, ...$url);
if ($client->statusCode < 0) { if ($this->client->statusCode < 0) {
throw new Exception($client->errMsg); throw new Exception($this->client->errMsg);
} }
return (new Response())->withStatus($client->getStatusCode()) $this->setStatusCode($this->client->getStatusCode());
->withHeaders($client->getHeaders()) $this->setBody($this->client->getBody());
->withBody(new Stream($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]);
return (new Response())->withStatus(-1)->withHeaders([]) $this->setStatusCode(-1);
->withBody(new Stream(jTraceEx($exception))); $this->setBody(jTraceEx($exception));
} }
} }
@@ -71,50 +67,55 @@ class CoroutineClient extends ClientAbstracts
* @param $host * @param $host
* @param $isHttps * @param $isHttps
* @param $path * @param $path
* @return SwowClient
*/ */
private function generate_client($data, $host, $isHttps, $path): SwowClient private function generate_client($data, $host, $isHttps, $path): void
{ {
if ($isHttps || $this->isSSL()) { if ($isHttps || $this->isSSL()) {
$client = new SwowClient($host, 443, true); $this->client = new SwowClient($host, 443, true);
} else { } else {
$client = new SwowClient($host, $this->getPort(), false); $this->client = new SwowClient($host, $this->getPort(), false);
} }
$client->set($this->settings()); $this->client->set($this->settings());
if (!empty($this->getAgent())) { if (!empty($this->getAgent())) {
$this->withAddedHeader('User-Agent', $this->getAgent()); $this->withAddedHeader('User-Agent', $this->getAgent());
} }
$client->setHeaders($this->getHeader()); $this->client->setHeaders($this->getHeader());
$client->setMethod(strtoupper($this->getMethod())); $this->client->setMethod(strtoupper($this->getMethod()));
$client->execute($this->setParams($client, $path, $data)); $this->client->execute($this->setParams($path, $data));
$client->close();
return $client;
} }
/** /**
* @param SwowClient $client
* @param $path * @param $path
* @param $data * @param $data
* @return string * @return string
*/ */
private function setParams(SwowClient $client, $path, $data): string private function setParams($path, $data): string
{ {
$content = $this->getData()->getContents(); $content = $this->getData()->getContents();
if (!empty($content)) { if (!empty($content)) {
$client->setData($content); $this->client->setData($content);
} }
if ($this->isGet()) { if ($this->isGet()) {
if (!empty($data)) $path .= '?' . $data; if (!empty($data)) $path .= '?' . $data;
} else { } else {
$data = $this->mergeParams($data); $data = $this->mergeParams($data);
if (!empty($data)) { if (!empty($data)) {
$client->setData($data); $this->client->setData($data);
} }
} }
return $path; return $path;
} }
/**
*
*/
public function close(): void
{
$this->client->close();
}
/** /**
* @return array * @return array
*/ */
+36 -32
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Http\Client; namespace Http\Client;
use CurlHandle;
use Exception; use Exception;
use Http\Message\Response; use Http\Message\Response;
use Http\Message\Stream; use Http\Message\Stream;
@@ -23,15 +22,17 @@ class Curl extends ClientAbstracts
* @param $method * @param $method
* @param $path * @param $path
* @param array $params * @param array $params
* @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
public function request($method, $path, array $params = []): ResponseInterface 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);
} }
return $this->execute($this->getCurlHandler($path, $method, $params));
$this->getCurlHandler($path, $method, $params);
$this->execute();
} }
@@ -39,10 +40,9 @@ class Curl extends ClientAbstracts
* @param $path * @param $path
* @param $method * @param $method
* @param $params * @param $params
* @return CurlHandle
* @throws Exception * @throws Exception
*/ */
private function getCurlHandler($path, $method, $params): CurlHandle private function getCurlHandler($path, $method, $params): void
{ {
[$host, $isHttps, $path] = $this->matchHost($path); [$host, $isHttps, $path] = $this->matchHost($path);
@@ -51,45 +51,43 @@ class Curl extends ClientAbstracts
$host .= ':' . $this->getPort(); $host .= ':' . $this->getPort();
} }
$resource = $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($resource); $this->curlHandlerSslSet();
} }
$contents = $this->getData()->getContents(); $contents = $this->getData()->getContents();
if (empty($params) && empty($contents)) { if (empty($params) && empty($contents)) {
return $resource; return;
} }
if (!empty($contents)) { if (!empty($contents)) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $contents); curl_setopt($this->client, CURLOPT_POSTFIELDS, $contents);
} else if ($method === self::POST) { } else if ($method === self::POST) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $this->mergeParams($params)); curl_setopt($this->client, CURLOPT_POSTFIELDS, $this->mergeParams($params));
} else if ($method === self::UPLOAD) { } else if ($method === self::UPLOAD) {
curl_setopt($resource, CURLOPT_POSTFIELDS, $params); curl_setopt($this->client, CURLOPT_POSTFIELDS, $params);
} }
return $resource;
} }
/** /**
* @param $resource
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function curlHandlerSslSet($resource): void private function curlHandlerSslSet(): void
{ {
if (!empty($this->ssl_key)) { if (!empty($this->ssl_key)) {
if (!file_exists($this->ssl_key)) { if (!file_exists($this->ssl_key)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLKEY, $this->getSslKeyFile()); curl_setopt($this->client, CURLOPT_SSLKEY, $this->getSslKeyFile());
} }
if (!empty($this->ssl_cert)) { if (!empty($this->ssl_cert)) {
if (!!file_exists($this->ssl_cert)) { if (!!file_exists($this->ssl_cert)) {
throw new Exception('SSL protocol certificate not found.'); throw new Exception('SSL protocol certificate not found.');
} }
curl_setopt($resource, CURLOPT_SSLCERT, $this->getSslCertFile()); curl_setopt($this->client, CURLOPT_SSLCERT, $this->getSslCertFile());
} }
} }
@@ -98,10 +96,9 @@ class Curl extends ClientAbstracts
* @param $resource * @param $resource
* @param $path * @param $path
* @param $method * @param $method
* @return CurlHandle
* @throws Exception * @throws Exception
*/ */
private function do($resource, $path, $method): CurlHandle 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()); // 超时设置
@@ -130,34 +127,40 @@ class Curl extends ClientAbstracts
} }
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method)); curl_setopt($resource, CURLOPT_CUSTOMREQUEST, strtoupper($method));
return $resource; $this->client = $resource;
} }
/** /**
* @param $curl
* @return ResponseInterface
* @throws Exception * @throws Exception
*/ */
private function execute($curl): ResponseInterface private function execute(): void
{ {
$output = curl_exec($curl); $output = curl_exec($this->client);
curl_close($curl);
if ($output === false) { if ($output === false) {
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($curl))); $this->setStatusCode(404);
$this->setBody(curl_error($this->client));
} else { } else {
$response = $this->explode($output); $this->explode($output);
} }
return $response; }
/**
*
*/
public function close(): void
{
curl_close($this->client);
} }
/** /**
* @param $output * @param $output
* @return ResponseInterface * @return void
* @throws Exception * @throws Exception
*/ */
private function explode($output): ResponseInterface private function explode($output): void
{ {
[$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') {
@@ -167,8 +170,9 @@ class Curl extends ClientAbstracts
$header = explode("\r\n", $header); $header = explode("\r\n", $header);
$status = explode(' ', array_shift($header)); $status = explode(' ', array_shift($header));
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header)) $this->setStatusCode(intval($status[1]));
->withBody(new Stream($body)); $this->setBody($body);
$this->setResponseHeader($header);
} }
/** /**
+20 -16
View File
@@ -15,66 +15,64 @@ interface IClient
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function get(string $path, array $params = []): ResponseInterface; public function get(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function post(string $path, array $params = []): ResponseInterface; public function post(string $path, array $params = []): void;
/**
*
*/
public function close(): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function delete(string $path, array $params = []): ResponseInterface; public function delete(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function options(string $path, array $params = []): ResponseInterface; public function options(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function upload(string $path, array $params = []): ResponseInterface; public function upload(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function put(string $path, array $params = []): ResponseInterface; public function put(string $path, array $params = []): void;
/** /**
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function head(string $path, array $params = []): ResponseInterface; public function head(string $path, array $params = []): void;
/** /**
* @param string $method * @param string $method
* @param string $path * @param string $path
* @param array $params * @param array $params
* @return ResponseInterface
*/ */
public function request(string $method, string $path, array $params = []): ResponseInterface; public function request(string $method, string $path, array $params = []): void;
/** /**
@@ -174,4 +172,10 @@ interface IClient
* @return static * @return static
*/ */
public function withContentType(string $contentType): static; public function withContentType(string $contentType): static;
/**
* @return mixed
*/
public function getBody(): mixed;
} }