1 /*
2 * Copyright 2016 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.handler.codec.http.multipart;
17
18 import io.netty.handler.codec.http.HttpConstants;
19 import io.netty.util.internal.ObjectUtil;
20
21 final class FileUploadUtil {
22
23 private FileUploadUtil() { }
24
25 static int hashCode(FileUpload upload) {
26 return upload.getName().hashCode();
27 }
28
29 static boolean equals(FileUpload upload1, FileUpload upload2) {
30 return upload1.getName().equalsIgnoreCase(upload2.getName());
31 }
32
33 static int compareTo(FileUpload upload1, FileUpload upload2) {
34 return upload1.getName().compareToIgnoreCase(upload2.getName());
35 }
36
37 /**
38 * Control characters, the DEL character, double-quote, and backslash are either disallowed or strongly discouraged,
39 * depending on which {@code multipart/form-data} specification you read.
40 * This method conservatively rejects all of them, and is used for <em>outbound</em> (encoding) filenames.
41 * @param filename The filename to check.
42 * @return The validated filename, unchanged.
43 */
44 static String validateFileNameForMultiPart(String filename) {
45 int length = ObjectUtil.checkNotNull(filename, "filename").length();
46 for (int i = 0; i < length; i++) {
47 char c = filename.charAt(i);
48 if (c < HttpConstants.SP /*control character block*/ || c == HttpConstants.DEL ||
49 c == HttpConstants.DOUBLE_QUOTE || c == HttpConstants.BACKSLASH) {
50 throw new IllegalArgumentException(
51 String.format("Illegal filename character 0x%02x at index %d", (int) c, i));
52 }
53 }
54 return filename;
55 }
56 }