1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use function React\Promise\reject; |
7
|
|
|
use function RingCentral\Psr7\stream_for; |
|
|
|
|
8
|
|
|
|
9
|
|
|
final class CustomRequestBodyParsers |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
private $types = []; |
15
|
|
|
|
16
|
|
|
public function __construct() |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Via: https://github.com/reactphp/http/pull/220#discussion_r140863176. |
20
|
|
|
*/ |
21
|
6 |
|
$this->addType('application/json', function (ServerRequestInterface $request) { |
22
|
1 |
|
$body = (string)$request->getBody(); |
23
|
1 |
|
$result = json_decode($body, true); |
24
|
1 |
|
if (!is_array($result)) { |
25
|
|
|
return $request; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
return $request->withParsedBody($result)->withBody(stream_for($body)); |
29
|
6 |
|
}); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Via: https://github.com/reactphp/http/pull/220#discussion_r140863176. |
33
|
|
|
*/ |
34
|
6 |
|
$xmlParser = function (ServerRequestInterface $request) { |
35
|
3 |
|
$body = (string)$request->getBody(); |
36
|
3 |
|
$backup = libxml_disable_entity_loader(true); |
37
|
3 |
|
$backup_errors = libxml_use_internal_errors(true); |
38
|
3 |
|
$result = simplexml_load_string($body); |
39
|
3 |
|
libxml_disable_entity_loader($backup); |
40
|
3 |
|
libxml_clear_errors(); |
41
|
3 |
|
libxml_use_internal_errors($backup_errors); |
42
|
3 |
|
if ($result === false) { |
43
|
|
|
return $request; |
44
|
|
|
} |
45
|
|
|
|
46
|
3 |
|
return $request->withParsedBody($result)->withBody(stream_for($body)); |
47
|
6 |
|
}; |
48
|
6 |
|
$this->addType('application/xml', $xmlParser); |
49
|
6 |
|
$this->addType('text/xml', $xmlParser); |
50
|
6 |
|
} |
51
|
|
|
|
52
|
6 |
|
public function __invoke(ServerRequestInterface $request, $next) |
53
|
|
|
{ |
54
|
6 |
|
$type = strtolower($request->getHeaderLine('Content-Type')); |
55
|
6 |
|
list($type) = explode(';', $type); |
56
|
|
|
|
57
|
6 |
|
if (!isset($this->types[$type])) { |
58
|
|
|
return $next($request); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
6 |
|
$parser = $this->types[$type]; |
63
|
|
|
/** @var ServerRequestInterface $request */ |
64
|
6 |
|
$request = $parser($request); |
65
|
1 |
|
} catch (\Throwable $t) { |
66
|
1 |
|
return reject($t); |
67
|
|
|
} |
68
|
|
|
|
69
|
5 |
|
return $next($request); |
70
|
|
|
} |
71
|
|
|
|
72
|
6 |
|
public function addType($type, $callback) |
73
|
|
|
{ |
74
|
6 |
|
$this->types[$type] = $callback; |
75
|
6 |
|
} |
76
|
|
|
} |
77
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths