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