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