|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace React\Http\Message; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use Psr\Http\Message\StreamInterface; |
|
7
|
|
|
use Psr\Http\Message\UriInterface; |
|
8
|
|
|
use React\Http\Io\BufferedBody; |
|
9
|
|
|
use React\Http\Io\HttpBodyStream; |
|
10
|
|
|
use React\Stream\ReadableStreamInterface; |
|
11
|
|
|
use RingCentral\Psr7\Request; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Respresents an incoming server request message. |
|
15
|
|
|
* |
|
16
|
|
|
* This class implements the |
|
17
|
|
|
* [PSR-7 `ServerRequestInterface`](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface) |
|
18
|
|
|
* which extends the |
|
19
|
|
|
* [PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface) |
|
20
|
|
|
* which in turn extends the |
|
21
|
|
|
* [PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface). |
|
22
|
|
|
* |
|
23
|
|
|
* This is mostly used internally to represent each incoming request message. |
|
24
|
|
|
* Likewise, you can also use this class in test cases to test how your web |
|
25
|
|
|
* application reacts to certain HTTP requests. |
|
26
|
|
|
* |
|
27
|
|
|
* > Internally, this implementation builds on top of an existing outgoing |
|
28
|
|
|
* request message and only adds required server methods. This base class is |
|
29
|
|
|
* considered an implementation detail that may change in the future. |
|
30
|
|
|
* |
|
31
|
|
|
* @see ServerRequestInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
final class ServerRequest extends Request implements ServerRequestInterface |
|
34
|
|
|
{ |
|
35
|
|
|
private $attributes = array(); |
|
36
|
|
|
|
|
37
|
|
|
private $serverParams; |
|
38
|
|
|
private $fileParams = array(); |
|
39
|
|
|
private $cookies = array(); |
|
40
|
|
|
private $queryParams = array(); |
|
41
|
|
|
private $parsedBody; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $method HTTP method for the request. |
|
45
|
|
|
* @param string|UriInterface $url URL for the request. |
|
46
|
|
|
* @param array<string,string|string[]> $headers Headers for the message. |
|
47
|
|
|
* @param string|ReadableStreamInterface|StreamInterface $body Message body. |
|
48
|
|
|
* @param string $version HTTP protocol version. |
|
49
|
|
|
* @param array<string,string> $serverParams server-side parameters |
|
50
|
|
|
* @throws \InvalidArgumentException for an invalid URL or body |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
$method, |
|
54
|
|
|
$url, |
|
55
|
|
|
array $headers = array(), |
|
56
|
|
|
$body = '', |
|
57
|
|
|
$version = '1.1', |
|
58
|
|
|
$serverParams = array() |
|
59
|
|
|
) { |
|
60
|
|
|
$stream = null; |
|
61
|
|
|
if (\is_string($body)) { |
|
62
|
|
|
$body = new BufferedBody($body); |
|
63
|
|
|
} elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { |
|
64
|
|
|
$stream = $body; |
|
65
|
|
|
$body = null; |
|
66
|
|
|
} elseif (!$body instanceof StreamInterface) { |
|
|
|
|
|
|
67
|
|
|
throw new \InvalidArgumentException('Invalid server request body given'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->serverParams = $serverParams; |
|
71
|
|
|
parent::__construct($method, $url, $headers, $body, $version); |
|
72
|
|
|
|
|
73
|
|
|
if ($stream !== null) { |
|
74
|
|
|
$size = (int) $this->getHeaderLine('Content-Length'); |
|
75
|
|
|
if (\strtolower($this->getHeaderLine('Transfer-Encoding')) === 'chunked') { |
|
76
|
|
|
$size = null; |
|
77
|
|
|
} |
|
78
|
|
|
$this->stream = new HttpBodyStream($stream, $size); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$query = $this->getUri()->getQuery(); |
|
82
|
|
|
if ($query !== '') { |
|
83
|
|
|
\parse_str($query, $this->queryParams); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Multiple cookie headers are not allowed according |
|
87
|
|
|
// to https://tools.ietf.org/html/rfc6265#section-5.4 |
|
88
|
|
|
$cookieHeaders = $this->getHeader("Cookie"); |
|
89
|
|
|
|
|
90
|
|
|
if (count($cookieHeaders) === 1) { |
|
91
|
|
|
$this->cookies = $this->parseCookie($cookieHeaders[0]); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getServerParams() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->serverParams; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getCookieParams() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->cookies; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function withCookieParams(array $cookies) |
|
106
|
|
|
{ |
|
107
|
|
|
$new = clone $this; |
|
108
|
|
|
$new->cookies = $cookies; |
|
109
|
|
|
return $new; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function getQueryParams() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->queryParams; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function withQueryParams(array $query) |
|
118
|
|
|
{ |
|
119
|
|
|
$new = clone $this; |
|
120
|
|
|
$new->queryParams = $query; |
|
121
|
|
|
return $new; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function getUploadedFiles() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->fileParams; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function withUploadedFiles(array $uploadedFiles) |
|
130
|
|
|
{ |
|
131
|
|
|
$new = clone $this; |
|
132
|
|
|
$new->fileParams = $uploadedFiles; |
|
133
|
|
|
return $new; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getParsedBody() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->parsedBody; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function withParsedBody($data) |
|
142
|
|
|
{ |
|
143
|
|
|
$new = clone $this; |
|
144
|
|
|
$new->parsedBody = $data; |
|
145
|
|
|
return $new; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function getAttributes() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->attributes; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function getAttribute($name, $default = null) |
|
154
|
|
|
{ |
|
155
|
|
|
if (!\array_key_exists($name, $this->attributes)) { |
|
156
|
|
|
return $default; |
|
157
|
|
|
} |
|
158
|
|
|
return $this->attributes[$name]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function withAttribute($name, $value) |
|
162
|
|
|
{ |
|
163
|
|
|
$new = clone $this; |
|
164
|
|
|
$new->attributes[$name] = $value; |
|
165
|
|
|
return $new; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function withoutAttribute($name) |
|
169
|
|
|
{ |
|
170
|
|
|
$new = clone $this; |
|
171
|
|
|
unset($new->attributes[$name]); |
|
172
|
|
|
return $new; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @param string $cookie |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
private function parseCookie($cookie) |
|
180
|
|
|
{ |
|
181
|
|
|
$cookieArray = \explode(';', $cookie); |
|
182
|
|
|
$result = array(); |
|
183
|
|
|
|
|
184
|
|
|
foreach ($cookieArray as $pair) { |
|
185
|
|
|
$pair = \trim($pair); |
|
186
|
|
|
$nameValuePair = \explode('=', $pair, 2); |
|
187
|
|
|
|
|
188
|
|
|
if (\count($nameValuePair) === 2) { |
|
189
|
|
|
$key = \urldecode($nameValuePair[0]); |
|
190
|
|
|
$value = \urldecode($nameValuePair[1]); |
|
191
|
|
|
$result[$key] = $value; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $result; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|