-
-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathRequest.php
More file actions
250 lines (212 loc) · 5.28 KB
/
Request.php
File metadata and controls
250 lines (212 loc) · 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
namespace TusPhp;
use TusPhp\Tus\Server;
use Symfony\Component\HttpFoundation\Request as HttpRequest;
class Request
{
/** @var HttpRequest */
protected $request;
/**
* Request constructor.
*/
public function __construct(HttpRequest $request = null)
{
if (null === $request) {
$this->request = HttpRequest::createFromGlobals();
} else {
$this->request = $request;
}
}
/**
* Get http method from current request.
*
* @return string
*/
public function method(): string
{
return $this->request->getMethod();
}
/**
* Get the current path info for the request.
*
* @return string
*/
public function path(): string
{
return $this->request->getPathInfo();
}
/**
* Get upload key from url.
*
* @return string
*/
public function key(): string
{
return basename($this->path());
}
/**
* Supported http requests.
*
* @return array
*/
public function allowedHttpVerbs(): array
{
return [
HttpRequest::METHOD_GET,
HttpRequest::METHOD_POST,
HttpRequest::METHOD_PATCH,
HttpRequest::METHOD_DELETE,
HttpRequest::METHOD_HEAD,
HttpRequest::METHOD_OPTIONS,
];
}
/**
* Retrieve a header from the request.
*
* @param string $key
* @param string|string[]|null $default
*
* @return string|null
*/
public function header(string $key, $default = null): ?string
{
return $this->request->headers->get($key, $default);
}
/**
* Get the root URL for the request.
*
* @return string
*/
public function url(): string
{
return rtrim($this->request->getUriForPath('/'), '/');
}
/**
* Extract metadata from header.
*
* @param string $key
* @param string $value
*
* @return array
*/
public function extractFromHeader(string $key, string $value): array
{
$meta = $this->header($key);
if (false !== strpos($meta, $value)) {
$meta = trim(str_replace($value, '', $meta));
return explode(' ', $meta) ?? [];
}
return [];
}
/**
* Extract base64 encoded filename from header.
*
* @return string
*/
public function extractFileName(): string
{
$name = $this->extractMeta('name') ?: $this->extractMeta('filename');
if ( ! $this->isValidFilename($name)) {
return '';
}
return $name;
}
/**
* Extracts the metadata from the request header.
*
* @param string $requestedKey
*
* @return string
*/
public function extractMeta(string $requestedKey): string
{
$uploadMetaData = $this->request->headers->get('Upload-Metadata');
if (empty($uploadMetaData)) {
return '';
}
$uploadMetaDataChunks = explode(',', $uploadMetaData);
foreach ($uploadMetaDataChunks as $chunk) {
$pieces = explode(' ', trim($chunk));
$key = $pieces[0];
$value = $pieces[1] ?? '';
if ($key === $requestedKey) {
return base64_decode($value);
}
}
return '';
}
/**
* Extracts all meta data from the request header.
*
* @return string[]
*/
public function extractAllMeta(): array
{
$uploadMetaData = $this->request->headers->get('Upload-Metadata');
if (empty($uploadMetaData)) {
return [];
}
$uploadMetaDataChunks = explode(',', $uploadMetaData);
$result = [];
foreach ($uploadMetaDataChunks as $chunk) {
$pieces = explode(' ', trim($chunk));
$key = $pieces[0];
$value = $pieces[1] ?? '';
$result[$key] = base64_decode($value);
}
return $result;
}
/**
* Extract partials from header.
*
* @return array
*/
public function extractPartials(): array
{
return $this->extractFromHeader('Upload-Concat', Server::UPLOAD_TYPE_FINAL . ';');
}
/**
* Check if this is a partial upload request.
*
* @return bool
*/
public function isPartial(): bool
{
return Server::UPLOAD_TYPE_PARTIAL === $this->header('Upload-Concat');
}
/**
* Check if this is a final concatenation request.
*
* @return bool
*/
public function isFinal(): bool
{
return null !== ($header = $this->header('Upload-Concat')) && false !== strpos($header, Server::UPLOAD_TYPE_FINAL . ';');
}
/**
* Get request.
*
* @return HttpRequest
*/
public function getRequest(): HttpRequest
{
return $this->request;
}
/**
* Validate file name.
*
* @param string $filename
*
* @return bool
*/
protected function isValidFilename(string $filename): bool
{
$forbidden = ['../', '"', "'", '&', '/', '\\', '?', '#', ':'];
foreach ($forbidden as $char) {
if (false !== strpos($filename, $char)) {
return false;
}
}
return true;
}
}