GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Clarify documentation on max HTTP header size (#14672) · netty/netty@4f95cae

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/http
      • HttpDecoderConfig.java
      • HttpRequestDecoder.java
      • HttpResponseDecoder.java
    • test/java/io/netty/handler/codec/http
      • HttpRequestDecoderTest.java
      • HttpResponseDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpDecoderConfig.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpDecoderConfig.java
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public int getMaxHeaderSize() {
7676
/**
7777
* Set the maximum line length of header lines.
7878
* This limits how much memory Netty will use when parsing HTTP header key-value pairs.
79+
* The limit applies to the sum of all the headers, so it applies equally to many short header-lines,
80+
* or fewer but longer header lines.
81+
* <p>
7982
* You would typically set this to the same value as {@link #setMaxInitialLineLength(int)}.
8083
*
8184
* @param maxHeaderSize The maximum length, in bytes.
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,22 @@ public class HttpRequestDecoder extends HttpObjectDecoder {
126126
* Creates a new instance with the default
127127
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
128128
* {@code maxChunkSize (8192)}.
129+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
130+
* the configuration parameters.
129131
*/
130132
public HttpRequestDecoder() {
131133
}
132134

133135
/**
134136
* Creates a new instance with the specified parameters.
137+
*
138+
* @param maxInitialLineLength the initial size of the temporary buffer used when parsing the lines of the
139+
* HTTP headers.
140+
* @param maxHeaderSize the maximum permitted combined size of all headers in any one request.
141+
* @param maxChunkSize The maximum amount of data that the decoder will buffer
142+
* before sending chunks down the pipeline.
143+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
144+
* the configuration parameters.
135145
*/
136146
public HttpRequestDecoder(
137147
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
@@ -144,6 +154,8 @@ public HttpRequestDecoder(
144154
/**
145155
* @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor,
146156
* to always have header validation enabled.
157+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
158+
* the configuration parameters.
147159
*/
148160
@Deprecated
149161
public HttpRequestDecoder(
@@ -154,6 +166,8 @@ public HttpRequestDecoder(
154166
/**
155167
* @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor,
156168
* to always have header validation enabled.
169+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
170+
* the configuration parameters.
157171
*/
158172
@Deprecated
159173
public HttpRequestDecoder(
@@ -166,6 +180,8 @@ public HttpRequestDecoder(
166180
/**
167181
* @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor,
168182
* to always have header validation enabled.
183+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
184+
* the configuration parameters.
169185
*/
170186
@Deprecated
171187
public HttpRequestDecoder(
@@ -178,6 +194,8 @@ public HttpRequestDecoder(
178194
/**
179195
* @deprecated Prefer the {@link #HttpRequestDecoder(HttpDecoderConfig)} constructor,
180196
* to always have header validation enabled.
197+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
198+
* the configuration parameters.
181199
*/
182200
@Deprecated
183201
public HttpRequestDecoder(
@@ -189,6 +207,8 @@ public HttpRequestDecoder(
189207

190208
/**
191209
* Creates a new instance with the specified configuration.
210+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
211+
* the configuration parameters.
192212
*/
193213
public HttpRequestDecoder(HttpDecoderConfig config) {
194214
super(config);
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,22 @@ public class HttpResponseDecoder extends HttpObjectDecoder {
124124
* Creates a new instance with the default
125125
* {@code maxInitialLineLength (4096)}, {@code maxHeaderSize (8192)}, and
126126
* {@code maxChunkSize (8192)}.
127+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
128+
* the configuration parameters.
127129
*/
128130
public HttpResponseDecoder() {
129131
}
130132

131133
/**
132134
* Creates a new instance with the specified parameters.
135+
*
136+
* @param maxInitialLineLength the initial size of the temporary buffer used when parsing the lines of the
137+
* HTTP headers.
138+
* @param maxHeaderSize the maximum permitted combined size of all headers in any one response.
139+
* @param maxChunkSize The maximum amount of data that the decoder will buffer
140+
* before sending chunks down the pipeline.
141+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
142+
* the configuration parameters.
133143
*/
134144
public HttpResponseDecoder(
135145
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
@@ -141,6 +151,8 @@ public HttpResponseDecoder(
141151

142152
/**
143153
* @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor.
154+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
155+
* the configuration parameters.
144156
*/
145157
@Deprecated
146158
public HttpResponseDecoder(
@@ -150,6 +162,8 @@ public HttpResponseDecoder(
150162

151163
/**
152164
* @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor.
165+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
166+
* the configuration parameters.
153167
*/
154168
@Deprecated
155169
public HttpResponseDecoder(
@@ -161,6 +175,8 @@ public HttpResponseDecoder(
161175

162176
/**
163177
* @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor.
178+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
179+
* the configuration parameters.
164180
*/
165181
@Deprecated
166182
public HttpResponseDecoder(
@@ -172,6 +188,8 @@ public HttpResponseDecoder(
172188

173189
/**
174190
* @deprecated Prefer the {@link #HttpResponseDecoder(HttpDecoderConfig)} constructor.
191+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
192+
* the configuration parameters.
175193
*/
176194
@Deprecated
177195
public HttpResponseDecoder(
@@ -183,6 +201,8 @@ public HttpResponseDecoder(
183201

184202
/**
185203
* Creates a new instance with the specified configuration.
204+
* @see HttpDecoderConfig HttpDecoderConfig API documentation for detailed descriptions of
205+
* the configuration parameters.
186206
*/
187207
public HttpResponseDecoder(HttpDecoderConfig config) {
188208
super(config);
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,33 @@ public void testTooLargeHeaders() {
381381
assertFalse(channel.finish());
382382
}
383383

384+
@Test
385+
void testTotalHeaderLimit() throws Exception {
386+
String requestStr = "GET /some/path HTTP/1.1\r\n" +
387+
"Host: a.b\r\n" + // 9 content bytes
388+
"a1: b\r\n" + // + 5 = 14 bytes,
389+
"a2: b\r\n\r\n"; // + 5 = 19 bytes
390+
391+
// Decoding with a max header size of 18 bytes must fail:
392+
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder(1024, 18, 1024));
393+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
394+
HttpRequest request = channel.readInbound();
395+
assertTrue(request.decoderResult().isFailure());
396+
assertInstanceOf(TooLongHttpHeaderException.class, request.decoderResult().cause());
397+
assertFalse(channel.finish());
398+
399+
// Decoding with a max header size of 19 must pass:
400+
channel = new EmbeddedChannel(new HttpRequestDecoder(1024, 19, 1024));
401+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
402+
request = channel.readInbound();
403+
assertTrue(request.decoderResult().isSuccess());
404+
assertEquals("a.b", request.headers().get("Host"));
405+
assertEquals("b", request.headers().get("a1"));
406+
assertEquals("b", request.headers().get("a2"));
407+
assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, channel.readInbound());
408+
assertFalse(channel.finish());
409+
}
410+
384411
@Test
385412
public void testHeaderNameStartsWithControlChar1c() {
386413
testHeaderNameStartsWithControlChar(0x1c);
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseDecoderTest.java
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4141
import static org.junit.jupiter.api.Assertions.assertEquals;
4242
import static org.junit.jupiter.api.Assertions.assertFalse;
43+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
4344
import static org.junit.jupiter.api.Assertions.assertNull;
4445
import static org.junit.jupiter.api.Assertions.assertTrue;
4546

@@ -108,6 +109,34 @@ public void testMaxHeaderSize2() {
108109
assertNull(ch.readInbound());
109110
}
110111

112+
@Test
113+
void testTotalHeaderLimit() throws Exception {
114+
String requestStr = "HTTP/1.1 200 OK\r\n" +
115+
"Server: X\r\n" + // 9 content bytes
116+
"a1: b\r\n" + // + 5 = 14 bytes,
117+
"a2: b\r\n\r\n"; // + 5 = 19 bytes
118+
119+
// Decoding with a max header size of 18 bytes must fail:
120+
EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseDecoder(1024, 18, 1024));
121+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
122+
HttpResponse response = channel.readInbound();
123+
assertTrue(response.decoderResult().isFailure());
124+
assertInstanceOf(TooLongHttpHeaderException.class, response.decoderResult().cause());
125+
assertFalse(channel.finish());
126+
127+
// Decoding with a max header size of 19 must pass:
128+
channel = new EmbeddedChannel(new HttpResponseDecoder(1024, 19, 1024));
129+
assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
130+
response = channel.readInbound();
131+
assertTrue(response.decoderResult().isSuccess());
132+
assertEquals("X", response.headers().get("Server"));
133+
assertEquals("b", response.headers().get("a1"));
134+
assertEquals("b", response.headers().get("a2"));
135+
channel.close();
136+
assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, channel.readInbound());
137+
assertFalse(channel.finish());
138+
}
139+
111140
@Test
112141
public void testResponseChunked() {
113142
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.