查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
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 org.jboss.netty.handler.codec.http;
17  
18  import org.jboss.netty.buffer.ChannelBuffer;
19  import org.jboss.netty.channel.ChannelPipeline;
20  import org.jboss.netty.handler.codec.frame.TooLongFrameException;
21  
22  
23  /**
24   * Decodes {@link ChannelBuffer}s into {@link HttpRequest}s and {@link HttpChunk}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 HttpChunk}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 HttpChunk}s in your handler, insert {@link HttpChunkAggregator}
51   *     after this decoder in the {@link ChannelPipeline}.</td>
52   * </tr>
53   * </table>
54   */
55  public class HttpRequestDecoder extends HttpMessageDecoder {
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);
71      }
72  
73      @Override
74      protected HttpMessage createMessage(String[] initialLine) throws Exception {
75          return new DefaultHttpRequest(
76                  HttpVersion.valueOf(initialLine[2]), HttpMethod.valueOf(initialLine[0]), initialLine[1]);
77      }
78  
79      @Override
80      protected boolean isDecodingRequest() {
81          return true;
82      }
83  }