Skip to content

Commit 518ca68

Browse files
committed
Add internal AbstractMessage base class (PSR-7)
1 parent 1bbd7f9 commit 518ca68

6 files changed

+393
-11
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2448,8 +2448,7 @@ constants with the `STATUS_*` prefix. For instance, the `200 OK` and
24482448
`404 Not Found` status codes can used as `Response::STATUS_OK` and
24492449
`Response::STATUS_NOT_FOUND` respectively.
24502450

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

24552454
##### html()

src/Io/AbstractMessage.php

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace React\Http\Io;
4+
5+
use Psr\Http\Message\MessageInterface;
6+
use Psr\Http\Message\StreamInterface;
7+
8+
/**
9+
* [Internal] Abstract HTTP message base class (PSR-7)
10+
*
11+
* @internal
12+
* @see MessageInterface
13+
*/
14+
abstract class AbstractMessage implements MessageInterface
15+
{
16+
/** @var array<string,string[]> */
17+
private $headers = array();
18+
19+
/** @var array<string,string> */
20+
private $headerNamesLowerCase = array();
21+
22+
/** @var string */
23+
private $protocolVersion;
24+
25+
/** @var StreamInterface */
26+
private $body;
27+
28+
/**
29+
* @param string $protocolVersion
30+
* @param array<string,string|string[]> $headers
31+
* @param StreamInterface $body
32+
*/
33+
protected function __construct($protocolVersion, array $headers, StreamInterface $body)
34+
{
35+
foreach ($headers as $name => $value) {
36+
if ($value !== array()) {
37+
if (\is_array($value)) {
38+
foreach ($value as &$one) {
39+
$one = (string) $one;
40+
}
41+
} else {
42+
$value = array((string) $value);
43+
}
44+
45+
$lower = \strtolower($name);
46+
if (isset($this->headerNamesLowerCase[$lower])) {
47+
$value = \array_merge($this->headers[$this->headerNamesLowerCase[$lower]], $value);
48+
unset($this->headers[$this->headerNamesLowerCase[$lower]]);
49+
}
50+
51+
$this->headers[$name] = $value;
52+
$this->headerNamesLowerCase[$lower] = $name;
53+
}
54+
}
55+
56+
$this->protocolVersion = (string) $protocolVersion;
57+
$this->body = $body;
58+
}
59+
60+
public function getProtocolVersion()
61+
{
62+
return $this->protocolVersion;
63+
}
64+
65+
public function withProtocolVersion($version)
66+
{
67+
if ((string) $version === $this->protocolVersion) {
68+
return $this;
69+
}
70+
71+
$message = clone $this;
72+
$message->protocolVersion = (string) $version;
73+
74+
return $message;
75+
}
76+
77+
public function getHeaders()
78+
{
79+
return $this->headers;
80+
}
81+
82+
public function hasHeader($name)
83+
{
84+
return isset($this->headerNamesLowerCase[\strtolower($name)]);
85+
}
86+
87+
public function getHeader($name)
88+
{
89+
$lower = \strtolower($name);
90+
return isset($this->headerNamesLowerCase[$lower]) ? $this->headers[$this->headerNamesLowerCase[$lower]] : array();
91+
}
92+
93+
public function getHeaderLine($name)
94+
{
95+
return \implode(', ', $this->getHeader($name));
96+
}
97+
98+
public function withHeader($name, $value)
99+
{
100+
if ($value === array()) {
101+
return $this->withoutHeader($name);
102+
} elseif (\is_array($value)) {
103+
foreach ($value as &$one) {
104+
$one = (string) $one;
105+
}
106+
} else {
107+
$value = array((string) $value);
108+
}
109+
110+
$lower = \strtolower($name);
111+
if (isset($this->headerNamesLowerCase[$lower]) && $this->headerNamesLowerCase[$lower] === (string) $name && $this->headers[$this->headerNamesLowerCase[$lower]] === $value) {
112+
return $this;
113+
}
114+
115+
$message = clone $this;
116+
if (isset($message->headerNamesLowerCase[$lower])) {
117+
unset($message->headers[$message->headerNamesLowerCase[$lower]]);
118+
}
119+
120+
$message->headers[$name] = $value;
121+
$message->headerNamesLowerCase[$lower] = $name;
122+
123+
return $message;
124+
}
125+
126+
public function withAddedHeader($name, $value)
127+
{
128+
if ($value === array()) {
129+
return $this;
130+
}
131+
132+
return $this->withHeader($name, \array_merge($this->getHeader($name), \is_array($value) ? $value : array($value)));
133+
}
134+
135+
public function withoutHeader($name)
136+
{
137+
$lower = \strtolower($name);
138+
if (!isset($this->headerNamesLowerCase[$lower])) {
139+
return $this;
140+
}
141+
142+
$message = clone $this;
143+
unset($message->headers[$message->headerNamesLowerCase[$lower]], $message->headerNamesLowerCase[$lower]);
144+
145+
return $message;
146+
}
147+
148+
public function getBody()
149+
{
150+
return $this->body;
151+
}
152+
153+
public function withBody(StreamInterface $body)
154+
{
155+
if ($body === $this->body) {
156+
return $this;
157+
}
158+
159+
$message = clone $this;
160+
$message->body = $body;
161+
162+
return $message;
163+
}
164+
}

src/Message/Response.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use Fig\Http\Message\StatusCodeInterface;
66
use Psr\Http\Message\ResponseInterface;
77
use Psr\Http\Message\StreamInterface;
8+
use React\Http\Io\AbstractMessage;
89
use React\Http\Io\BufferedBody;
910
use React\Http\Io\HttpBodyStream;
1011
use React\Stream\ReadableStreamInterface;
11-
use RingCentral\Psr7\MessageTrait;
1212

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

319-
$this->protocol = (string) $version;
320-
$this->setHeaders($headers);
321-
$this->stream = $body;
318+
parent::__construct($version, $headers, $body);
322319

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

0 commit comments

Comments
 (0)