View Javadoc
1   /*
2    * Copyright 2012 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    *   http://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.buffer.ByteBuf;
19  
20  /**
21   * Shared Static object between HttpMessageDecoder, HttpPostRequestDecoder and HttpPostRequestEncoder
22   */
23  final class HttpPostBodyUtil {
24  
25      public static final int chunkSize = 8096;
26  
27      /**
28       * Default Content-Type in binary form
29       */
30      public static final String DEFAULT_BINARY_CONTENT_TYPE = "application/octet-stream";
31  
32      /**
33       * Default Content-Type in Text form
34       */
35      public static final String DEFAULT_TEXT_CONTENT_TYPE = "text/plain";
36  
37      /**
38       * Allowed mechanism for multipart
39       * mechanism := "7bit"
40                    / "8bit"
41                    / "binary"
42         Not allowed: "quoted-printable"
43                    / "base64"
44       */
45      public enum TransferEncodingMechanism {
46          /**
47           * Default encoding
48           */
49          BIT7("7bit"),
50          /**
51           * Short lines but not in ASCII - no encoding
52           */
53          BIT8("8bit"),
54          /**
55           * Could be long text not in ASCII - no encoding
56           */
57          BINARY("binary");
58  
59          private final String value;
60  
61          TransferEncodingMechanism(String value) {
62              this.value = value;
63          }
64  
65          public String value() {
66              return value;
67          }
68  
69          @Override
70          public String toString() {
71              return value;
72          }
73      }
74  
75      private HttpPostBodyUtil() {
76      }
77  
78      /**
79      * This class intends to decrease the CPU in seeking ahead some bytes in
80      * HttpPostRequestDecoder
81      */
82      static class SeekAheadOptimize {
83          byte[] bytes;
84          int readerIndex;
85          int pos;
86          int origPos;
87          int limit;
88          ByteBuf buffer;
89  
90          /**
91           * @param buffer buffer with a backing byte array
92           */
93          SeekAheadOptimize(ByteBuf buffer) {
94              if (!buffer.hasArray()) {
95                  throw new IllegalArgumentException("buffer hasn't backing byte array");
96              }
97              this.buffer = buffer;
98              bytes = buffer.array();
99              readerIndex = buffer.readerIndex();
100             origPos = pos = buffer.arrayOffset() + readerIndex;
101             limit = buffer.arrayOffset() + buffer.writerIndex();
102         }
103 
104         /**
105         *
106         * @param minus this value will be used as (currentPos - minus) to set
107         * the current readerIndex in the buffer.
108         */
109         void setReadPosition(int minus) {
110             pos -= minus;
111             readerIndex = getReadPosition(pos);
112             buffer.readerIndex(readerIndex);
113         }
114 
115         /**
116         *
117         * @param index raw index of the array (pos in general)
118         * @return the value equivalent of raw index to be used in readerIndex(value)
119         */
120         int getReadPosition(int index) {
121             return index - origPos + readerIndex;
122         }
123     }
124 
125     /**
126      * Find the first non whitespace
127      * @return the rank of the first non whitespace
128      */
129     static int findNonWhitespace(String sb, int offset) {
130         int result;
131         for (result = offset; result < sb.length(); result ++) {
132             if (!Character.isWhitespace(sb.charAt(result))) {
133                 break;
134             }
135         }
136         return result;
137     }
138 
139     /**
140      * Find the end of String
141      * @return the rank of the end of string
142      */
143     static int findEndOfString(String sb) {
144         int result;
145         for (result = sb.length(); result > 0; result --) {
146             if (!Character.isWhitespace(sb.charAt(result - 1))) {
147                 break;
148             }
149         }
150         return result;
151     }
152 
153 }