Skip to content

Commit

Permalink
Add internal AbstractMessage base class (PSR-7)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 13, 2024
1 parent 1bbd7f9 commit 518ca68
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 11 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2448,8 +2448,7 @@ constants with the `STATUS_*` prefix. For instance, the `200 OK` and
`404 Not Found` status codes can used as `Response::STATUS_OK` and
`Response::STATUS_NOT_FOUND` respectively.

> Internally, this implementation builds on top of an existing incoming
response message and only adds required streaming support. This base class is
> Internally, this implementation builds on top of a base class which is
considered an implementation detail that may change in the future.

##### html()
Expand Down
164 changes: 164 additions & 0 deletions src/Io/AbstractMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace React\Http\Io;

use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\StreamInterface;

/**
* [Internal] Abstract HTTP message base class (PSR-7)
*
* @internal
* @see MessageInterface
*/
abstract class AbstractMessage implements MessageInterface
{
/** @var array<string,string[]> */
private $headers = array();

/** @var array<string,string> */
private $headerNamesLowerCase = array();

/** @var string */
private $protocolVersion;

/** @var StreamInterface */
private $body;

/**
* @param string $protocolVersion
* @param array<string,string|string[]> $headers
* @param StreamInterface $body
*/
protected function __construct($protocolVersion, array $headers, StreamInterface $body)
{
foreach ($headers as $name => $value) {
if ($value !== array()) {
if (\is_array($value)) {
foreach ($value as &$one) {
$one = (string) $one;
}
} else {
$value = array((string) $value);
}

$lower = \strtolower($name);
if (isset($this->headerNamesLowerCase[$lower])) {
$value = \array_merge($this->headers[$this->headerNamesLowerCase[$lower]], $value);
unset($this->headers[$this->headerNamesLowerCase[$lower]]);
}

$this->headers[$name] = $value;
$this->headerNamesLowerCase[$lower] = $name;
}
}

$this->protocolVersion = (string) $protocolVersion;
$this->body = $body;
}

public function getProtocolVersion()
{
return $this->protocolVersion;
}

public function withProtocolVersion($version)
{
if ((string) $version === $this->protocolVersion) {
return $this;
}

$message = clone $this;
$message->protocolVersion = (string) $version;

return $message;
}

public function getHeaders()
{
return $this->headers;
}

public function hasHeader($name)
{
return isset($this->headerNamesLowerCase[\strtolower($name)]);
}

public function getHeader($name)
{
$lower = \strtolower($name);
return isset($this->headerNamesLowerCase[$lower]) ? $this->headers[$this->headerNamesLowerCase[$lower]] : array();
}

public function getHeaderLine($name)
{
return \implode(', ', $this->getHeader($name));
}

public function withHeader($name, $value)
{
if ($value === array()) {
return $this->withoutHeader($name);
} elseif (\is_array($value)) {
foreach ($value as &$one) {
$one = (string) $one;
}
} else {
$value = array((string) $value);
}

$lower = \strtolower($name);
if (isset($this->headerNamesLowerCase[$lower]) && $this->headerNamesLowerCase[$lower] === (string) $name && $this->headers[$this->headerNamesLowerCase[$lower]] === $value) {
return $this;
}

$message = clone $this;
if (isset($message->headerNamesLowerCase[$lower])) {
unset($message->headers[$message->headerNamesLowerCase[$lower]]);
}

$message->headers[$name] = $value;
$message->headerNamesLowerCase[$lower] = $name;

return $message;
}

public function withAddedHeader($name, $value)
{
if ($value === array()) {
return $this;
}

return $this->withHeader($name, \array_merge($this->getHeader($name), \is_array($value) ? $value : array($value)));
}

public function withoutHeader($name)
{
$lower = \strtolower($name);
if (!isset($this->headerNamesLowerCase[$lower])) {
return $this;
}

$message = clone $this;
unset($message->headers[$message->headerNamesLowerCase[$lower]], $message->headerNamesLowerCase[$lower]);

return $message;
}

public function getBody()
{
return $this->body;
}

public function withBody(StreamInterface $body)
{
if ($body === $this->body) {
return $this;
}

$message = clone $this;
$message->body = $body;

return $message;
}
}
11 changes: 4 additions & 7 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use React\Http\Io\AbstractMessage;
use React\Http\Io\BufferedBody;
use React\Http\Io\HttpBodyStream;
use React\Stream\ReadableStreamInterface;
use RingCentral\Psr7\MessageTrait;

/**
* Represents an outgoing server response message.
Expand All @@ -35,13 +35,12 @@
* `404 Not Found` status codes can used as `Response::STATUS_OK` and
* `Response::STATUS_NOT_FOUND` respectively.
*
* > Internally, this implementation builds on top of an existing incoming
* response message and only adds required streaming support. This base class is
* > Internally, this implementation builds on top a base class which is
* considered an implementation detail that may change in the future.
*
* @see \Psr\Http\Message\ResponseInterface
*/
final class Response extends MessageTrait implements ResponseInterface, StatusCodeInterface
final class Response extends AbstractMessage implements ResponseInterface, StatusCodeInterface
{
/**
* Create an HTML response
Expand Down Expand Up @@ -316,9 +315,7 @@ public function __construct(
throw new \InvalidArgumentException('Invalid response body given');
}

$this->protocol = (string) $version;
$this->setHeaders($headers);
$this->stream = $body;
parent::__construct($version, $headers, $body);

$this->statusCode = (int) $status;
$this->reasonPhrase = ($reason !== '' && $reason !== null) ? (string) $reason : self::getReasonPhraseForStatusCode($status);
Expand Down
Loading

0 comments on commit 518ca68

Please sign in to comment.