1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Transformers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request as IlluminateRequest; |
6
|
|
|
use Swoole\Http\Request as SwooleRequest; |
7
|
|
|
use Swoole\Http\Response as SwooleResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
10
|
|
|
|
11
|
|
|
class Request |
12
|
|
|
{ |
13
|
|
|
const BLACK_LIST = ['php', 'htaccess', 'config']; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Illuminate\Http\Request |
17
|
|
|
*/ |
18
|
|
|
protected $illuminateRequest; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Make a request. |
22
|
|
|
* |
23
|
|
|
* @param \Swoole\Http\Request $swooleRequest |
24
|
|
|
* @return \SwooleTW\Http\Server\Request |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public static function make(SwooleRequest $swooleRequest) |
27
|
|
|
{ |
28
|
|
|
list($get, $post, $cookie, $files, $server, $content) |
29
|
|
|
= static::toIlluminateParameters($swooleRequest); |
30
|
|
|
|
31
|
|
|
return new static($get, $post, $cookie, $files, $server, $content); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Request constructor. |
36
|
|
|
* |
37
|
|
|
* @param array $get |
38
|
|
|
* @param array $post |
39
|
|
|
* @param array $cookie |
40
|
|
|
* @param array $files |
41
|
|
|
* @param array $server |
42
|
|
|
* @param string $content |
43
|
|
|
* @throws \LogicException |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $get, array $post, array $cookie, array $files, array $server, $content = null) |
46
|
|
|
{ |
47
|
|
|
$this->createIlluminateRequest($get, $post, $cookie, $files, $server, $content); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create Illuminate Request. |
52
|
|
|
* |
53
|
|
|
* @param array $get |
54
|
|
|
* @param array $post |
55
|
|
|
* @param array $cookie |
56
|
|
|
* @param array $files |
57
|
|
|
* @param array $server |
58
|
|
|
* @param string $content |
59
|
|
|
* @return \Illuminate\Http\Request |
60
|
|
|
* @throws \LogicException |
61
|
|
|
*/ |
62
|
|
|
protected function createIlluminateRequest($get, $post, $cookie, $files, $server, $content = null) |
63
|
|
|
{ |
64
|
|
|
IlluminateRequest::enableHttpMethodParameterOverride(); |
65
|
|
|
|
66
|
|
|
/* |
67
|
|
|
|-------------------------------------------------------------------------- |
68
|
|
|
| Copy from \Symfony\Component\HttpFoundation\Request::createFromGlobals(). |
69
|
|
|
|-------------------------------------------------------------------------- |
70
|
|
|
| |
71
|
|
|
| With the php's bug #66606, the php's built-in web server |
72
|
|
|
| stores the Content-Type and Content-Length header values in |
73
|
|
|
| HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH fields. |
74
|
|
|
| |
75
|
|
|
*/ |
76
|
|
|
|
77
|
|
|
if ('cli-server' === PHP_SAPI) { |
78
|
|
|
if (array_key_exists('HTTP_CONTENT_LENGTH', $server)) { |
79
|
|
|
$server['CONTENT_LENGTH'] = $server['HTTP_CONTENT_LENGTH']; |
80
|
|
|
} |
81
|
|
|
if (array_key_exists('HTTP_CONTENT_TYPE', $server)) { |
82
|
|
|
$server['CONTENT_TYPE'] = $server['HTTP_CONTENT_TYPE']; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$request = new SymfonyRequest($get, $post, [], $cookie, $files, $server, $content); |
87
|
|
|
|
88
|
|
|
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded') |
89
|
|
|
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH')) |
90
|
|
|
) { |
91
|
|
|
parse_str($request->getContent(), $data); |
92
|
|
|
$request->request = new ParameterBag($data); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->illuminateRequest = IlluminateRequest::createFromBase($request); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Illuminate\Http\Request |
100
|
|
|
*/ |
101
|
|
|
public function toIlluminate() |
102
|
|
|
{ |
103
|
|
|
return $this->getIlluminateRequest(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return \Illuminate\Http\Request |
108
|
|
|
*/ |
109
|
|
|
public function getIlluminateRequest() |
110
|
|
|
{ |
111
|
|
|
return $this->illuminateRequest; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Transforms request parameters. |
116
|
|
|
* |
117
|
|
|
* @param \Swoole\Http\Request $request |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected static function toIlluminateParameters(SwooleRequest $request) |
121
|
|
|
{ |
122
|
|
|
$get = isset($request->get) ? $request->get : []; |
123
|
|
|
$post = isset($request->post) ? $request->post : []; |
124
|
|
|
$cookie = isset($request->cookie) ? $request->cookie : []; |
125
|
|
|
$files = isset($request->files) ? $request->files : []; |
126
|
|
|
$header = isset($request->header) ? $request->header : []; |
127
|
|
|
$server = isset($request->server) ? $request->server : []; |
128
|
|
|
$server = static::transformServerParameters($server, $header); |
129
|
|
|
$content = $request->rawContent(); |
130
|
|
|
|
131
|
|
|
return [$get, $post, $cookie, $files, $server, $content]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Transforms $_SERVER array. |
136
|
|
|
* |
137
|
|
|
* @param array $server |
138
|
|
|
* @param array $header |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected static function transformServerParameters(array $server, array $header) |
142
|
|
|
{ |
143
|
|
|
$__SERVER = []; |
144
|
|
|
|
145
|
|
|
foreach ($server as $key => $value) { |
146
|
|
|
$key = strtoupper($key); |
147
|
|
|
$__SERVER[$key] = $value; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
foreach ($header as $key => $value) { |
151
|
|
|
$key = str_replace('-', '_', $key); |
152
|
|
|
$key = strtoupper($key); |
153
|
|
|
|
154
|
|
|
if (! in_array($key, ['REMOTE_ADDR', 'SERVER_PORT', 'HTTPS'])) { |
155
|
|
|
$key = 'HTTP_' . $key; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$__SERVER[$key] = $value; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $__SERVER; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Handle static request. |
166
|
|
|
* |
167
|
|
|
* @param \Swoole\Http\Request $swooleRequest |
168
|
|
|
* @param \Swoole\Http\Response $swooleResponse |
169
|
|
|
* @param string $path |
170
|
|
|
* @return boolean |
171
|
|
|
*/ |
172
|
|
|
public static function handleStatic($swooleRequest, $swooleResponse, string $publicPath) |
173
|
|
|
{ |
174
|
|
|
$uri = $swooleRequest->server['request_uri'] ?? ''; |
175
|
|
|
$extension = substr(strrchr($uri, '.'), 1); |
176
|
|
|
if ($extension && in_array($extension, static::BLACK_LIST)) { |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$filename = $publicPath . $uri; |
181
|
|
|
if (! is_file($filename) || filesize($filename) === 0) { |
182
|
|
|
return; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$swooleResponse->status(200); |
186
|
|
|
$mime = mime_content_type($filename); |
187
|
|
|
if ($extension === 'js') { |
188
|
|
|
$mime = 'text/javascript'; |
189
|
|
|
} elseif ($extension === 'css') { |
190
|
|
|
$mime = 'text/css'; |
191
|
|
|
} |
192
|
|
|
$swooleResponse->header('Content-Type', $mime); |
193
|
|
|
$swooleResponse->sendfile($filename); |
194
|
|
|
|
195
|
|
|
return true; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
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