1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPFastCGI\FastCGIDaemon\Http; |
4
|
|
|
|
5
|
|
|
use Nyholm\Psr7Server\ServerRequestCreatorInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The default implementation of the RequestInterface. |
10
|
|
|
*/ |
11
|
|
|
final class Request implements RequestInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ServerRequestCreatorInterface|null |
15
|
|
|
*/ |
16
|
|
|
private static $serverRequestCreator = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private static $bufferSize = 10485760; // 10 MB |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private static $uploadDir = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $uploadedFiles = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $params; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var resource |
40
|
|
|
*/ |
41
|
|
|
private $stdin; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param array $params The FastCGI server params as an associative array |
47
|
|
|
* @param resource $stdin The FastCGI stdin data as a stream resource |
48
|
|
|
*/ |
49
|
|
|
public function __construct(array $params, $stdin) |
50
|
|
|
{ |
51
|
|
|
$this->params = []; |
52
|
|
|
|
53
|
|
|
foreach ($params as $name => $value) { |
54
|
|
|
$this->params[strtoupper($name)] = $value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->stdin = $stdin; |
58
|
|
|
|
59
|
|
|
rewind($this->stdin); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void |
63
|
|
|
{ |
64
|
|
|
self::$serverRequestCreator = $serverRequestCreator; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getParams() |
71
|
|
|
{ |
72
|
|
|
return $this->params; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Remove all uploaded files |
77
|
|
|
*/ |
78
|
|
|
public function cleanUploadedFiles(): void |
79
|
|
|
{ |
80
|
|
|
foreach ($this->uploadedFiles as $file) { |
81
|
|
|
@unlink($file['tmp_name']); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set a buffer size to read uploaded files |
87
|
|
|
*/ |
88
|
|
|
public static function setBufferSize(int $size): void |
89
|
|
|
{ |
90
|
|
|
static::$bufferSize = $size; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public static function getBufferSize(): int |
94
|
|
|
{ |
95
|
|
|
return static::$bufferSize; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public static function setUploadDir(string $dir): void |
99
|
|
|
{ |
100
|
|
|
static::$uploadDir = $dir; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public static function getUploadDir(): string |
104
|
|
|
{ |
105
|
|
|
return static::$uploadDir ?: sys_get_temp_dir(); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
|
public function getQuery() |
112
|
|
|
{ |
113
|
|
|
$query = null; |
114
|
|
|
|
115
|
|
|
if (isset($this->params['QUERY_STRING'])) { |
116
|
|
|
parse_str($this->params['QUERY_STRING'], $query); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $query ?: []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function getPost() |
126
|
|
|
{ |
127
|
|
|
$post = null; |
128
|
|
|
|
129
|
|
|
if (isset($this->params['REQUEST_METHOD']) && isset($this->params['CONTENT_TYPE'])) { |
130
|
|
|
$requestMethod = $this->params['REQUEST_METHOD']; |
131
|
|
|
$contentType = $this->params['CONTENT_TYPE']; |
132
|
|
|
|
133
|
|
|
if (strcasecmp($requestMethod, 'POST') === 0 && stripos($contentType, 'multipart/form-data') === 0) { |
134
|
|
|
if (preg_match('/boundary=(?P<quote>[\'"]?)(.*)(?P=quote)/', $contentType, $matches)) { |
135
|
|
|
list($postData, $this->uploadedFiles) = $this->parseMultipartFormData($this->stdin, $matches[2]); |
136
|
|
|
parse_str($postData, $post); |
137
|
|
|
|
138
|
|
|
return $post; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (strcasecmp($requestMethod, 'POST') === 0 && stripos($contentType, 'application/x-www-form-urlencoded') === 0) { |
143
|
|
|
$postData = stream_get_contents($this->stdin); |
144
|
|
|
rewind($this->stdin); |
145
|
|
|
|
146
|
|
|
parse_str($postData, $post); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $post ?: []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
private function parseMultipartFormData($stream, $boundary) { |
154
|
|
|
$post = ""; |
155
|
|
|
$files = []; |
156
|
|
|
$fieldType = $fieldName = $filename = $mimeType = null; |
157
|
|
|
$inHeader = $getContent = false; |
|
|
|
|
158
|
|
|
|
159
|
|
|
while (!feof($stream)) { |
160
|
|
|
$getContent = $fieldName && !$inHeader; |
|
|
|
|
161
|
|
|
$buffer = stream_get_line($stream, static::$bufferSize, "\n" . ($getContent ? '--'.$boundary : '')); |
|
|
|
|
162
|
|
|
$buffer = trim($buffer, "\r"); |
163
|
|
|
|
164
|
|
|
// Find the empty line between headers and body |
165
|
|
|
if ($inHeader && strlen($buffer) == 0) { |
166
|
|
|
$inHeader = false; |
167
|
|
|
|
168
|
|
|
continue; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($getContent) { |
172
|
|
|
if ($fieldType === 'data') { |
173
|
|
|
$post .= (isset($post[0]) ? '&' : '') . $fieldName . "=" . urlencode($buffer); |
174
|
|
|
} elseif ($fieldType === 'file' && $filename) { |
175
|
|
|
$tmpPath = @tempnam($this->getUploadDir(), 'fastcgi_upload'); |
176
|
|
|
$err = file_put_contents($tmpPath, $buffer); |
177
|
|
|
$this->addFile($files, $fieldName, $filename, $tmpPath, $mimeType, false === $err); |
178
|
|
|
$filename = $mimeType = null; |
179
|
|
|
} |
180
|
|
|
$fieldName = $fieldType = null; |
181
|
|
|
|
182
|
|
|
continue; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Assert: We may be in the header, lets try to find 'Content-Disposition' and 'Content-Type'. |
186
|
|
|
if (strpos($buffer, 'Content-Disposition') === 0) { |
187
|
|
|
$inHeader = true; |
188
|
|
|
if (preg_match('/name=\"([^\"]*)\"/', $buffer, $matches)) { |
189
|
|
|
$fieldName = $matches[1]; |
190
|
|
|
} |
191
|
|
|
if (preg_match('/filename=\"([^\"]*)\"/', $buffer, $matches)) { |
192
|
|
|
$filename = $matches[1]; |
193
|
|
|
$fieldType = 'file'; |
194
|
|
|
} else { |
195
|
|
|
$fieldType = 'data'; |
196
|
|
|
} |
197
|
|
|
} elseif (strpos($buffer, 'Content-Type') === 0) { |
198
|
|
|
$inHeader = true; |
199
|
|
|
if (preg_match('/Content-Type: (.*)?/', $buffer, $matches)) { |
200
|
|
|
$mimeType = trim($matches[1]); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return [$post, $files]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* {@inheritdoc} |
210
|
|
|
*/ |
211
|
|
|
public function getCookies() |
212
|
|
|
{ |
213
|
|
|
$cookies = []; |
214
|
|
|
|
215
|
|
|
if (isset($this->params['HTTP_COOKIE'])) { |
216
|
|
|
$cookiePairs = explode(';', $this->params['HTTP_COOKIE']); |
217
|
|
|
|
218
|
|
|
foreach ($cookiePairs as $cookiePair) { |
219
|
|
|
list($name, $value) = explode('=', trim($cookiePair)); |
220
|
|
|
$cookies[$name] = $value; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $cookies; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
|
|
public function getStdin() |
231
|
|
|
{ |
232
|
|
|
return $this->stdin; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function getServerRequest() |
239
|
|
|
{ |
240
|
|
|
if (null === self::$serverRequestCreator) { |
241
|
|
|
throw new \RuntimeException('You need to add an object of \Nyholm\Psr7Server\ServerRequestCreatorInterface to \PHPFastCGI\FastCGIDaemon\Http\Request::setServerRequestCreator to use PSR-7 requests. Please install and read more at https://github.com/nyholm/psr7-server'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$server = $this->params; |
245
|
|
|
$query = $this->getQuery(); |
246
|
|
|
$post = $this->getPost(); |
247
|
|
|
$cookies = $this->getCookies(); |
248
|
|
|
|
249
|
|
|
return self::$serverRequestCreator->fromArrays( |
250
|
|
|
$server, |
251
|
|
|
self::$serverRequestCreator->getHeadersFromServer($server), |
252
|
|
|
$cookies, |
253
|
|
|
$query, |
254
|
|
|
$post, |
|
|
|
|
255
|
|
|
$this->uploadedFiles, |
256
|
|
|
$this->stdin |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* {@inheritdoc} |
264
|
|
|
*/ |
265
|
|
|
public function getHttpFoundationRequest() |
266
|
|
|
{ |
267
|
|
|
if (!class_exists(HttpFoundationRequest::class)) { |
268
|
|
|
throw new \RuntimeException('You need to install symfony/http-foundation:^4.0 to use HttpFoundation requests.'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$query = $this->getQuery(); |
272
|
|
|
$post = $this->getPost(); |
273
|
|
|
$cookies = $this->getCookies(); |
274
|
|
|
|
275
|
|
|
return new HttpFoundationRequest($query, $post, [], $cookies, $this->uploadedFiles, $this->params, $this->stdin); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Add a file to the $files array |
280
|
|
|
*/ |
281
|
|
|
private function addFile(array &$files, string $fieldName, string $filename, string $tmpPath, string $mimeType, bool $err): void |
282
|
|
|
{ |
283
|
|
|
$data = [ |
284
|
|
|
'type' => $mimeType ?: 'application/octet-stream', |
285
|
|
|
'name' => $filename, |
286
|
|
|
'tmp_name' => $tmpPath, |
287
|
|
|
'error' => $err ? UPLOAD_ERR_CANT_WRITE : UPLOAD_ERR_OK, |
288
|
|
|
'size' => filesize($tmpPath), |
289
|
|
|
]; |
290
|
|
|
|
291
|
|
|
$parts = preg_split('|(\[[^\]]*\])|', $fieldName, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
292
|
|
|
$count = count($parts); |
293
|
|
|
if (1 === $count) { |
294
|
|
|
$files[$fieldName] = $data; |
295
|
|
|
} else { |
296
|
|
|
$current = &$files; |
297
|
|
|
foreach ($parts as $i => $part) { |
298
|
|
|
if ($part === '[]') { |
299
|
|
|
$current[] = $data; |
300
|
|
|
continue; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$trimmedMatch = trim($part, '[]'); |
304
|
|
|
if ($i === $count -1) { |
305
|
|
|
$current[$trimmedMatch] = $data; |
306
|
|
|
} else { |
307
|
|
|
$current = &$current[$trimmedMatch]; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: