-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerRequest.php
More file actions
327 lines (301 loc) · 9.09 KB
/
Copy pathServerRequest.php
File metadata and controls
327 lines (301 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* This file is part of HttpMessage
*
* @package bdk/http-message
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2024 Brad Kent
* @version v1.0
*/
namespace bdk\HttpMessage;
use bdk\HttpMessage\Request;
use bdk\HttpMessage\Utility\ParseStr;
use bdk\HttpMessage\Utility\ServerRequest as ServerRequestUtil;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
/**
* Http ServerRequest
*
* @psalm-consistent-constructor
*/
class ServerRequest extends Request implements ServerRequestInterface
{
/** @var array */
private $attributes = array();
/** @var array $_COOKIE */
private $cookie = array();
/** @var array */
private $files = array();
/** @var array $_GET */
private $get = array();
/** @var null|array|object $_POST */
private $post = null;
/** @var array $_SERVER */
private $server = array();
/**
* Constructor
*
* @param string $method The HTTP method associated with the request.
* @param UriInterface|string $uri The URI associated with the request.
* @param array $serverParams An array of Server API (SAPI) parameters with
* which to seed the generated request instance. (and headers)
*/
public function __construct($method = 'GET', $uri = '', array $serverParams = array())
{
parent::__construct($method, $uri);
$headers = $this->getHeadersViaServer($serverParams);
$query = $this->getUri()->getQuery();
$this->get = $query !== ''
? ParseStr::parse($query)
: array();
$this->server = \array_merge(array(
'REQUEST_METHOD' => $method,
), $serverParams);
$this->protocolVersion = isset($serverParams['SERVER_PROTOCOL'])
? \str_replace('HTTP/', '', (string) $serverParams['SERVER_PROTOCOL'])
: '1.1';
$this->setHeaders($headers);
}
/**
* Instantiate self from superglobals
*
* @param array $parseStrOpts Parse options (default: {convDot:false, convSpace:false})
*
* @return self
*/
public static function fromGlobals($parseStrOpts = array())
{
return ServerRequestUtil::fromGlobals($parseStrOpts);
}
/**
* Get $_SERVER values
*
* @return array
*/
public function getServerParams(): array
{
return $this->server;
}
/**
* Get Cookie values
*
* @return array
*/
public function getCookieParams(): array
{
return $this->cookie;
}
/**
* Return an instance with the specified cookies.
*
* @param array $cookies $_COOKIE
*
* @return static
*/
public function withCookieParams(array $cookies): static
{
$this->assertCookieParams($cookies);
$new = clone $this;
$new->cookie = $cookies;
return $new;
}
/**
* Get $_GET data
*
* @return array
*/
public function getQueryParams(): array
{
return $this->get;
}
/**
* Return an instance with the specified query string arguments.
*
* @param array $query $_GET params
*
* @return static
*/
public function withQueryParams(array $query): static
{
$this->assertQueryParams($query);
$new = clone $this;
$new->get = $query;
return $new;
}
/**
* Retrieve normalized file upload data.
*
* This method returns upload metadata in a normalized tree, with each leaf
* an instance of Psr\Http\Message\UploadedFileInterface.
*
* @return array An array tree of UploadedFileInterface instances (or an empty array)
*/
public function getUploadedFiles(): array
{
return $this->files;
}
/**
* Create a new instance with the specified uploaded files.
*
* @param array $uploadedFiles An array tree of UploadedFileInterface instances.
*
* @return static
* @throws InvalidArgumentException if an invalid structure is provided.
*/
public function withUploadedFiles(array $uploadedFiles): static
{
$this->assertUploadedFiles($uploadedFiles);
$new = clone $this;
$new->files = $uploadedFiles;
return $new;
}
/**
* Get $_POST data
*
* @return null|array|object
*/
public function getParsedBody()
{
return $this->post;
}
/**
* Return an instance with the specified body parameters.
*
* @param null|array|object $data The deserialized body data ($_POST).
* This will typically be in an array or object
*
* @return static
*/
public function withParsedBody($data): static
{
$this->assertParsedBody($data);
$new = clone $this;
$new->post = $data;
return $new;
}
/**
* Retrieve attributes derived from the request.
*
* The request "attributes" may be used to allow injection of any
* parameters derived from the request: e.g., the results of path
* match operations; the results of decrypting cookies; the results of
* deserializing non-form-encoded message bodies; etc. Attributes
* will be application and request specific, and CAN be mutable.
*
* @return mixed[] Attributes derived from the request.
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* Retrieve a single derived request attribute.
*
* @param string $name The attribute name.
* @param mixed $default Default value to return if the attribute does not exist.
*
* @return mixed
*/
public function getAttribute(string $name, $default = null)
{
if (\array_key_exists($name, $this->attributes) === false) {
return $default;
}
return $this->attributes[$name];
}
/**
* Return an instance with the specified derived request attribute.
*
* @param string $name attribute name
* @param mixed $value value
*
* @return static
*/
public function withAttribute(string $name, $value): static
{
$this->assertAttributeName($name);
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
/**
* Return an instance that removes the specified derived request attribute.
*
* @param string $name attribute name
*
* @return static
*/
public function withoutAttribute(string $name): static
{
if ($this->assertAttributeName($name, false) === false) {
return $this;
}
if (\array_key_exists($name, $this->attributes) === false) {
return $this;
}
$new = clone $this;
unset($new->attributes[$name]);
return $new;
}
/**
* Get all HTTP header key/values as an associative array for the current request.
*
* See also the php function `getallheaders`
*
* @param array $serverParams $_SERVER
*
* @return array<string,string> The HTTP header key/value pairs.
*/
protected function getHeadersViaServer(array $serverParams)
{
$headers = array();
$keysSansHttp = array(
'CONTENT_LENGTH' => 'Content-Length',
'CONTENT_MD5' => 'Content-Md5',
'CONTENT_TYPE' => 'Content-Type',
);
$auth = $this->getAuthorizationHeader($serverParams);
if (\strlen($auth)) {
// set default... can be overwritten by HTTP_AUTHORIZATION
$headers['Authorization'] = $auth;
}
/** @var mixed $value */
foreach ($serverParams as $key => $value) {
$key = (string) $key;
if (isset($keysSansHttp[$key])) {
$key = $keysSansHttp[$key];
$headers[$key] = (string) $value;
} elseif (\substr($key, 0, 5) === 'HTTP_') {
$key = \substr($key, 5);
$key = \strtolower($key);
$key = \str_replace(' ', '-', \ucwords(\str_replace('_', ' ', $key)));
$headers[$key] = (string) $value;
}
}
return $headers;
}
/**
* Build Authorization header value from $_SERVER values
*
* @param array $serverParams $_SERVER vals
*
* @return string (empty string if no auth)
*/
private function getAuthorizationHeader(array $serverParams)
{
if (isset($serverParams['REDIRECT_HTTP_AUTHORIZATION'])) {
return (string) $serverParams['REDIRECT_HTTP_AUTHORIZATION'];
}
if (isset($serverParams['PHP_AUTH_USER'])) {
$user = (string) $serverParams['PHP_AUTH_USER'];
$pass = isset($serverParams['PHP_AUTH_PW']) ? (string) $serverParams['PHP_AUTH_PW'] : '';
return 'Basic ' . \base64_encode($user . ':' . $pass);
}
if (isset($serverParams['PHP_AUTH_DIGEST'])) {
return (string) $serverParams['PHP_AUTH_DIGEST'];
}
return '';
}
}