Skip to content

Commit 3260343

Browse files
authored
Merge pull request #549 from WyriHaximus-labs/1.x-psr-7-v2
[1.x] Support PSR-7 v2 along side v1
2 parents e5b8eb2 + 13ba70f commit 3260343

25 files changed

Lines changed: 2413 additions & 16 deletions

‎composer.json‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
3131
"fig/http-message-util": "^1.1",
32-
"psr/http-message": "^1.0",
32+
"psr/http-message": "^2.0 || ^1.0",
3333
"react/event-loop": "^1.2",
3434
"react/promise": "^3.2 || ^2.3 || ^1.2.1",
3535
"react/socket": "^1.16",
3636
"react/stream": "^1.4"
3737
},
3838
"require-dev": {
39-
"clue/http-proxy-react": "^1.8",
39+
"clue/http-proxy-react": "^1.10",
4040
"clue/reactphp-ssh-proxy": "^1.4",
4141
"clue/socks-react": "^1.4",
4242
"phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36",
@@ -47,7 +47,10 @@
4747
"autoload": {
4848
"psr-4": {
4949
"React\\Http\\": "src/"
50-
}
50+
},
51+
"exclude-from-classmap": [
52+
"src/**.v2.php"
53+
]
5154
},
5255
"autoload-dev": {
5356
"psr-4": {

‎src/Io/AbstractMessage.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
use Psr\Http\Message\MessageInterface;
66
use Psr\Http\Message\StreamInterface;
77

8+
9+
if (\PHP_VERSION_ID >= 70000 && \call_user_func(array(new \ReflectionMethod('\Psr\Http\Message\MessageInterface', 'getProtocolVersion'), 'hasReturnType'))) {
10+
// load class for psr/http-message v2 using native types like `getProtocolVersion(): string`
11+
require __DIR__ . '/AbstractMessage.v2.php';
12+
} else {
13+
// fall back to class without native types
14+
815
/**
9-
* [Internal] Abstract HTTP message base class (PSR-7)
16+
* [Internal] Abstract HTTP message base class (PSR-7 v1)
1017
*
1118
* @internal
1219
* @see MessageInterface
20+
* @see AbstractMessage.v2.php (PSR-7 v2)
1321
*/
1422
abstract class AbstractMessage implements MessageInterface
1523
{
@@ -170,3 +178,4 @@ public function withBody(StreamInterface $body)
170178
return $message;
171179
}
172180
}
181+
}

‎src/Io/AbstractMessage.v2.php‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 v2)
10+
*
11+
* @internal
12+
* @see MessageInterface
13+
* @see AbstractMessage.php (PSR-7 v1)
14+
*/
15+
abstract class AbstractMessage implements MessageInterface
16+
{
17+
/**
18+
* [Internal] Regex used to match all request header fields into an array, thanks to @kelunik for checking the HTTP specs and coming up with this regex
19+
*
20+
* @internal
21+
* @var string
22+
*/
23+
const REGEX_HEADERS = '/^([^()<>@,;:\\\"\/\[\]?={}\x00-\x20\x7F]++):[\x20\x09]*+((?:[\x20\x09]*+[\x21-\x7E\x80-\xFF]++)*+)[\x20\x09]*+[\r]?+\n/m';
24+
25+
/** @var array<string,string[]> */
26+
private $headers = array();
27+
28+
/** @var array<string,string> */
29+
private $headerNamesLowerCase = array();
30+
31+
/** @var string */
32+
private $protocolVersion;
33+
34+
/** @var StreamInterface */
35+
private $body;
36+
37+
/**
38+
* @param string $protocolVersion
39+
* @param array<string,string|string[]> $headers
40+
* @param StreamInterface $body
41+
*/
42+
protected function __construct($protocolVersion, array $headers, StreamInterface $body)
43+
{
44+
foreach ($headers as $name => $value) {
45+
if ($value !== array()) {
46+
if (\is_array($value)) {
47+
foreach ($value as &$one) {
48+
$one = (string) $one;
49+
}
50+
} else {
51+
$value = array((string) $value);
52+
}
53+
54+
$lower = \strtolower($name);
55+
if (isset($this->headerNamesLowerCase[$lower])) {
56+
$value = \array_merge($this->headers[$this->headerNamesLowerCase[$lower]], $value);
57+
unset($this->headers[$this->headerNamesLowerCase[$lower]]);
58+
}
59+
60+
$this->headers[$name] = $value;
61+
$this->headerNamesLowerCase[$lower] = $name;
62+
}
63+
}
64+
65+
$this->protocolVersion = (string) $protocolVersion;
66+
$this->body = $body;
67+
}
68+
69+
public function getProtocolVersion(): string
70+
{
71+
return $this->protocolVersion;
72+
}
73+
74+
public function withProtocolVersion(string $version): MessageInterface
75+
{
76+
if ((string) $version === $this->protocolVersion) {
77+
return $this;
78+
}
79+
80+
$message = clone $this;
81+
$message->protocolVersion = (string) $version;
82+
83+
return $message;
84+
}
85+
86+
public function getHeaders(): array
87+
{
88+
return $this->headers;
89+
}
90+
91+
public function hasHeader(string $name): bool
92+
{
93+
return isset($this->headerNamesLowerCase[\strtolower($name)]);
94+
}
95+
96+
public function getHeader(string $name): array
97+
{
98+
$lower = \strtolower($name);
99+
return isset($this->headerNamesLowerCase[$lower]) ? $this->headers[$this->headerNamesLowerCase[$lower]] : array();
100+
}
101+
102+
public function getHeaderLine(string $name): string
103+
{
104+
return \implode(', ', $this->getHeader($name));
105+
}
106+
107+
public function withHeader(string $name, $value): MessageInterface
108+
{
109+
if ($value === array()) {
110+
return $this->withoutHeader($name);
111+
} elseif (\is_array($value)) {
112+
foreach ($value as &$one) {
113+
$one = (string) $one;
114+
}
115+
} else {
116+
$value = array((string) $value);
117+
}
118+
119+
$lower = \strtolower($name);
120+
if (isset($this->headerNamesLowerCase[$lower]) && $this->headerNamesLowerCase[$lower] === (string) $name && $this->headers[$this->headerNamesLowerCase[$lower]] === $value) {
121+
return $this;
122+
}
123+
124+
$message = clone $this;
125+
if (isset($message->headerNamesLowerCase[$lower])) {
126+
unset($message->headers[$message->headerNamesLowerCase[$lower]]);
127+
}
128+
129+
$message->headers[$name] = $value;
130+
$message->headerNamesLowerCase[$lower] = $name;
131+
132+
return $message;
133+
}
134+
135+
public function withAddedHeader(string $name, $value): MessageInterface
136+
{
137+
if ($value === array()) {
138+
return $this;
139+
}
140+
141+
return $this->withHeader($name, \array_merge($this->getHeader($name), \is_array($value) ? $value : array($value)));
142+
}
143+
144+
public function withoutHeader(string $name): MessageInterface
145+
{
146+
$lower = \strtolower($name);
147+
if (!isset($this->headerNamesLowerCase[$lower])) {
148+
return $this;
149+
}
150+
151+
$message = clone $this;
152+
unset($message->headers[$message->headerNamesLowerCase[$lower]], $message->headerNamesLowerCase[$lower]);
153+
154+
return $message;
155+
}
156+
157+
public function getBody(): StreamInterface
158+
{
159+
return $this->body;
160+
}
161+
162+
public function withBody(StreamInterface $body): MessageInterface
163+
{
164+
if ($body === $this->body) {
165+
return $this;
166+
}
167+
168+
$message = clone $this;
169+
$message->body = $body;
170+
171+
return $message;
172+
}
173+
}

‎src/Io/AbstractRequest.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,18 @@
77
use Psr\Http\Message\UriInterface;
88
use React\Http\Message\Uri;
99

10+
if (\PHP_VERSION_ID >= 70000 && \call_user_func(array(new \ReflectionMethod('\Psr\Http\Message\RequestInterface', 'getRequestTarget'), 'hasReturnType'))) {
11+
// load class for psr/http-message v2 using native types like `getRequestTarget(): string`
12+
require __DIR__ . '/AbstractRequest.v2.php';
13+
} else {
14+
// fall back to class without native types
15+
1016
/**
11-
* [Internal] Abstract HTTP request base class (PSR-7)
17+
* [Internal] Abstract HTTP request base class (PSR-7 v1)
1218
*
1319
* @internal
1420
* @see RequestInterface
21+
* @see AbstractRequest.v2.php (PSR-7 v2)
1522
*/
1623
abstract class AbstractRequest extends AbstractMessage implements RequestInterface
1724
{
@@ -154,3 +161,4 @@ public function withUri(UriInterface $uri, $preserveHost = false)
154161
return $request;
155162
}
156163
}
164+
}

0 commit comments

Comments
 (0)