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