diff --git a/src/ClientAbstracts.php b/src/ClientAbstracts.php index 962cb1a..a41a7b7 100644 --- a/src/ClientAbstracts.php +++ b/src/ClientAbstracts.php @@ -48,10 +48,14 @@ abstract class ClientAbstracts implements IClient private int $port = 80; + + private int $statusCode = 200; + + /** - * @var ResponseInterface|null + * @var string|null */ - protected ?ResponseInterface $body; + protected ?string $body; 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; } diff --git a/src/CoroutineClient.php b/src/CoroutineClient.php index 32b825b..6743af8 100644 --- a/src/CoroutineClient.php +++ b/src/CoroutineClient.php @@ -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,12 @@ 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()); } 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)); } } diff --git a/src/Curl.php b/src/Curl.php index 67c9993..e6816fa 100644 --- a/src/Curl.php +++ b/src/Curl.php @@ -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,8 @@ 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); } /**