This commit is contained in:
2021-11-29 11:28:01 +08:00
parent d075dd73a4
commit 336237d338
3 changed files with 40 additions and 23 deletions
+29 -7
View File
@@ -48,10 +48,14 @@ abstract class ClientAbstracts implements IClient
private int $port = 80; private int $port = 80;
private int $statusCode = 200;
/** /**
* @var ResponseInterface|null * @var string|null
*/ */
protected ?ResponseInterface $body; protected ?string $body;
private ?StreamInterface $_data = null; private ?StreamInterface $_data = null;
@@ -63,18 +67,36 @@ abstract class ClientAbstracts implements IClient
/** /**
* @return string|null * @return int
*/ */
public function getBody(): string|null public function getStatusCode(): int
{ {
return $this->body->getBody()->getContents(); return $this->statusCode;
} }
/** /**
* @param ResponseInterface|null $body * @param int $statusCode
*/ */
public function setBody(?ResponseInterface $body): void 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; $this->body = $body;
} }
+4 -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,12 @@ 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()));
} 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);
} }
} }
+7 -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,8 @@ 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);
} }
/** /**