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    *   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.netty5.handler.codec.http;
17  
18  import io.netty5.channel.ChannelHandlerContext;
19  import io.netty5.channel.ChannelPipeline;
20  
21  /**
22   * Decodes {@link io.netty5.buffer.api.Buffer}s into {@link HttpResponse}s and {@link HttpContent}s.
23   *
24   * <h3>Parameters that prevents excessive memory consumption</h3>
25   * <table border="1">
26   * <tr>
27   * <th>Name</th><th>Meaning</th>
28   * </tr>
29   * <tr>
30   * <td>{@code maxInitialLineLength}</td>
31   * <td>The maximum length of the initial line (e.g. {@code "HTTP/1.0 200 OK"})
32   *     If the length of the initial line exceeds this value, a
33   *     {@link TooLongHttpLineException} will be raised.</td>
34   * </tr>
35   * <tr>
36   * <td>{@code maxHeaderSize}</td>
37   * <td>The maximum length of all headers.  If the sum of the length of each
38   *     header exceeds this value, a {@link TooLongHttpHeaderException} will be raised.</td>
39   * </tr>
40   * <tr>
41   * <td>{@code maxChunkSize}</td>
42   * <td>The maximum length of the content or each chunk.  If the content length
43   *     exceeds this value, the transfer encoding of the decoded response will be
44   *     converted to 'chunked' and the content will be split into multiple
45   *     {@link HttpContent}s.  If the transfer encoding of the HTTP response is
46   *     'chunked' already, each chunk will be split into smaller chunks if the
47   *     length of the chunk exceeds this value.  If you prefer not to handle
48   *     {@link HttpContent}s in your handler, insert {@link HttpObjectAggregator}
49   *     after this decoder in the {@link ChannelPipeline}.</td>
50   * </tr>
51   * </table>
52   *
53   * <h3>Decoding a response for a <tt>HEAD</tt> request</h3>
54   * <p>
55   * Unlike other HTTP requests, the successful response of a <tt>HEAD</tt>
56   * request does not have any content even if there is <tt>Content-Length</tt>
57   * header.  Because {@link HttpResponseDecoder} is not able to determine if the
58   * response currently being decoded is associated with a <tt>HEAD</tt> request,
59   * you must override {@link #isContentAlwaysEmpty(HttpMessage)} to return
60   * <tt>true</tt> for the response of the <tt>HEAD</tt> request.
61   * </p><p>
62   * If you are writing an HTTP client that issues a <tt>HEAD</tt> request,
63   * please use {@link HttpClientCodec} instead of this decoder.  It will perform
64   * additional state management to handle the responses for <tt>HEAD</tt>
65   * requests correctly.
66   * </p>
67   *
68   * <h3>Decoding a response for a <tt>CONNECT</tt> request</h3>
69   * <p>
70   * You also need to do additional state management to handle the response of a
71   * <tt>CONNECT</tt> request properly, like you did for <tt>HEAD</tt>.  One
72   * difference is that the decoder should stop decoding completely after decoding
73   * the successful 200 response since the connection is not an HTTP connection
74   * anymore.
75   * </p><p>
76   * {@link HttpClientCodec} also handles this edge case correctly, so you have to
77   * use {@link HttpClientCodec} if you are writing an HTTP client that issues a
78   * <tt>CONNECT</tt> request.
79   * </p>
80   */
81  public class HttpResponseDecoder extends HttpObjectDecoder {
82  
83      private static final HttpResponseStatus UNKNOWN_STATUS = new HttpResponseStatus(999, "Unknown");
84  
85      /**
86       * Creates a new instance with the default
87       * {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
88       * {@code maxChunkSize (8192)}.
89       */
90      public HttpResponseDecoder() {
91      }
92  
93      /**
94       * Creates a new instance with the specified parameters.
95       */
96      public HttpResponseDecoder(
97              int maxInitialLineLength, int maxHeaderSize) {
98          super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED);
99      }
100 
101     public HttpResponseDecoder(
102             int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders) {
103         super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders);
104     }
105 
106     public HttpResponseDecoder(
107             int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders,
108             int initialBufferSize) {
109         super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders, initialBufferSize);
110     }
111 
112     public HttpResponseDecoder(
113             int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders,
114             int initialBufferSize, boolean allowDuplicateContentLengths) {
115         super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders,
116               initialBufferSize, allowDuplicateContentLengths);
117     }
118 
119     @Override
120     protected HttpMessage createMessage(String[] initialLine) {
121         return new DefaultHttpResponse(
122                 HttpVersion.valueOf(initialLine[0]),
123                 HttpResponseStatus.valueOf(Integer.parseInt(initialLine[1]), initialLine[2]), validateHeaders);
124     }
125 
126     @Override
127     protected HttpMessage createInvalidMessage(ChannelHandlerContext ctx) {
128         return new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, UNKNOWN_STATUS, ctx.bufferAllocator().allocate(0),
129                 validateHeaders);
130     }
131 
132     @Override
133     protected boolean isDecodingRequest() {
134         return false;
135     }
136 }