|
17 | 17 |
|
18 | 18 | import io.netty.buffer.Unpooled;
|
19 | 19 | import io.netty.channel.embedded.EmbeddedChannel;
|
| 20 | +import io.netty.handler.codec.TooLongFrameException; |
20 | 21 | import io.netty.util.CharsetUtil;
|
21 | 22 | import org.junit.Test;
|
22 | 23 |
|
| 24 | +import java.util.Arrays; |
23 | 25 | import java.util.List;
|
24 | 26 |
|
25 | 27 | import static org.hamcrest.CoreMatchers.*;
|
26 | 28 | import static org.junit.Assert.*;
|
27 | 29 |
|
28 | 30 | public class HttpResponseDecoderTest {
|
29 | 31 |
|
| 32 | + /** |
| 33 | + * The size of headers should be calculated correctly even if a single header is split into multiple fragments. |
| 34 | + * @see <a href="https://github.com/netty/netty/issues/3445">#3445</a> |
| 35 | + */ |
| 36 | + @Test |
| 37 | + public void testMaxHeaderSize1() { |
| 38 | + final int maxHeaderSize = 8192; |
| 39 | + |
| 40 | + final EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(4096, maxHeaderSize, 8192)); |
| 41 | + final char[] bytes = new char[maxHeaderSize / 2 - 2]; |
| 42 | + Arrays.fill(bytes, 'a'); |
| 43 | + |
| 44 | + ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n", CharsetUtil.US_ASCII)); |
| 45 | + |
| 46 | + // Write two 4096-byte headers (= 8192 bytes) |
| 47 | + ch.writeInbound(Unpooled.copiedBuffer("A:", CharsetUtil.US_ASCII)); |
| 48 | + ch.writeInbound(Unpooled.copiedBuffer(bytes, CharsetUtil.US_ASCII)); |
| 49 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 50 | + assertNull(ch.readInbound()); |
| 51 | + ch.writeInbound(Unpooled.copiedBuffer("B:", CharsetUtil.US_ASCII)); |
| 52 | + ch.writeInbound(Unpooled.copiedBuffer(bytes, CharsetUtil.US_ASCII)); |
| 53 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 54 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 55 | + |
| 56 | + HttpResponse res = ch.readInbound(); |
| 57 | + assertNull(res.decoderResult().cause()); |
| 58 | + assertTrue(res.decoderResult().isSuccess()); |
| 59 | + |
| 60 | + assertNull(ch.readInbound()); |
| 61 | + assertTrue(ch.finish()); |
| 62 | + assertThat(ch.readInbound(), instanceOf(LastHttpContent.class)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Complementary test case of {@link #testMaxHeaderSize1()} When it actually exceeds the maximum, it should fail. |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void testMaxHeaderSize2() { |
| 70 | + final int maxHeaderSize = 8192; |
| 71 | + |
| 72 | + final EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder(4096, maxHeaderSize, 8192)); |
| 73 | + final char[] bytes = new char[maxHeaderSize / 2 - 2]; |
| 74 | + Arrays.fill(bytes, 'a'); |
| 75 | + |
| 76 | + ch.writeInbound(Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\n", CharsetUtil.US_ASCII)); |
| 77 | + |
| 78 | + // Write a 4096-byte header and a 4097-byte header to test an off-by-one case (= 8193 bytes) |
| 79 | + ch.writeInbound(Unpooled.copiedBuffer("A:", CharsetUtil.US_ASCII)); |
| 80 | + ch.writeInbound(Unpooled.copiedBuffer(bytes, CharsetUtil.US_ASCII)); |
| 81 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 82 | + assertNull(ch.readInbound()); |
| 83 | + ch.writeInbound(Unpooled.copiedBuffer("B: ", CharsetUtil.US_ASCII)); // Note an extra space. |
| 84 | + ch.writeInbound(Unpooled.copiedBuffer(bytes, CharsetUtil.US_ASCII)); |
| 85 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 86 | + ch.writeInbound(Unpooled.copiedBuffer("\r\n", CharsetUtil.US_ASCII)); |
| 87 | + |
| 88 | + HttpResponse res = ch.readInbound(); |
| 89 | + assertTrue(res.decoderResult().cause() instanceof TooLongFrameException); |
| 90 | + |
| 91 | + assertFalse(ch.finish()); |
| 92 | + assertNull(ch.readInbound()); |
| 93 | + } |
| 94 | + |
30 | 95 | @Test
|
31 | 96 | public void testResponseChunked() {
|
32 | 97 | EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
|
|
0 commit comments