forked from yegor256/takes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RqMultipart.java
557 lines (542 loc) · 19.9 KB
/
RqMultipart.java
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.rq;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.misc.Sprintf;
import org.takes.misc.VerboseIterable;
/**
* HTTP multipart FORM data decoding.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.9
*/
@SuppressWarnings("PMD.TooManyMethods")
public interface RqMultipart extends Request {
/**
* Get single part.
* @param name Name of the part to get
* @return List of parts (can be empty)
*/
Iterable<Request> part(CharSequence name);
/**
* Get all part names.
* @return All names
*/
Iterable<String> names();
/**
* Request decorator, that decodes FORM data from
* {@code multipart/form-data} format (RFC 2045).
*
* <p>For {@code application/x-www-form-urlencoded}
* format use {@link org.takes.rq.RqForm}.
*
* <p>It is highly recommended to use {@link org.takes.rq.RqGreedy}
* decorator before passing request to this class.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.9
* @see <a href="http://www.w3.org/TR/html401/interact/forms.html">
* Forms in HTML</a>
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @see org.takes.rq.RqGreedy
*/
@EqualsAndHashCode(callSuper = true)
final class Base extends RqWrap implements RqMultipart {
/**
* Pattern to get boundary from header.
*/
private static final Pattern BOUNDARY = Pattern.compile(
".*[^a-z]boundary=([^;]+).*"
);
/**
* Pattern to get name from header.
*/
private static final Pattern NAME = Pattern.compile(
".*[^a-z]name=\"([^\"]+)\".*"
);
/**
* Map of params and values.
*/
private final transient ConcurrentMap<String, List<Request>> map;
/**
* Internal buffer.
*/
private final transient ByteBuffer buffer;
/**
* Origin request body.
*/
private final transient ReadableByteChannel body;
/**
* Ctor.
* @param req Original request
* @throws IOException If fails
* @checkstyle ExecutableStatementCountCheck (2 lines)
* @todo #558:30min Base ctor. According to new qulice version,
* constructor must contain only variables initialization and other
* constructor calls. Refactor code according to that rule and
* remove `ConstructorOnlyInitializesOrCallOtherConstructors`
* warning suppression.
*/
@SuppressWarnings
(
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
)
public Base(final Request req) throws IOException {
super(req);
final InputStream stream = new RqLengthAware(req).body();
try {
this.body = Channels.newChannel(stream);
try {
this.buffer = ByteBuffer.allocate(
// @checkstyle MagicNumberCheck (1 line)
Math.min(8192, stream.available())
);
this.map = this.buildRequests(req);
} finally {
this.body.close();
}
} finally {
stream.close();
}
}
@Override
public Iterable<Request> part(final CharSequence name) {
final List<Request> values = this.map
.get(name.toString().toLowerCase(Locale.ENGLISH));
final Iterable<Request> iter;
if (values == null) {
iter = new VerboseIterable<Request>(
Collections.<Request>emptyList(),
new Sprintf(
"there are no parts by name \"%s\" among %d others: %s",
name, this.map.size(), this.map.keySet()
)
);
} else {
iter = new VerboseIterable<Request>(
values,
new Sprintf(
"there are just %d parts by name \"%s\"",
values.size(), name
)
);
}
return iter;
}
@Override
public Iterable<String> names() {
return this.map.keySet();
}
/**
* Build a request for each part of the origin request.
* @param req Origin request
* @return The requests map that use the part name as a map key
* @throws IOException If fails
*/
private ConcurrentMap<String, List<Request>> buildRequests(
final Request req) throws IOException {
final String header = new RqHeaders.Smart(
new RqHeaders.Base(req)
// @checkstyle MultipleStringLiteralsCheck (1 line)
).single("Content-Type");
if (!header.toLowerCase(Locale.ENGLISH)
.startsWith("multipart/form-data")) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
// @checkstyle LineLength (1 line)
"RqMultipart.Base can only parse multipart/form-data, while Content-Type specifies a different type: \"%s\"",
header
)
);
}
final Matcher matcher = RqMultipart.Base.BOUNDARY.matcher(header);
if (!matcher.matches()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
// @checkstyle LineLength (1 line)
"boundary is not specified in Content-Type header: \"%s\"",
header
)
);
}
if (this.body.read(this.buffer) < 0) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
"failed to read the request body"
);
}
final byte[] boundary = String.format(
"\r\n--%s", matcher.group(1)
).getBytes(StandardCharsets.UTF_8);
this.buffer.flip();
this.buffer.position(boundary.length - 2);
final Collection<Request> requests = new LinkedList<Request>();
while (this.buffer.hasRemaining()) {
final byte data = this.buffer.get();
if (data == '-') {
break;
}
this.buffer.position(this.buffer.position() + 1);
requests.add(this.make(boundary));
}
return RqMultipart.Base.asMap(requests);
}
/**
* Make a request.
* Scans the origin request until the boundary reached. Caches
* the content into a temporary file and returns it as a new request.
* @param boundary Boundary
* @return Request
* @throws IOException If fails
* @todo #254:30min in order to delete temporary files InputStream
* instance on Request.body should be closed. In context of multipart
* requests that means that body of all parts should be closed once
* they are not needed anymore.
*/
private Request make(final byte[] boundary) throws IOException {
final File file = File.createTempFile(
RqMultipart.class.getName(), ".tmp"
);
final FileChannel channel = new RandomAccessFile(
file, "rw"
).getChannel();
try {
channel.write(
ByteBuffer.wrap(
this.head().iterator().next().getBytes(
StandardCharsets.UTF_8
)
)
);
// @checkstyle MultipleStringLiteralsCheck (1 line)
channel.write(ByteBuffer.wrap("\r\n".getBytes()));
this.copy(channel, boundary);
} finally {
channel.close();
}
return new RqWithHeader(
new RqLive(
new TempInputStream(
new FileInputStream(file),
file
)
),
"Content-Length",
String.valueOf(file.length())
);
}
/**
* Copy until boundary reached.
* @param target Output file channel
* @param boundary Boundary
* @throws IOException If fails
* @checkstyle ExecutableStatementCountCheck (2 lines)
*/
private void copy(final WritableByteChannel target,
final byte[] boundary) throws IOException {
int match = 0;
boolean cont = true;
while (cont) {
if (!this.buffer.hasRemaining()) {
this.buffer.clear();
for (int idx = 0; idx < match; ++idx) {
this.buffer.put(boundary[idx]);
}
match = 0;
if (this.body.read(this.buffer) == -1) {
break;
}
this.buffer.flip();
}
final ByteBuffer btarget = this.buffer.slice();
final int offset = this.buffer.position();
btarget.limit(0);
while (this.buffer.hasRemaining()) {
final byte data = this.buffer.get();
if (data == boundary[match]) {
++match;
if (match == boundary.length) {
cont = false;
break;
}
} else {
match = 0;
btarget.limit(this.buffer.position() - offset);
}
}
target.write(btarget);
}
}
/**
* Convert a list of requests to a map.
* @param reqs Requests
* @return Map of them
* @throws IOException If fails
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private static ConcurrentMap<String, List<Request>> asMap(
final Collection<Request> reqs) throws IOException {
final ConcurrentMap<String, List<Request>> map =
new ConcurrentHashMap<String, List<Request>>(reqs.size());
for (final Request req : reqs) {
final String header = new RqHeaders.Smart(
new RqHeaders.Base(req)
// @checkstyle MultipleStringLiteralsCheck (1 line)
).single("Content-Disposition");
final Matcher matcher = RqMultipart.Base.NAME.matcher(header);
if (!matcher.matches()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
// @checkstyle LineLength (1 line)
"\"name\" not found in Content-Disposition header: %s",
header
)
);
}
final String name = matcher.group(1);
map.putIfAbsent(name, new LinkedList<Request>());
map.get(name).add(req);
}
return map;
}
}
/**
* Smart decorator.
* @since 0.15
*/
final class Smart implements RqMultipart {
/**
* Original request.
*/
private final transient RqMultipart origin;
/**
* Ctor.
* @param req Original
*/
public Smart(final RqMultipart req) {
this.origin = req;
}
/**
* Get single part.
* @param name Name of the part to get
* @return Part
* @throws HttpException If fails
*/
public Request single(final CharSequence name) throws HttpException {
final Iterator<Request> parts = this.part(name).iterator();
if (!parts.hasNext()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"form param \"%s\" is mandatory", name
)
);
}
return parts.next();
}
@Override
public Iterable<Request> part(final CharSequence name) {
return this.origin.part(name);
}
@Override
public Iterable<String> names() {
return this.origin.names();
}
@Override
public Iterable<String> head() throws IOException {
return this.origin.head();
}
@Override
public InputStream body() throws IOException {
return this.origin.body();
}
}
/**
* Fake decorator.
* @since 0.16
*/
final class Fake implements RqMultipart {
/**
* Fake boundary constant.
*/
private static final String BOUNDARY = "AaB02x";
/**
* Carriage return constant.
*/
private static final String CRLF = "\r\n";
/**
* Fake multipart request.
*/
private final RqMultipart fake;
/**
* Fake ctor.
* @param req Fake request header holder
* @param dispositions Fake request body parts
* @throws IOException If fails
*/
public Fake(final Request req, final Request... dispositions)
throws IOException {
this.fake = new RqMultipart.Base(
//@checkstyle AnonInnerLength (1 line)
new FakeMultipartRequest(req, dispositions)
);
}
@Override
public Iterable<Request> part(final CharSequence name) {
return this.fake.part(name);
}
@Override
public Iterable<String> names() {
return this.fake.names();
}
@Override
public Iterable<String> head() throws IOException {
return this.fake.head();
}
@Override
public InputStream body() throws IOException {
return this.fake.body();
}
/**
* Fake body stream creator.
* @param dispositions Fake request body parts
* @return InputStream of given dispositions
* @throws IOException If fails
*/
private static InputStream fakeStream(final Request... dispositions)
throws IOException {
final StringBuilder builder = fakeBody(dispositions);
return new ByteArrayInputStream(
builder.toString().getBytes(StandardCharsets.UTF_8)
);
}
/**
* Fake body creator.
* @param dispositions Fake request body parts
* @return StringBuilder of given dispositions
* @throws IOException If fails
*/
@SuppressWarnings("PMD.InsufficientStringBufferDeclaration")
private static StringBuilder fakeBody(final Request... dispositions)
throws IOException {
final StringBuilder builder = new StringBuilder();
for (final Request each : dispositions) {
builder.append(String.format("--%s", Fake.BOUNDARY))
.append(Fake.CRLF)
// @checkstyle MultipleStringLiteralsCheck (1 line)
.append("Content-Disposition: ")
.append(
new RqHeaders.Smart(
new RqHeaders.Base(each)
// @checkstyle MultipleStringLiteralsCheck (1 line)
).single("Content-Disposition")
).append(Fake.CRLF);
final String body = new RqPrint(each).printBody();
if (!(Fake.CRLF.equals(body) || "".equals(body))) {
builder.append(Fake.CRLF).append(body).append(Fake.CRLF);
}
}
builder.append("Content-Transfer-Encoding: utf-8").append(Fake.CRLF)
.append(String.format("--%s--", Fake.BOUNDARY));
return builder;
}
/**
* This class is using a decorator pattern for representing
* a fake HTTP multipart request.
*/
private static class FakeMultipartRequest implements Request {
/**
* Request object. Holds a value for the header.
*/
private final Request req;
/**
* Holding multiple request body parts.
*/
private final Request[] dispositions;
/**
* The Constructor for the class.
* @param req The Request object
* @param dispositions The sequence of dispositions
*/
FakeMultipartRequest(
final Request req, final Request... dispositions
) {
this.req = req;
this.dispositions = dispositions;
}
@Override
public Iterable<String> head() throws IOException {
return new RqWithHeaders(
this.req,
String.format(
"Content-Type: multipart/form-data; boundary=%s",
Fake.BOUNDARY
),
String.format(
"Content-Length: %s",
Fake.fakeBody(this.dispositions).length()
)
).head();
}
@Override
public InputStream body() throws IOException {
return Fake.fakeStream(this.dispositions);
}
}
}
}