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;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.ChannelPipeline;
21  import io.netty.handler.codec.TooLongFrameException;
22  
23  
24  /**
25   * Decodes {@link ByteBuf}s into {@link HttpRequest}s and {@link HttpContent}s.
26   *
27   * <h3>Parameters that prevents excessive memory consumption</h3>
28   * <table border="1">
29   * <tr>
30   * <th>Name</th><th>Meaning</th>
31   * </tr>
32   * <tr>
33   * <td>{@code maxInitialLineLength}</td>
34   * <td>The maximum length of the initial line (e.g. {@code "GET / HTTP/1.0"})
35   *     If the length of the initial line exceeds this value, a
36   *     {@link TooLongFrameException} will be raised.</td>
37   * </tr>
38   * <tr>
39   * <td>{@code maxHeaderSize}</td>
40   * <td>The maximum length of all headers.  If the sum of the length of each
41   *     header exceeds this value, a {@link TooLongFrameException} will be raised.</td>
42   * </tr>
43   * <tr>
44   * <td>{@code maxChunkSize}</td>
45   * <td>The maximum length of the content or each chunk.  If the content length
46   *     exceeds this value, the transfer encoding of the decoded request will be
47   *     converted to 'chunked' and the content will be split into multiple
48   *     {@link HttpContent}s.  If the transfer encoding of the HTTP request is
49   *     'chunked' already, each chunk will be split into smaller chunks if the
50   *     length of the chunk exceeds this value.  If you prefer not to handle
51   *     {@link HttpContent}s in your handler, insert {@link HttpObjectAggregator}
52   *     after this decoder in the {@link ChannelPipeline}.</td>
53   * </tr>
54   * </table>
55   */
56  public class HttpRequestDecoder extends HttpObjectDecoder {
57  
58      /**
59       * Creates a new instance with the default
60       * {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
61       * {@code maxChunkSize (8192)}.
62       */
63      public HttpRequestDecoder() {
64      }
65  
66      /**
67       * Creates a new instance with the specified parameters.
68       */
69      public HttpRequestDecoder(
70              int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
71          super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true);
72      }
73  
74      public HttpRequestDecoder(
75              int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders) {
76          super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders);
77      }
78  
79      public HttpRequestDecoder(
80              int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders,
81              int initialBufferSize) {
82          super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize);
83      }
84  
85      @Override
86      protected HttpMessage createMessage(String[] initialLine) throws Exception {
87          return new DefaultHttpRequest(
88                  HttpVersion.valueOf(initialLine[2]),
89                  HttpMethod.valueOf(initialLine[0]), initialLine[1], validateHeaders);
90      }
91  
92      @Override
93      protected HttpMessage createInvalidMessage() {
94          return new DefaultFullHttpRequest(
95                  HttpVersion.HTTP_1_0, HttpMethod.GET, "/bad-request", Unpooled.EMPTY_BUFFER, validateHeaders);
96      }
97  
98      @Override
99      protected boolean isDecodingRequest() {
100         return true;
101     }
102 }