1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use function RingCentral\Psr7\stream_for; |
|
|
|
|
7
|
|
|
|
8
|
|
|
final class CustomRequestBodyParsers |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
private $types = []; |
14
|
|
|
|
15
|
|
|
public function __construct() |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Via: https://github.com/reactphp/http/pull/220#discussion_r140863176. |
19
|
|
|
*/ |
20
|
5 |
|
$this->addType('application/json', function (ServerRequestInterface $request) { |
21
|
1 |
|
$body = (string)$request->getBody(); |
22
|
1 |
|
$result = json_decode($body, true); |
23
|
1 |
|
if (!is_array($result)) { |
24
|
|
|
return $request; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
return $request->withParsedBody($result)->withBody(stream_for($body)); |
28
|
5 |
|
}); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Via: https://github.com/reactphp/http/pull/220#discussion_r140863176. |
32
|
|
|
*/ |
33
|
5 |
|
$xmlParser = function (ServerRequestInterface $request) { |
34
|
3 |
|
$body = (string)$request->getBody(); |
35
|
3 |
|
$backup = libxml_disable_entity_loader(true); |
36
|
3 |
|
$backup_errors = libxml_use_internal_errors(true); |
37
|
3 |
|
$result = simplexml_load_string($body); |
38
|
3 |
|
libxml_disable_entity_loader($backup); |
39
|
3 |
|
libxml_clear_errors(); |
40
|
3 |
|
libxml_use_internal_errors($backup_errors); |
41
|
3 |
|
if ($result === false) { |
42
|
|
|
return $request; |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
return $request->withParsedBody($result)->withBody(stream_for($body)); |
46
|
5 |
|
}; |
47
|
5 |
|
$this->addType('application/xml', $xmlParser); |
48
|
5 |
|
$this->addType('text/xml', $xmlParser); |
49
|
5 |
|
} |
50
|
|
|
|
51
|
5 |
|
public function __invoke(ServerRequestInterface $request, $next) |
52
|
|
|
{ |
53
|
5 |
|
$type = strtolower($request->getHeaderLine('Content-Type')); |
54
|
5 |
|
list($type) = explode(';', $type); |
55
|
|
|
|
56
|
5 |
|
if (!isset($this->types[$type])) { |
57
|
|
|
return $next($request); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
5 |
|
$parser = $this->types[$type]; |
62
|
|
|
/** @var ServerRequestInterface $request */ |
63
|
5 |
|
$request = $parser($request); |
64
|
|
|
} catch (\Exception $e) { |
65
|
|
|
return $next($request); |
66
|
|
|
} catch (\Throwable $t) { |
67
|
|
|
return $next($request); |
68
|
|
|
} |
69
|
|
|
|
70
|
5 |
|
return $next($request); |
71
|
|
|
} |
72
|
|
|
|
73
|
5 |
|
public function addType($type, $callback) |
74
|
|
|
{ |
75
|
5 |
|
$this->types[$type] = $callback; |
76
|
5 |
|
} |
77
|
|
|
} |
78
|
|
|
|
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