-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathServerRequest.php
More file actions
783 lines (680 loc) · 19.4 KB
/
ServerRequest.php
File metadata and controls
783 lines (680 loc) · 19.4 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
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Http/blob/master/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Http;
use Closure;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use RuntimeException;
use function array_merge;
use function count;
use function explode;
use function is_array;
use function is_null;
use function is_object;
use function json_decode;
use function libxml_clear_errors;
use function libxml_disable_entity_loader;
use function libxml_use_internal_errors;
use function parse_str;
use function preg_split;
use function property_exists;
use function simplexml_load_string;
use function strtolower;
use const LIBXML_VERSION;
class ServerRequest implements ServerRequestInterface
{
protected ServerRequestInterface $serverRequest;
/** @var array<string, callable|null> */
protected array $bodyParsers;
/**
* @param ServerRequestInterface $serverRequest
*/
final public function __construct(ServerRequestInterface $serverRequest)
{
$this->serverRequest = $serverRequest;
$this->registerMediaTypeParser('application/json', function (string $input) {
$result = json_decode($input, true);
if (!is_array($result)) {
return null;
}
return $result;
});
$xmlParserCallable = function (string $input) {
$backup = self::disableXmlEntityLoader(true);
$backup_errors = libxml_use_internal_errors(true);
$result = simplexml_load_string($input);
self::disableXmlEntityLoader($backup);
libxml_clear_errors();
libxml_use_internal_errors($backup_errors);
if ($result === false) {
return null;
}
return $result;
};
$this->registerMediaTypeParser('application/xml', $xmlParserCallable);
$this->registerMediaTypeParser('text/xml', $xmlParserCallable);
$this->registerMediaTypeParser('application/x-www-form-urlencoded', function (string $input) {
parse_str($input, $data);
return $data;
});
}
/**
* Disable magic setter to ensure immutability
* @param mixed $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
}
/**
* {@inheritdoc}
*/
public function getAttribute($name, $default = null)
{
return $this->serverRequest->getAttribute($name, $default);
}
/**
* {@inheritdoc}
*/
public function getAttributes(): array
{
return $this->serverRequest->getAttributes();
}
/**
* {@inheritdoc}
*/
public function getBody(): StreamInterface
{
return $this->serverRequest->getBody();
}
/**
* {@inheritdoc}
*/
public function getCookieParams(): array
{
return $this->serverRequest->getCookieParams();
}
/**
* {@inheritdoc}
*/
public function getHeader($name): array
{
return $this->serverRequest->getHeader($name);
}
/**
* {@inheritdoc}
*/
public function getHeaderLine($name): string
{
return $this->serverRequest->getHeaderLine($name);
}
/**
* {@inheritdoc}
*/
public function getHeaders(): array
{
return $this->serverRequest->getHeaders();
}
/**
* {@inheritdoc}
*/
public function getMethod(): string
{
return $this->serverRequest->getMethod();
}
/**
* {@inheritdoc}
*/
public function getParsedBody()
{
$parsedBody = $this->serverRequest->getParsedBody();
if (!empty($parsedBody)) {
return $parsedBody;
}
$mediaType = $this->getMediaType();
if ($mediaType === null) {
return $parsedBody;
}
// Check if this specific media type has a parser registered first
if (!isset($this->bodyParsers[$mediaType])) {
// If not, look for a media type with a structured syntax suffix (RFC 6839)
$parts = explode('+', $mediaType);
if (count($parts) >= 2) {
$mediaType = 'application/' . $parts[count($parts) - 1];
}
}
if (isset($this->bodyParsers[$mediaType])) {
$body = (string)$this->getBody();
$parsed = $this->bodyParsers[$mediaType]($body);
if (!is_null($parsed) && !is_object($parsed) && !is_array($parsed)) {
throw new RuntimeException(
'Request body media type parser return value must be an array, an object, or null'
);
}
return $parsed;
}
return null;
}
/**
* {@inheritdoc}
*/
public function getProtocolVersion(): string
{
return $this->serverRequest->getProtocolVersion();
}
/**
* {@inheritdoc}
*/
public function getQueryParams(): array
{
$queryParams = $this->serverRequest->getQueryParams();
if (!empty($queryParams)) {
return $queryParams;
}
$parsedQueryParams = [];
parse_str($this->serverRequest->getUri()->getQuery(), $parsedQueryParams);
return $parsedQueryParams;
}
/**
* {@inheritdoc}
*/
public function getRequestTarget(): string
{
return $this->serverRequest->getRequestTarget();
}
/**
* {@inheritdoc}
*/
public function getServerParams(): array
{
return $this->serverRequest->getServerParams();
}
/**
* {@inheritdoc}
*/
public function getUploadedFiles(): array
{
return $this->serverRequest->getUploadedFiles();
}
/**
* {@inheritdoc}
*/
public function getUri(): UriInterface
{
return $this->serverRequest->getUri();
}
/**
* {@inheritdoc}
*/
public function hasHeader($name): bool
{
return $this->serverRequest->hasHeader($name);
}
/**
* {@inheritdoc}
*/
public function withAddedHeader($name, $value): MessageInterface
{
$serverRequest = $this->serverRequest->withAddedHeader($name, $value);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withAttribute($name, $value): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withAttribute($name, $value);
return new static($serverRequest);
}
/**
* Create a new instance with the specified derived request attributes.
*
* Note: This method is not part of the PSR-7 standard.
*
* This method allows setting all new derived request attributes as
* described in getAttributes().
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message, and MUST return a new instance that has the
* updated attributes.
*
* @param array $attributes New attributes
* @return static
*/
public function withAttributes(array $attributes): ServerRequestInterface
{
$serverRequest = $this->serverRequest;
foreach ($attributes as $attribute => $value) {
$serverRequest = $serverRequest->withAttribute($attribute, $value);
}
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withoutAttribute($name): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withoutAttribute($name);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withBody(StreamInterface $body): MessageInterface
{
$serverRequest = $this->serverRequest->withBody($body);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withCookieParams(array $cookies): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withCookieParams($cookies);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withHeader($name, $value): MessageInterface
{
$serverRequest = $this->serverRequest->withHeader($name, $value);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withoutHeader($name): MessageInterface
{
$serverRequest = $this->serverRequest->withoutHeader($name);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withMethod($method): RequestInterface
{
$serverRequest = $this->serverRequest->withMethod($method);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withParsedBody($data): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withParsedBody($data);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withProtocolVersion($version): MessageInterface
{
$serverRequest = $this->serverRequest->withProtocolVersion($version);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withQueryParams(array $query): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withQueryParams($query);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withRequestTarget($requestTarget): RequestInterface
{
$serverRequest = $this->serverRequest->withRequestTarget($requestTarget);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withUploadedFiles(array $uploadedFiles): ServerRequestInterface
{
$serverRequest = $this->serverRequest->withUploadedFiles($uploadedFiles);
return new static($serverRequest);
}
/**
* {@inheritdoc}
*/
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
$serverRequest = $this->serverRequest->withUri($uri, $preserveHost);
return new static($serverRequest);
}
/**
* Get serverRequest content character set, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null
*/
public function getContentCharset(): ?string
{
$mediaTypeParams = $this->getMediaTypeParams();
if (isset($mediaTypeParams['charset'])) {
return $mediaTypeParams['charset'];
}
return null;
}
/**
* Get serverRequest content type.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null The serverRequest content type, if known
*/
public function getContentType(): ?string
{
$result = $this->serverRequest->getHeader('Content-Type');
return $result ? $result[0] : null;
}
/**
* Get serverRequest content length, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return int|null
*/
public function getContentLength(): ?int
{
$result = $this->serverRequest->getHeader('Content-Length');
return $result ? (int) $result[0] : null;
}
/**
* Fetch cookie value from cookies sent by the client to the server.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key The attribute name.
* @param mixed $default Default value to return if the attribute does not exist.
*
* @return mixed
*/
public function getCookieParam(string $key, $default = null)
{
$cookies = $this->serverRequest->getCookieParams();
$result = $default;
if (isset($cookies[$key])) {
$result = $cookies[$key];
}
return $result;
}
/**
* Get serverRequest media type, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string|null The serverRequest media type, minus content-type params
*/
public function getMediaType(): ?string
{
$contentType = $this->getContentType();
if ($contentType) {
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
if ($contentTypeParts === false) {
return null;
}
return strtolower($contentTypeParts[0]);
}
return null;
}
/**
* Get serverRequest media type params, if known.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return string[]
*/
public function getMediaTypeParams(): array
{
$contentType = $this->getContentType();
$contentTypeParams = [];
if ($contentType) {
$contentTypeParts = preg_split('/\s*[;,]\s*/', $contentType);
if ($contentTypeParts !== false) {
$contentTypePartsLength = count($contentTypeParts);
for ($i = 1; $i < $contentTypePartsLength; $i++) {
$paramParts = explode('=', $contentTypeParts[$i]);
/** @var string[] $paramParts */
$contentTypeParams[strtolower($paramParts[0])] = $paramParts[1];
}
}
}
return $contentTypeParams;
}
/**
* Fetch serverRequest parameter value from body or query string (in that order).
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key The parameter key.
* @param mixed $default The default value.
*
* @return mixed The parameter value.
*/
public function getParam(string $key, $default = null)
{
$postParams = $this->getParsedBody();
$getParams = $this->getQueryParams();
$result = $default;
if (is_array($postParams) && isset($postParams[$key])) {
$result = $postParams[$key];
} elseif (is_object($postParams) && property_exists($postParams, $key)) {
$result = $postParams->$key;
} elseif (isset($getParams[$key])) {
$result = $getParams[$key];
}
return $result;
}
/**
* Fetch associative array of body and query string parameters.
*
* Note: This method is not part of the PSR-7 standard.
*
* @return array
*/
public function getParams(): array
{
$params = $this->getQueryParams();
$postParams = $this->getParsedBody();
if ($postParams) {
$params = array_merge($params, (array)$postParams);
}
return $params;
}
/**
* Fetch parameter value from serverRequest body.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getParsedBodyParam(string $key, $default = null)
{
$postParams = $this->getParsedBody();
$result = $default;
if (is_array($postParams) && isset($postParams[$key])) {
$result = $postParams[$key];
} elseif (is_object($postParams) && property_exists($postParams, $key)) {
$result = $postParams->$key;
}
return $result;
}
/**
* Fetch parameter value from query string.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function getQueryParam(string $key, $default = null)
{
$getParams = $this->getQueryParams();
$result = $default;
if (isset($getParams[$key])) {
$result = $getParams[$key];
}
return $result;
}
/**
* Retrieve a server parameter.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function getServerParam(string $key, $default = null)
{
$serverParams = $this->serverRequest->getServerParams();
return $serverParams[$key] ?? $default;
}
/**
* Register media type parser.
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $mediaType A HTTP media type (excluding content-type params).
* @param callable $callable A callable that returns parsed contents for media type.
* @return static
*/
public function registerMediaTypeParser(string $mediaType, callable $callable): ServerRequestInterface
{
if ($callable instanceof Closure) {
$callable = $callable->bindTo($this);
}
$this->bodyParsers[$mediaType] = $callable;
return $this;
}
/**
* Is this a DELETE serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isDelete(): bool
{
return $this->isMethod('DELETE');
}
/**
* Is this a GET serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isGet(): bool
{
return $this->isMethod('GET');
}
/**
* Is this a HEAD serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isHead(): bool
{
return $this->isMethod('HEAD');
}
/**
* Does this serverRequest use a given method?
*
* Note: This method is not part of the PSR-7 standard.
*
* @param string $method HTTP method
* @return bool
*/
public function isMethod(string $method): bool
{
return $this->serverRequest->getMethod() === $method;
}
/**
* Is this a OPTIONS serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isOptions(): bool
{
return $this->isMethod('OPTIONS');
}
/**
* Is this a PATCH serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPatch(): bool
{
return $this->isMethod('PATCH');
}
/**
* Is this a POST serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPost(): bool
{
return $this->isMethod('POST');
}
/**
* Is this a PUT serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isPut(): bool
{
return $this->isMethod('PUT');
}
/**
* Is this an XHR serverRequest?
*
* Note: This method is not part of the PSR-7 standard.
*
* @return bool
*/
public function isXhr(): bool
{
return $this->serverRequest->getHeaderLine('X-Requested-With') === 'XMLHttpRequest';
}
private static function disableXmlEntityLoader(bool $disable): bool
{
if (LIBXML_VERSION >= 20900) {
// libxml >= 2.9.0 disables entity loading by default, so it is
// safe to skip the real call (deprecated in PHP 8).
return true;
}
// @codeCoverageIgnoreStart
return libxml_disable_entity_loader($disable);
// @codeCoverageIgnoreEnd
}
}