-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequest.php
575 lines (509 loc) · 17.2 KB
/
Request.php
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
// This file is part of Minz.
// Copyright 2020-2024 Marien Fressinaud
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace Minz;
/**
* The Request class abstracts a request from a user client.
*
* It generally represents HTTP requests, but it also can represent a request
* from the CLI. Abstracting requests makes it easy to test applications based
* on Minz.
*
* The main idea of Minz is to transform Request into Response via a controller
* action. Thus, it’s essential to understand these classes.
*
* @see \Minz\Response
*
* A request is represented by a method (e.g. `GET`, `POST`), a path (e.g.
* `/foo`) and some parameters and headers. It’s one of the first object to
* initialize in an application. For instance, in `public/index.php`:
*
* ```php
* $request = \Minz\Request::initFromGlobals();
* ```
*
* Requests can also be used to handle command line (e.g. in a `cli.php` file):
*
* ```php
* $request = \Minz\Request::initFromCli($argv);
* ```
*
* With the above code, you can accept commands of this form:
*
* ```console
* $ php cli.php some command --foo=bar --spam
* ```
*
* It will generate the following request:
*
* ```php
* $request = new \Minz\Request('CLI', '/some/command', [
* 'foo' => 'bar',
* 'spam' => true,
* ]);
* ```
*
* The request is compared by the Engine to the routes declared in the Router.
* If it finds a correspondance, it executes the corresponding controller
* action and pass the request as a parameter.
*
* @see \Minz\Engine
* @see \Minz\Router
*
* You can get the parameters of a request very simply:
*
* ```php
* $foo = $request->param('foo');
* $bar = $request->param('bar', 'a default value');
* ```
*
* You also can automatically cast parameters to the desire types:
*
* ```php
* $boolean = $request->paramBoolean('boolean-param');
* $integer = $request->paramInteger('integer-param');
* $datetime = $request->paramDatetime('datetime-param');
* $array = $request->paramArray('array-param');
* $json = $request->paramJson('json-param');
* $file = $request->paramFile('file-param');
* ```
*
* Headers and cookies can be retrieved in a similar way (except that there are
* no cast-methods):
*
* ```php
* $accept_header = $request->header('HTTP_ACCEPT');
* $my_cookie = $request->cookie('my_cookie');
* ```
*
* @phpstan-type RequestMethod value-of<Request::VALID_METHODS>
*
* @phpstan-type RequestParameters array<string, mixed>
*
* @phpstan-type RequestHeaders array<string, mixed>
*/
class Request
{
public const VALID_HTTP_METHODS = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'];
public const VALID_METHODS = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'CLI'];
/** @var RequestMethod */
private string $method;
private string $path;
/** @var RequestParameters */
private array $parameters;
/** @var RequestHeaders */
private array $headers;
/**
* Create a Request reading the global variables.
*
* @throws \Minz\Errors\RequestError
* Raised if the REQUEST_METHOD variable is invalid.
*/
public static function initFromGlobals(): Request
{
$request_method = strtoupper($_SERVER['REQUEST_METHOD']);
$http_method = $request_method === 'HEAD' ? 'GET' : $request_method;
if (!in_array($http_method, self::VALID_HTTP_METHODS)) {
throw new Errors\RequestError("The HTTP method '{$http_method}' is not supported.");
}
/** @var RequestMethod */
$http_method = $http_method;
$http_uri = $_SERVER['REQUEST_URI'];
$http_parameters = array_merge(
$_GET,
$_POST,
$_FILES,
['@input' => @file_get_contents('php://input')],
);
$http_headers = array_merge($_SERVER, [
'COOKIE' => $_COOKIE,
]);
return new Request($http_method, $http_uri, $http_parameters, $http_headers);
}
/**
* Create a Request reading the CLI arguments.
*
* @param non-empty-list<string> $argv
*/
public static function initFromCli(array $argv): Request
{
// Read command line parameters to create a Request
$command = [];
$parameters = [];
// We need to skip the first argument which is the name of the script
$arguments = array_slice($argv, 1);
foreach ($arguments as $argument) {
$result = preg_match('/^--(?P<option>[\w\-]+)(=(?P<argument>.+))?$/sm', $argument, $matches);
if ($result) {
$parameters[$matches['option']] = $matches['argument'] ?? true;
} else {
$command[] = $argument;
}
}
$request_uri = implode('/', $command);
if (!$request_uri) {
$request_uri = '/help';
} elseif ($request_uri[0] !== '/') {
$request_uri = '/' . $request_uri;
}
$parameters['bin'] = $argv[0];
return new \Minz\Request('CLI', $request_uri, $parameters);
}
/**
* Create a Request
*
* @param RequestMethod $method
* The method that is executing the request. Its value must be one of
* the Request::VALID_METHODS. Valid methods are equivalent to a subset
* of HTTP verbs + the `cli` value (to handle requests from the CLI).
* `head` HTTP requests must be passed as `get` requests (they only
* differ at rendering). The method is lowercased before being compared
* to valid methods.
* For HTTP requests, its value usually comes from `$_SERVER['REQUEST_METHOD']`.
* For CLI requests, it always must be `cli`.
* @param string $uri
* The URI that is executing the request. It can be a path starting by
* a slash (/), or a full URL from which the path will be extracted. If
* `Configuration::url_options['path']` is set, its value is removed
* from the beginning of the extracted path.
* For HTTP requests, its value usually comes from `$_SERVER['REQUEST_URI']`.
* CLI must respect this format as well and build the path by itself (cf.
* example above)
* @param RequestParameters $parameters
* The parameters of the request where keys are the names of the parameters.
* The parameters can be retrieved with the `param*()` methods.
* For HTTP requests, its value usually is a merge of `$_GET`, `$_POST`
* and `$_FILE` global variables.
* CLI must build the array by itself (cf. example above)
* @param RequestHeaders $headers
* The headers of the request where keys are the names of the headers.
* Cookies must be associated to the `COOKIE` key. Headers can be
* retrieved with the `header()` method, while cookies are retrieved
* with the `cookie()` one.
* For HTTP requests, its value usually is a merge of `$_SERVER` and
* `$_COOKIE` global variables.
* CLI requests usually don’t have headers.
*
* @throws \Minz\Errors\RequestError
* Raised if the method is invalid, if uri is empty or invalid, or if
* parameters or headers aren't arrays.
*
* @see \Minz\Configuration::$url_options
* @see https://developer.mozilla.org/docs/Web/HTTP/Methods
* @see https://developer.mozilla.org/docs/Web/HTTP/Headers
* @see https://developer.mozilla.org/docs/Web/HTTP/Overview#requests
*/
public function __construct(string $method, string $uri, array $parameters = [], array $headers = [])
{
if (!$uri) {
throw new Errors\RequestError('URI cannot be empty.');
}
if ($uri[0] === '/') {
// parse_url() has issues to parse URLs starting with multiple
// leading slashes:
// - it considers legitimately "foo" is the domain in "//foo", but
// it's very unlikely that the server will pass such a domain
// without expliciting the protocol;
// - it simply fails for URLs starting with 3 slashes or more.
// For these reasons, we consider all URIs starting by a slash to
// be a path and we remove query and hash manually.
$path = $uri;
$pos_query = strpos($path, '?');
if ($pos_query !== false) {
$path = substr($path, 0, $pos_query);
}
$pos_hash = strpos($path, '#');
if ($pos_hash !== false) {
$path = substr($path, 0, $pos_hash);
}
} else {
// In other cases, the URI probably contains the protocol and
// domain, so we let parse_url to do its job.
$uri_components = parse_url($uri);
if (!$uri_components) {
throw new Errors\RequestError("{$uri} URI path cannot be parsed.");
}
if (empty($uri_components['path'])) {
$path = '/';
} else {
$path = $uri_components['path'];
}
if ($path[0] !== '/') {
throw new Errors\RequestError("{$uri} URI path must start with a slash.");
}
}
// If a path is specified in url_options, we must remove its value
// from the beginning of the request path because routes are relative
// to the url_options path.
$url_options_path = Configuration::$url_options['path'];
if ($url_options_path !== '/' && str_starts_with($path, $url_options_path)) {
$path = substr($path, strlen($url_options_path));
}
$this->method = $method;
$this->path = $path;
$this->parameters = $parameters;
$this->headers = $headers;
}
/**
* @return RequestMethod
*/
public function method(): string
{
return $this->method;
}
public function path(): string
{
return $this->path;
}
public function setParam(string $name, mixed $value): void
{
$this->parameters[$name] = $value;
}
public function hasParam(string $name): bool
{
return isset($this->parameters[$name]);
}
/**
* @template T of ?string
*
* @param T $default
*
* @return string|T
*
* @see Request::paramString
*/
public function param(string $name, ?string $default = null): mixed
{
return $this->paramString($name, $default);
}
/**
* @template T of ?string
*
* @param T $default
*
* @return string|T
*/
public function paramString(string $name, ?string $default = null): mixed
{
if (isset($this->parameters[$name])) {
$value = $this->parameters[$name];
if (
!is_bool($value) &&
!is_float($value) &&
!is_integer($value) &&
!is_string($value)
) {
return $default;
}
return strval($value);
} else {
return $default;
}
}
/**
* Return a parameter value as a boolean.
*/
public function paramBoolean(string $name, bool $default = false): bool
{
if (isset($this->parameters[$name])) {
return filter_var($this->parameters[$name], FILTER_VALIDATE_BOOLEAN);
} else {
return $default;
}
}
/**
* Return a parameter value as an integer.
*
* @template T of ?int
*
* @param T $default
*
* @return int|T
*/
public function paramInteger(string $name, ?int $default = null): ?int
{
if (isset($this->parameters[$name])) {
$value = $this->parameters[$name];
if (!is_float($value) && !is_integer($value) && !is_string($value)) {
return $default;
}
return intval($value);
} else {
return $default;
}
}
/**
* Return a parameter value as a DateTimeImmutable.
*
* @template T of ?\DateTimeImmutable
*
* @param T $default
*
* @return \DateTimeImmutable|T
*/
public function paramDatetime(
string $name,
?\DateTimeImmutable $default = null,
string $format = 'Y-m-d\\TH:i'
): ?\DateTimeImmutable {
if (isset($this->parameters[$name])) {
$value = $this->parameters[$name];
if (!is_string($value)) {
return $default;
}
$datetime = \DateTimeImmutable::createFromFormat($format, $value);
if ($datetime === false) {
return $default;
}
return $datetime;
} else {
return $default;
}
}
/**
* Return a parameter value as an array.
*
* If the parameter isn’t an array, it’s placed into one.
*
* The default value is merged with the parameter value.
*
* @param mixed[] $default
*
* @return mixed[]
*/
public function paramArray(string $name, array $default = []): array
{
if (isset($this->parameters[$name])) {
$value = $this->parameters[$name];
if (!is_array($value)) {
$value = [$value];
}
return array_merge($default, $value);
} else {
return $default;
}
}
/**
* Return a parameter value as a Json array.
*
* If the value is equal to true, false or null, it returns the value in
* an array.
*
* If the parameter cannot be parsed as Json, default value is returned.
*
* @template T of mixed[]|null
*
* @param T $default
*
* @return mixed[]|T
*/
public function paramJson(string $name, mixed $default = null): ?array
{
if (!isset($this->parameters[$name])) {
return $default;
}
$value = $this->parameters[$name];
if (!is_string($value)) {
return $default;
}
$json_value = json_decode($value, true);
if ($json_value === null && $value !== 'null') {
return $default;
}
if (!is_array($json_value)) {
$json_value = [$json_value];
}
return $json_value;
}
/**
* Return a parameter value as a \Minz\File.
*
* The parameter must be an array containing at least a `tmp_name` and an
* `error` keys, or a null value will be returned.
*
* @see https://www.php.net/manual/features.file-upload.post-method.php
*/
public function paramFile(string $name): ?\Minz\File
{
if (!isset($this->parameters[$name])) {
return null;
}
$parameter = $this->parameters[$name];
if (!is_array($parameter)) {
return null;
}
$file_info = [
'tmp_name' => $parameter['tmp_name'] ?? '',
'error' => $parameter['error'] ?? -1,
'name' => $parameter['name'] ?? '',
];
if (isset($parameter['is_uploaded_file'])) {
$file_info['is_uploaded_file'] = $parameter['is_uploaded_file'];
};
try {
return new File($file_info);
} catch (Errors\RuntimeException $e) {
return null;
}
}
public function header(string $name, mixed $default = null): mixed
{
if (isset($this->headers[$name])) {
return $this->headers[$name];
} else {
return $default;
}
}
/**
* Return the value of a cookie.
*
* Cookies must be passed during Request initialization as $headers['COOKIE'].
*/
public function cookie(string $name, mixed $default = null): mixed
{
if (
isset($this->headers['COOKIE']) &&
is_array($this->headers['COOKIE']) &&
isset($this->headers['COOKIE'][$name])
) {
return $this->headers['COOKIE'][$name];
} else {
return $default;
}
}
/**
* Return whether the given media is accepted by the client or not.
*
* @see https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
*/
public function isAccepting(string $media): bool
{
// No Accept header implies the user agent accepts any media type (cf.
// the RFC 7231)
$accept_header = $this->header('HTTP_ACCEPT', '*/*');
if (!is_string($accept_header)) {
return false;
}
$accept_medias = explode(',', $accept_header);
list($media_type, $media_subtype) = explode('/', $media);
foreach ($accept_medias as $accept_media) {
// We only want to know if the media is included in the Accept
// header, we can remove the preference weight (i.e. "q" parameter)
$semicolon_position = strpos($accept_media, ';');
if ($semicolon_position !== false) {
$accept_media = substr($accept_media, 0, $semicolon_position);
}
$accept_media = trim($accept_media);
list($accept_type, $accept_subtype) = explode('/', $accept_media);
if (
($accept_type === $media_type && $accept_subtype === '*') ||
$accept_media === '*/*' ||
$accept_media === $media
) {
return true;
}
}
return false;
}
}