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 Kiri\Context;
use Kiri\Core\Help;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Swoole\Coroutine\System;
@@ -48,10 +47,16 @@ abstract class ClientAbstracts implements IClient
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;
@@ -59,25 +64,71 @@ abstract class ClientAbstracts implements IClient
private int $connect_timeout = 1;
/**
* @var resource|\Swoole\Coroutine\Http\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;
}
/**
* @param ResponseInterface|null $body
* @param ?string $body
*/
public function setBody(?ResponseInterface $body): void
public function setBody(?string $body): void
{
$this->body = $body;
}
+5 -9
View File
@@ -10,8 +10,6 @@ declare(strict_types=1);
namespace Http\Client;
use Exception;
use Http\Message\Response;
use Http\Message\Stream;
use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Logger;
use Kiri\Kiri;
@@ -53,15 +51,13 @@ class CoroutineClient extends ClientAbstracts
if ($this->client->statusCode < 0) {
throw new Exception($this->client->errMsg);
}
$body = (new Response())->withStatus($this->client->getStatusCode())
->withHeaders($this->client->getHeaders())
->withBody(new Stream($this->client->getBody()));
$this->setStatusCode($this->client->getStatusCode());
$this->setBody($this->client->getBody());
$this->setResponseHeader($this->client->headers);
} catch (\Throwable $exception) {
Kiri::getDi()->get(Logger::class)->error('rpc', [$exception]);
$body = (new Response())->withStatus(-1)->withHeaders([])
->withBody(new Stream(jTraceEx($exception)));
} finally {
$this->setBody($body);
$this->setStatusCode(-1);
$this->setBody(jTraceEx($exception));
}
}
+8 -7
View File
@@ -138,11 +138,11 @@ class Curl extends ClientAbstracts
{
$output = curl_exec($this->client);
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 {
$response = $this->explode($output);
$this->explode($output);
}
$this->setBody($response);
}
@@ -157,10 +157,10 @@ class Curl extends ClientAbstracts
/**
* @param $output
* @return ResponseInterface
* @return void
* @throws Exception
*/
private function explode($output): ResponseInterface
private function explode($output): void
{
[$header, $body] = explode("\r\n\r\n", $output, 2);
if ($header == 'HTTP/1.1 100 Continue') {
@@ -170,8 +170,9 @@ class Curl extends ClientAbstracts
$header = explode("\r\n", $header);
$status = explode(' ', array_shift($header));
return (new Response())->withStatus(intval($status[1]))->withHeaders($this->headerFormat($header))
->withBody(new Stream($body));
$this->setStatusCode(intval($status[1]));
$this->setBody($body);
$this->setResponseHeader($header);
}
/**