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