Compare commits

...

6 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
3 changed files with 74 additions and 26 deletions
+61 -10
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,10 +47,16 @@ abstract class ClientAbstracts implements IClient
private int $port = 80; private int $port = 80;
private array $_responseHeader = [];
private int $statusCode = 200;
/** /**
* @var ResponseInterface|null * @var string|null
*/ */
protected ?ResponseInterface $body; protected ?string $body;
private ?StreamInterface $_data = null; private ?StreamInterface $_data = null;
@@ -59,25 +64,71 @@ abstract class ClientAbstracts implements IClient
private int $connect_timeout = 1; private int $connect_timeout = 1;
/**
* @var resource|\Swoole\Coroutine\Http\Client
*/
protected mixed $client; protected mixed $client;
/** /**
* @return string|ResponseInterface|null * @return int
*/ */
public function getBody(): string|ResponseInterface|null 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
{ {
if ($this->body instanceof ResponseInterface) {
return $this->body->getBody()->getContents();
}
return $this->body; return $this->body;
} }
/** /**
* @param ResponseInterface|null $body * @param ?string $body
*/ */
public function setBody(?ResponseInterface $body): void public function setBody(?string $body): void
{ {
$this->body = $body; $this->body = $body;
} }
+5 -9
View File
@@ -10,8 +10,6 @@ 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;
@@ -53,15 +51,13 @@ class CoroutineClient extends ClientAbstracts
if ($this->client->statusCode < 0) { if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg); throw new Exception($this->client->errMsg);
} }
$body = (new Response())->withStatus($this->client->getStatusCode()) $this->setStatusCode($this->client->getStatusCode());
->withHeaders($this->client->getHeaders()) $this->setBody($this->client->getBody());
->withBody(new Stream($this->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]);
$body = (new Response())->withStatus(-1)->withHeaders([]) $this->setStatusCode(-1);
->withBody(new Stream(jTraceEx($exception))); $this->setBody(jTraceEx($exception));
} finally {
$this->setBody($body);
} }
} }
+8 -7
View File
@@ -138,11 +138,11 @@ class Curl extends ClientAbstracts
{ {
$output = curl_exec($this->client); $output = curl_exec($this->client);
if ($output === false) { if ($output === false) {
$response = (new Response())->withStatus(400)->withBody(new Stream(curl_error($this->client))); $this->setStatusCode(404);
$this->setBody(curl_error($this->client));
} else { } else {
$response = $this->explode($output); $this->explode($output);
} }
$this->setBody($response);
} }
@@ -157,10 +157,10 @@ class Curl extends ClientAbstracts
/** /**
* @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') {
@@ -170,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);
} }
/** /**