1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPFastCGI\FastCGIDaemon\Http; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpFoundationRequest; |
6
|
|
|
use Zend\Diactoros\ServerRequest; |
7
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The default implementation of the RequestInterface. |
11
|
|
|
*/ |
12
|
|
|
class Request implements RequestInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected static $buffer_size = 10485760; // 10 MB |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected static $upload_dir = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $uploaded_files = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $params; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var resource |
36
|
|
|
*/ |
37
|
|
|
private $stdin; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $params The FastCGI server params as an associative array |
43
|
|
|
* @param resource $stdin The FastCGI stdin data as a stream resource |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $params, $stdin) |
46
|
|
|
{ |
47
|
|
|
$this->params = []; |
48
|
|
|
|
49
|
|
|
foreach ($params as $name => $value) { |
50
|
|
|
$this->params[strtoupper($name)] = $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->stdin = $stdin; |
54
|
|
|
|
55
|
|
|
rewind($this->stdin); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function getParams() |
62
|
|
|
{ |
63
|
|
|
return $this->params; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function cleanUploadedFiles() |
70
|
|
|
{ |
71
|
|
|
foreach ($this->uploaded_files as $file) { |
72
|
|
|
@unlink($file['tmp_name']); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public static function setBufferSize($size) |
80
|
|
|
{ |
81
|
|
|
static::$buffer_size = $size; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public static function getBufferSize() |
88
|
|
|
{ |
89
|
|
|
return static::$buffer_size; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public static function setUploadDir($dir) |
96
|
|
|
{ |
97
|
|
|
static::$upload_dir = $dir; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public static function getUploadDir() |
104
|
|
|
{ |
105
|
|
|
return static::$upload_dir ?: 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, 'application/x-www-form-urlencoded') === 0) { |
134
|
|
|
$postData = stream_get_contents($this->stdin); |
135
|
|
|
rewind($this->stdin); |
136
|
|
|
|
137
|
|
|
parse_str($postData, $post); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $post ?: []; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function getFile() |
148
|
|
|
{ |
149
|
|
|
if (isset($this->params['REQUEST_METHOD']) && isset($this->params['CONTENT_TYPE'])) { |
150
|
|
|
$requestMethod = $this->params['REQUEST_METHOD']; |
151
|
|
|
$contentType = $this->params['CONTENT_TYPE']; |
152
|
|
|
|
153
|
|
|
if (strcasecmp($requestMethod, 'POST') === 0 && stripos($contentType, 'multipart/form-data') === 0) { |
154
|
|
|
if (preg_match('/boundary=(.*)?/', $contentType, $matches)) { |
155
|
|
|
list($postData, $this->uploaded_files) = $this->parseMultipartFormData($this->stdin, $matches[1]); |
156
|
|
|
parse_str($postData, $post); |
157
|
|
|
return $post; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function parseMultipartFormData($stream, $boundary) { |
165
|
|
|
$post = ""; |
166
|
|
|
$files = []; |
167
|
|
|
$field_type = $field_name = $filename = $mimetype = null; |
|
|
|
|
168
|
|
|
$in_header = $get_content = false; |
|
|
|
|
169
|
|
|
|
170
|
|
|
while (!feof($stream)) { |
171
|
|
|
$get_content = $field_name && !$in_header; |
|
|
|
|
172
|
|
|
$buffer = stream_get_line($stream, static::$buffer_size, "\r\n" . ($get_content ? '--'.$boundary : '')); |
173
|
|
|
|
174
|
|
|
if ($in_header && strlen($buffer) == 0) { |
175
|
|
|
$in_header = false; |
176
|
|
|
} else { |
177
|
|
|
if ($get_content) { |
178
|
|
|
if ($field_type === 'data') { |
179
|
|
|
$post .= (isset($post[0]) ? '&' : '') . $field_name . "=" . urlencode($buffer); |
180
|
|
|
} elseif ($field_type === 'file' && $filename) { |
181
|
|
|
$tmp_path = $this->getUploadDir().'/'.substr(md5(rand().time()), 0, 16); |
182
|
|
|
$err = file_put_contents($tmp_path, $buffer); |
183
|
|
|
$files[$field_name] = [ |
184
|
|
|
'type' => $mime_type ?: 'application/octet-stream', |
|
|
|
|
185
|
|
|
'name' => $filename, |
186
|
|
|
'tmp_name' => $tmp_path, |
187
|
|
|
'error' => ($err === false) ? true : 0, |
188
|
|
|
'size' => filesize($tmp_path), |
189
|
|
|
]; |
190
|
|
|
$filename = $mime_type = null; |
191
|
|
|
} |
192
|
|
|
$field_name = $field_type = null; |
193
|
|
|
} elseif (strpos($buffer, 'Content-Disposition') === 0) { |
194
|
|
|
$in_header = true; |
195
|
|
|
if (preg_match('/name=\"([^\"]*)\"/', $buffer, $matches)) { |
196
|
|
|
$field_name = $matches[1]; |
197
|
|
|
} |
198
|
|
|
if ($is_file = preg_match('/filename=\"([^\"]*)\"/', $buffer, $matches)) { |
199
|
|
|
$filename = $matches[1]; |
200
|
|
|
} |
201
|
|
|
$field_type = $is_file ? 'file' : 'data'; |
202
|
|
|
} elseif (strpos($buffer, 'Content-Type') === 0) { |
203
|
|
|
$in_header = true; |
204
|
|
|
if (preg_match('/Content-Type: (.*)?/', $buffer, $matches)) { |
205
|
|
|
$mime_type = trim($matches[1]); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return [$post, $files]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritdoc} |
216
|
|
|
*/ |
217
|
|
|
public function getCookies() |
218
|
|
|
{ |
219
|
|
|
$cookies = []; |
220
|
|
|
|
221
|
|
|
if (isset($this->params['HTTP_COOKIE'])) { |
222
|
|
|
$cookiePairs = explode(';', $this->params['HTTP_COOKIE']); |
223
|
|
|
|
224
|
|
|
foreach ($cookiePairs as $cookiePair) { |
225
|
|
|
list($name, $value) = explode('=', trim($cookiePair)); |
226
|
|
|
$cookies[$name] = $value; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $cookies; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
|
public function getStdin() |
237
|
|
|
{ |
238
|
|
|
return $this->stdin; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* {@inheritdoc} |
243
|
|
|
*/ |
244
|
|
|
public function getServerRequest() |
245
|
|
|
{ |
246
|
|
|
$query = $this->getQuery(); |
247
|
|
|
$post = $this->getFile() ?: $this->getPost(); |
248
|
|
|
$cookies = $this->getCookies(); |
249
|
|
|
|
250
|
|
|
$server = ServerRequestFactory::normalizeServer($this->params); |
251
|
|
|
$headers = ServerRequestFactory::marshalHeaders($server); |
252
|
|
|
$uri = ServerRequestFactory::marshalUriFromServer($server, $headers); |
253
|
|
|
$method = ServerRequestFactory::get('REQUEST_METHOD', $server, 'GET'); |
254
|
|
|
|
255
|
|
|
$request = new ServerRequest($server, $this->uploaded_files, $uri, $method, $this->stdin, $headers); |
256
|
|
|
|
257
|
|
|
return $request |
258
|
|
|
->withCookieParams($cookies) |
259
|
|
|
->withQueryParams($query) |
260
|
|
|
->withParsedBody($post); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* {@inheritdoc} |
265
|
|
|
*/ |
266
|
|
|
public function getHttpFoundationRequest() |
267
|
|
|
{ |
268
|
|
|
$query = $this->getQuery(); |
269
|
|
|
$post = $this->getFile() ?: $this->getPost(); |
270
|
|
|
$cookies = $this->getCookies(); |
271
|
|
|
|
272
|
|
|
return new HttpFoundationRequest($query, $post, [], $cookies, $this->uploaded_files, $this->params, $this->stdin); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: