View Javadoc
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.handler.codec.http.HttpHeaderValidationUtil;
20  import io.netty.util.internal.ObjectUtil;
21  
22  final class FileUploadUtil {
23  
24      private FileUploadUtil() { }
25  
26      static int hashCode(FileUpload upload) {
27          return upload.getName().hashCode();
28      }
29  
30      static boolean equals(FileUpload upload1, FileUpload upload2) {
31          return upload1.getName().equalsIgnoreCase(upload2.getName());
32      }
33  
34      static int compareTo(FileUpload upload1, FileUpload upload2) {
35          return upload1.getName().compareToIgnoreCase(upload2.getName());
36      }
37  
38      /**
39       * Control characters, the DEL character, double-quote, and backslash are either disallowed or strongly discouraged,
40       * depending on which {@code multipart/form-data} specification you read.
41       * This method conservatively rejects all of them, and is used for <em>outbound</em> (encoding) filenames.
42       * @param filename The filename to check.
43       * @return The validated filename, unchanged.
44       */
45      static String validateFileNameForMultiPart(String filename) {
46          int length = ObjectUtil.checkNotNull(filename, "filename").length();
47          for (int i = 0; i < length; i++) {
48              char c = filename.charAt(i);
49              if (c < HttpConstants.SP /*control character block*/ || c == HttpConstants.DEL ||
50                  c == HttpConstants.DOUBLE_QUOTE || c == HttpConstants.BACKSLASH) {
51                  throw new IllegalArgumentException(
52                      String.format("Illegal filename character 0x%02x at index %d", (int) c, i));
53              }
54          }
55          return filename;
56      }
57  
58      /**
59       * Control characters and the DEL character are either disallowed or strongly discouraged in the
60       * {@code Content-Type}.
61       * Space and tab is allowed except for the first character, quotation and other special characters are allowed.
62       * <p>
63       * The Content-Type value MUST be structured in the form of {@code type/subtype parameters}.
64       * See <a href="https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3.1">RFC 9110 §8.3.1</a>,
65       * and <a href="https://www.rfc-editor.org/info/rfc2045/#section-5.1">RFC 2045 §5.1</a>.
66       * <p>
67       * This is used for multipart <em>encoding</em>, so we refuse to emit a malformed content type.
68       *
69       * @param contentType The Content-Type header value to check.
70       * @return The valid Content-Type header value, unchanged.
71       */
72      static String validateContentTypeForMultiPart(String contentType) {
73          checkContentTypeForMultiPart(
74              ObjectUtil.checkNotNull(contentType, "contentType"), true);
75          return contentType;
76      }
77  
78      /**
79       * Determine if the given content type value is syntactically valid and well-formed.
80       * <p>
81       * The Content-Type value is valid if it is structured in the form of {@code type/subtype parameters}.
82       * See <a href="https://www.rfc-editor.org/rfc/rfc9110.html#section-8.3.1">RFC 9110 §8.3.1</a>,
83       * and <a href="https://www.rfc-editor.org/info/rfc2045/#section-5.1">RFC 2045 §5.1</a>.
84       * <p>
85       * This is used for multipart <em>decoding</em>, where
86       * <a href="https://www.rfc-editor.org/info/rfc2045/#section-5.2">RFC 2045 §5.2</a>
87       * recommends that syntactically invalid content type values be replaced with a default.
88       *
89       * @param contentType The Content-Type header value to check.
90       * @return {@code true} if the Content-Type header value is valid, otherwise {@code false}.
91       */
92      static boolean isContentTypeForMultiPartValid(String contentType) {
93          return contentType != null && checkContentTypeForMultiPart(contentType, false);
94      }
95  
96      private static boolean checkContentTypeForMultiPart(String contentType, boolean throwIfInvalid) {
97          int index = HttpHeaderValidationUtil.validateValidHeaderValue(contentType);
98          if (index != -1) {
99              if (throwIfInvalid) {
100                 throw new IllegalArgumentException(
101                     String.format("Illegal Content-Type character 0x%02x at index %d",
102                         (int) contentType.charAt(index), index));
103             }
104             return false;
105         }
106 
107         int slash = contentType.indexOf('/');
108         int semicolon = contentType.indexOf(';');
109         if (slash == -1 ||
110             slash == 0 ||
111             (semicolon == -1 && slash == contentType.length() - 1) ||
112             (semicolon != -1 && slash + 1 >= semicolon)) {
113             if (throwIfInvalid) {
114                 throw new IllegalArgumentException("Malformed content type value: " +
115                     "index of '/' is " + slash + " content type length " + contentType.length());
116             }
117             return false;
118         }
119         return true;
120     }
121 }