|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Softonic\RequestContentDecompress\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
7
|
|
|
use Illuminate\Http\JsonResponse; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
use Illuminate\Support\Facades\App; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
|
11
|
|
|
|
|
12
|
|
|
class RequestContentDecompress |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var App |
|
16
|
|
|
*/ |
|
17
|
|
|
private $app; |
|
18
|
|
|
|
|
19
|
6 |
|
public function __construct(Application $app) |
|
20
|
|
|
{ |
|
21
|
6 |
|
$this->app = $app; |
|
|
|
|
|
|
22
|
6 |
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Handle an incoming request. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* @param \Closure $next |
|
29
|
|
|
* |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
6 |
|
public function handle($request, Closure $next) |
|
33
|
|
|
{ |
|
34
|
6 |
|
$newRequest = $request; |
|
35
|
6 |
|
if ($request->header('Content-Encoding') === 'gzip') { |
|
36
|
4 |
|
$zippedContent = $request->getContent(); |
|
37
|
4 |
|
$request->headers->remove('Content-Encoding'); |
|
38
|
4 |
|
$content = @gzdecode($zippedContent); |
|
39
|
4 |
|
if ($content === false) { |
|
40
|
2 |
|
return new JsonResponse(['error' => 'Invalid data encoding.'], 415); |
|
41
|
|
|
} |
|
42
|
2 |
|
$baseRequest = new SymfonyRequest(); |
|
43
|
2 |
|
$baseRequest->initialize( |
|
44
|
2 |
|
$request->query->all(), |
|
45
|
2 |
|
$request->request->all(), |
|
46
|
2 |
|
$request->attributes->all(), |
|
47
|
2 |
|
$request->cookies->all(), |
|
48
|
2 |
|
$request->files->all(), |
|
49
|
2 |
|
$request->server->all(), |
|
50
|
2 |
|
$content |
|
51
|
|
|
); |
|
52
|
2 |
|
$newRequest = Request::createFromBase($baseRequest); |
|
53
|
2 |
|
$this->app->instance(Request::class, $newRequest); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
return $next($newRequest); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..