From 602f976e4111d66b807f9b2c1cbf09a8f245cd01 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sun, 19 Aug 2012 19:06:47 +0900 Subject: [PATCH] [#527] Add a new property to HttpMessage to help clarify its transfer encoding - Add an enum: HttpTransferEncoding - consists of SINGLE, STREAMED, and CHUNKED - Add HttpMessage.transferEncoding - replaces is/setChunked() --- .../codec/http/DefaultHttpMessage.java | 48 +++++++---- .../codec/http/DefaultHttpRequest.java | 4 +- .../codec/http/DefaultHttpResponse.java | 4 +- .../codec/http/HttpChunkAggregator.java | 27 +++--- .../handler/codec/http/HttpClientCodec.java | 6 +- .../handler/codec/http/HttpCodecUtil.java | 6 +- .../codec/http/HttpContentDecoder.java | 5 +- .../codec/http/HttpContentEncoder.java | 4 +- .../netty/handler/codec/http/HttpMessage.java | 61 ++++++-------- .../codec/http/HttpMessageDecoder.java | 30 +++---- .../codec/http/HttpMessageEncoder.java | 82 +++++++------------ .../codec/http/HttpTransferEncoding.java | 36 ++++++++ .../handler/codec/spdy/SpdyHttpEncoder.java | 4 +- .../codec/http/HttpChunkAggregatorTest.java | 8 +- .../codec/http/HttpServerCodecTest.java | 5 +- .../http/snoop/HttpSnoopClientHandler.java | 2 +- .../http/snoop/HttpSnoopServerHandler.java | 2 +- 17 files changed, 173 insertions(+), 161 deletions(-) create mode 100644 codec-http/src/main/java/io/netty/handler/codec/http/HttpTransferEncoding.java diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java index 97ed226749..a0697aa12f 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpMessage.java @@ -31,7 +31,7 @@ public class DefaultHttpMessage implements HttpMessage { private final HttpHeaders headers = new HttpHeaders(); private HttpVersion version; private ByteBuf content = Unpooled.EMPTY_BUFFER; - private boolean chunked; + private HttpTransferEncoding te = HttpTransferEncoding.SINGLE; /** * Creates a new instance. @@ -61,19 +61,31 @@ public class DefaultHttpMessage implements HttpMessage { } @Override - public boolean isChunked() { - if (chunked) { - return true; - } else { - return HttpCodecUtil.isTransferEncodingChunked(this); - } + public HttpTransferEncoding getTransferEncoding() { + return te; } @Override - public void setChunked(boolean chunked) { - this.chunked = chunked; - if (chunked) { + public void setTransferEncoding(HttpTransferEncoding te) { + if (te == null) { + throw new NullPointerException("te (transferEncoding)"); + } + this.te = te; + switch (te) { + case SINGLE: + HttpCodecUtil.removeTransferEncodingChunked(this); + break; + case STREAMED: + HttpCodecUtil.removeTransferEncodingChunked(this); setContent(Unpooled.EMPTY_BUFFER); + break; + case CHUNKED: + if (!HttpCodecUtil.isTransferEncodingChunked(this)) { + addHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED); + } + removeHeader(HttpHeaders.Names.CONTENT_LENGTH); + setContent(Unpooled.EMPTY_BUFFER); + break; } } @@ -87,10 +99,12 @@ public class DefaultHttpMessage implements HttpMessage { if (content == null) { content = Unpooled.EMPTY_BUFFER; } - if (content.readable() && isChunked()) { + + if (!getTransferEncoding().isSingle() && content.readable()) { throw new IllegalArgumentException( - "non-empty content disallowed if this.chunked == true"); + "non-empty content disallowed if this.transferEncoding != SINGLE"); } + this.content = content; } @@ -134,7 +148,11 @@ public class DefaultHttpMessage implements HttpMessage { @Override public ByteBuf getContent() { - return content; + if (getTransferEncoding() == HttpTransferEncoding.SINGLE) { + return content; + } else { + return Unpooled.EMPTY_BUFFER; + } } @Override @@ -145,8 +163,8 @@ public class DefaultHttpMessage implements HttpMessage { buf.append(getProtocolVersion().getText()); buf.append(", keepAlive: "); buf.append(HttpHeaders.isKeepAlive(this)); - buf.append(", chunked: "); - buf.append(isChunked()); + buf.append(", transferEncoding: "); + buf.append(getTransferEncoding()); buf.append(')'); buf.append(StringUtil.NEWLINE); appendHeaders(buf); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java index 7f963eed95..afadec21bd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java @@ -68,8 +68,8 @@ public class DefaultHttpRequest extends DefaultHttpMessage implements HttpReques public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getClass().getSimpleName()); - buf.append("(chunked: "); - buf.append(isChunked()); + buf.append("(transferEncoding: "); + buf.append(getTransferEncoding()); buf.append(')'); buf.append(StringUtil.NEWLINE); buf.append(getMethod().toString()); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpResponse.java b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpResponse.java index f56db47e3b..440b34fed0 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpResponse.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpResponse.java @@ -52,8 +52,8 @@ public class DefaultHttpResponse extends DefaultHttpMessage implements HttpRespo public String toString() { StringBuilder buf = new StringBuilder(); buf.append(getClass().getSimpleName()); - buf.append("(chunked: "); - buf.append(isChunked()); + buf.append("(transferEncoding: "); + buf.append(getTransferEncoding()); buf.append(')'); buf.append(StringUtil.NEWLINE); buf.append(getProtocolVersion().getText()); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkAggregator.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkAggregator.java index e6831625fe..e175ffcdb9 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkAggregator.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkAggregator.java @@ -26,7 +26,6 @@ import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.TooLongFrameException; import io.netty.util.CharsetUtil; -import java.util.List; import java.util.Map.Entry; /** @@ -128,29 +127,27 @@ public class HttpChunkAggregator extends MessageToMessageDecoder encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING); - encodings.remove(HttpHeaders.Values.CHUNKED); - if (encodings.isEmpty()) { - m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING); - } - m.setChunked(false); + switch (m.getTransferEncoding()) { + case SINGLE: + this.currentMessage = null; + return m; + case STREAMED: + case CHUNKED: + // A streamed message - initialize the cumulative buffer, and wait for incoming chunks. + m.setTransferEncoding(HttpTransferEncoding.SINGLE); m.setContent(Unpooled.compositeBuffer(maxCumulationBufferComponents)); this.currentMessage = m; return null; - } else { - // Not a chunked message - pass through. - this.currentMessage = null; - return m; + default: + throw new Error(); } } else if (msg instanceof HttpChunk) { // Sanity check if (currentMessage == null) { throw new IllegalStateException( "received " + HttpChunk.class.getSimpleName() + - " without " + HttpMessage.class.getSimpleName()); + " without " + HttpMessage.class.getSimpleName() + + " or last message's transfer encoding was 'SINGLE'"); } // Merge the received chunk into the content of the current message. diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java index 8ee9d10bb9..93c9a7ebf7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java @@ -92,7 +92,7 @@ public class HttpClientCodec extends CombinedChannelHandler { if (failOnMissingResponse) { // check if the request is chunked if so do not increment - if (msg instanceof HttpRequest && !((HttpRequest) msg).isChunked()) { + if (msg instanceof HttpRequest && ((HttpMessage) msg).getTransferEncoding().isSingle()) { requestResponseCounter.incrementAndGet(); } else if (msg instanceof HttpChunk && ((HttpChunk) msg).isLast()) { // increment as its the last chunk @@ -127,8 +127,8 @@ public class HttpClientCodec extends CombinedChannelHandler { return; } - // check if its a HttpMessage and its not chunked - if (msg instanceof HttpMessage && !((HttpMessage) msg).isChunked()) { + // check if it's an HttpMessage and its transfer encoding is SINGLE. + if (msg instanceof HttpMessage && ((HttpMessage) msg).getTransferEncoding().isSingle()) { requestResponseCounter.decrementAndGet(); } else if (msg instanceof HttpChunk && ((HttpChunk) msg).isLast()) { requestResponseCounter.decrementAndGet(); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpCodecUtil.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpCodecUtil.java index d2a7b1dc00..a9021db7d8 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpCodecUtil.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpCodecUtil.java @@ -154,7 +154,11 @@ final class HttpCodecUtil { static void removeTransferEncodingChunked(HttpMessage m) { List values = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING); values.remove(HttpHeaders.Values.CHUNKED); - m.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, values); + if (values.isEmpty()) { + m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING); + } else { + m.setHeader(HttpHeaders.Names.TRANSFER_ENCODING, values); + } } static boolean isContentLengthSet(HttpMessage m) { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java index 87aa20caef..7b7ec71a08 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpContentDecoder.java @@ -73,7 +73,8 @@ public abstract class HttpContentDecoder extends MessageToMessageDecoder - * Please note that this method will keep returning {@code true} if the - * {@code "Transfer-Encoding"} of this message is {@code "chunked"}, even if - * you attempt to override this property by calling {@link #setChunked(boolean)} - * with {@code false}. - *

- * - * @return True if this message is chunked, otherwise false + * Returns the transfer encoding of this {@link HttpMessage}. + *
    + *
  • {@link HttpTransferEncoding#CHUNKED} - an HTTP message whose {@code "Transfer-Encoding"} + * is {@code "chunked"}.
  • + *
  • {@link HttpTransferEncoding#STREAMED} - an HTTP message which is not chunked, but + * is followed by {@link HttpChunk}s that represent its content. {@link #getContent()} + * returns an empty buffer.
  • + *
  • {@link HttpTransferEncoding#SINGLE} - a self-contained HTTP message which is not chunked + * and {@link #getContent()} returns the full content.
  • + *
*/ - boolean isChunked(); + HttpTransferEncoding getTransferEncoding(); /** - * Sets the boolean defining if this {@link HttpMessage} is chunked. - * - *

- * If this is set to true, it means that this initial {@link HttpMessage} - * does not contain any content - The content is contained by multiple - * {@link HttpChunk}s, which are generated by the {@link HttpMessageDecoder} - * consecutively. - * - * Because of this, the content of this {@link HttpMessage} becomes - * {@link Unpooled#EMPTY_BUFFER} - *

- * - *

- * Even if this method is called with {@code false}, {@link #isChunked()} - * will keep returning {@code true} if the {@code "Transfer-Encoding"} of - * this message is {@code "chunked"}. - *

- * - * @param chunked True if this message is to be delivered in chunks, - * otherwise false. + * Sets the transfer encoding of this {@link HttpMessage}. + *
    + *
  • If set to {@link HttpTransferEncoding#CHUNKED}, the {@code "Transfer-Encoding: chunked"} + * header is set and the {@code "Content-Length"} header and the content of this message are + * removed automatically.
  • + *
  • If set to {@link HttpTransferEncoding#STREAMED}, the {@code "Transfer-Encoding: chunked"} + * header and the content of this message are removed automatically.
  • + *
  • If set to {@link HttpTransferEncoding#SINGLE}, the {@code "Transfer-Encoding: chunked"} + * header is removed automatically.
  • + *
+ * For more information about what {@link HttpTransferEncoding} means, see {@link #getTransferEncoding()}. */ - void setChunked(boolean chunked); + void setTransferEncoding(HttpTransferEncoding te); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageDecoder.java index 389110f532..13ff6ad8f8 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageDecoder.java @@ -191,15 +191,10 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder maxChunkSize || HttpHeaders.is100ContinueExpected(message)) { // Generate HttpMessage first. HttpChunks will follow. checkpoint(State.READ_FIXED_LENGTH_CONTENT_AS_CHUNKS); - message.setChunked(true); + message.setTransferEncoding(HttpTransferEncoding.STREAMED); // chunkSize will be decreased as the READ_FIXED_LENGTH_CONTENT_AS_CHUNKS // state reads data chunk by chunk. chunkSize = HttpHeaders.getContentLength(message, -1); @@ -224,7 +219,7 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder maxChunkSize || HttpHeaders.is100ContinueExpected(message)) { // Generate HttpMessage first. HttpChunks will follow. checkpoint(State.READ_VARIABLE_LENGTH_CONTENT_AS_CHUNKS); - message.setChunked(true); + message.setTransferEncoding(HttpTransferEncoding.STREAMED); return message; } break; @@ -240,8 +235,9 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder maxChunkSize) { toRead = maxChunkSize; } - if (!message.isChunked()) { - message.setChunked(true); + + if (message.getTransferEncoding() != HttpTransferEncoding.STREAMED) { + message.setTransferEncoding(HttpTransferEncoding.STREAMED); return new Object[] {message, new DefaultHttpChunk(buffer.readBytes(toRead))}; } else { return new DefaultHttpChunk(buffer.readBytes(toRead)); @@ -464,8 +460,8 @@ public abstract class HttpMessageDecoder extends ReplayingDecoder= 0) { nextState = State.READ_FIXED_LENGTH_CONTENT; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageEncoder.java index c8d86c669e..8772baf1a3 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpMessageEncoder.java @@ -21,11 +21,8 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.UnsupportedMessageTypeException; -import io.netty.handler.codec.http.HttpHeaders.Names; -import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.util.CharsetUtil; -import java.io.UnsupportedEncodingException; import java.util.Map; /** @@ -47,7 +44,7 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder { private static final ByteBuf LAST_CHUNK = copiedBuffer("0\r\n\r\n", CharsetUtil.US_ASCII); - private boolean transferEncodingChunked; + private HttpTransferEncoding lastTE; /** * Creates a new instance. @@ -64,26 +61,14 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder { public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception { if (msg instanceof HttpMessage) { HttpMessage m = (HttpMessage) msg; - boolean contentMustBeEmpty; - if (m.isChunked()) { - // if Content-Length is set then the message can't be HTTP chunked - if (HttpCodecUtil.isContentLengthSet(m)) { - contentMustBeEmpty = false; - transferEncodingChunked = false; - HttpCodecUtil.removeTransferEncodingChunked(m); - } else { - // check if the Transfer-Encoding is set to chunked already. - // if not add the header to the message - if (!HttpCodecUtil.isTransferEncodingChunked(m)) { - m.addHeader(Names.TRANSFER_ENCODING, Values.CHUNKED); - } - contentMustBeEmpty = true; - transferEncodingChunked = true; - } - } else { - transferEncodingChunked = contentMustBeEmpty = HttpCodecUtil.isTransferEncodingChunked(m); - } + HttpTransferEncoding te = m.getTransferEncoding(); + lastTE = te; + // Calling setTransferEncoding() will sanitize the headers and the content. + // For example, it will remove the cases such as 'Transfer-Encoding' and 'Content-Length' + // coexist. It also removes the content if the transferEncoding is not SINGLE. + m.setTransferEncoding(te); + // Encode the message. out.markWriterIndex(); encodeInitialLine(out, m); encodeHeaders(out, m); @@ -91,20 +76,25 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder { out.writeByte(LF); ByteBuf content = m.getContent(); - if (content.readable()) { - if (contentMustBeEmpty) { - out.resetWriterIndex(); - throw new IllegalArgumentException( - "HttpMessage.content must be empty if Transfer-Encoding is chunked."); - } else { - out.writeBytes(content, content.readerIndex(), content.readableBytes()); - } - } + out.writeBytes(content, content.readerIndex(), content.readableBytes()); } else if (msg instanceof HttpChunk) { HttpChunk chunk = (HttpChunk) msg; - if (transferEncodingChunked) { + HttpTransferEncoding te = lastTE; + if (te == null) { + throw new IllegalArgumentException("HttpChunk must follow an HttpMessage."); + } + + switch (te) { + case SINGLE: + throw new IllegalArgumentException( + "The transfer encoding of the last encoded HttpMessage is SINGLE."); + case STREAMED: { + ByteBuf content = chunk.getContent(); + out.writeBytes(content, content.readerIndex(), content.readableBytes()); + break; + } + case CHUNKED: if (chunk.isLast()) { - transferEncodingChunked = false; if (chunk instanceof HttpChunkTrailer) { out.writeByte((byte) '0'); out.writeByte(CR); @@ -125,11 +115,6 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder { out.writeByte(CR); out.writeByte(LF); } - } else { - if (!chunk.isLast()) { - ByteBuf chunkContent = chunk.getContent(); - out.writeBytes(chunkContent, chunkContent.readerIndex(), chunkContent.readableBytes()); - } } } else { throw new UnsupportedMessageTypeException(msg, HttpMessage.class, HttpChunk.class); @@ -137,27 +122,18 @@ public abstract class HttpMessageEncoder extends MessageToByteEncoder { } private static void encodeHeaders(ByteBuf buf, HttpMessage message) { - try { - for (Map.Entry h: message.getHeaders()) { - encodeHeader(buf, h.getKey(), h.getValue()); - } - } catch (UnsupportedEncodingException e) { - throw (Error) new Error().initCause(e); + for (Map.Entry h: message.getHeaders()) { + encodeHeader(buf, h.getKey(), h.getValue()); } } private static void encodeTrailingHeaders(ByteBuf buf, HttpChunkTrailer trailer) { - try { - for (Map.Entry h: trailer.getHeaders()) { - encodeHeader(buf, h.getKey(), h.getValue()); - } - } catch (UnsupportedEncodingException e) { - throw (Error) new Error().initCause(e); + for (Map.Entry h: trailer.getHeaders()) { + encodeHeader(buf, h.getKey(), h.getValue()); } } - private static void encodeHeader(ByteBuf buf, String header, String value) - throws UnsupportedEncodingException { + private static void encodeHeader(ByteBuf buf, String header, String value) { buf.writeBytes(header.getBytes(CharsetUtil.US_ASCII)); buf.writeByte(COLON); buf.writeByte(SP); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpTransferEncoding.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpTransferEncoding.java new file mode 100644 index 0000000000..e499396bd2 --- /dev/null +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpTransferEncoding.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.handler.codec.http; + +public enum HttpTransferEncoding { + CHUNKED(false), + STREAMED(false), + SINGLE(true); + + private final boolean single; + + private HttpTransferEncoding(boolean single) { + this.single = single; + } + + public boolean isMultiple() { + return !single; + } + + public boolean isSingle() { + return single; + } +} diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java index 22816e0769..c413c09cbf 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpEncoder.java @@ -218,7 +218,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { private SpdySynStreamFrame createSynStreamFrame(HttpMessage httpMessage) throws Exception { - boolean chunked = httpMessage.isChunked(); + boolean chunked = httpMessage.getTransferEncoding().isMultiple(); // Get the Stream-ID, Associated-To-Stream-ID, Priority, URL, and scheme from the headers int streamID = SpdyHttpHeaders.getStreamId(httpMessage); @@ -286,7 +286,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder { private SpdySynReplyFrame createSynReplyFrame(HttpResponse httpResponse) throws Exception { - boolean chunked = httpResponse.isChunked(); + boolean chunked = httpResponse.getTransferEncoding().isMultiple(); // Get the Stream-ID from the headers int streamID = SpdyHttpHeaders.getStreamId(httpResponse); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkAggregatorTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkAggregatorTest.java index 20e81b5f2a..b4540af3e1 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkAggregatorTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpChunkAggregatorTest.java @@ -38,7 +38,7 @@ public class HttpChunkAggregatorTest { HttpMessage message = new DefaultHttpMessage(HttpVersion.HTTP_1_1); HttpHeaders.setHeader(message, "X-Test", true); - message.setChunked(true); + message.setTransferEncoding(HttpTransferEncoding.STREAMED); HttpChunk chunk1 = new DefaultHttpChunk(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpChunk chunk2 = new DefaultHttpChunk(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpChunk chunk3 = new DefaultHttpChunk(Unpooled.EMPTY_BUFFER); @@ -59,7 +59,7 @@ public class HttpChunkAggregatorTest { } - private void checkContentBuffer(HttpMessage aggregatedMessage) { + private static void checkContentBuffer(HttpMessage aggregatedMessage) { CompositeByteBuf buffer = (CompositeByteBuf) aggregatedMessage.getContent(); assertEquals(2, buffer.numComponents()); List buffers = buffer.decompose(0, buffer.capacity()); @@ -76,7 +76,7 @@ public class HttpChunkAggregatorTest { EmbeddedMessageChannel embedder = new EmbeddedMessageChannel(aggr); HttpMessage message = new DefaultHttpMessage(HttpVersion.HTTP_1_1); HttpHeaders.setHeader(message, "X-Test", true); - message.setChunked(true); + message.setTransferEncoding(HttpTransferEncoding.CHUNKED); HttpChunk chunk1 = new DefaultHttpChunk(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpChunk chunk2 = new DefaultHttpChunk(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpChunkTrailer trailer = new DefaultHttpChunkTrailer(); @@ -107,7 +107,7 @@ public class HttpChunkAggregatorTest { HttpChunkAggregator aggr = new HttpChunkAggregator(4); EmbeddedMessageChannel embedder = new EmbeddedMessageChannel(aggr); HttpMessage message = new DefaultHttpMessage(HttpVersion.HTTP_1_1); - message.setChunked(true); + message.setTransferEncoding(HttpTransferEncoding.STREAMED); HttpChunk chunk1 = new DefaultHttpChunk(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpChunk chunk2 = new DefaultHttpChunk(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); assertFalse(embedder.writeInbound(message)); diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java index 2b627d431a..6ffcb87a5a 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpServerCodecTest.java @@ -24,7 +24,7 @@ import org.junit.Assert; import org.junit.Test; public class HttpServerCodecTest { - + /** * Testcase for https://github.com/netty/netty/issues/433 */ @@ -45,8 +45,7 @@ public class HttpServerCodecTest { decoderEmbedder.finish(); HttpMessage httpMessage = (HttpMessage) decoderEmbedder.readInbound(); - Assert.assertTrue(httpMessage.isChunked()); - + Assert.assertSame(HttpTransferEncoding.STREAMED, httpMessage.getTransferEncoding()); boolean empty = true; int totalBytesPolled = 0; diff --git a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopClientHandler.java b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopClientHandler.java index c5ceb55320..030b01be76 100644 --- a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopClientHandler.java +++ b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopClientHandler.java @@ -44,7 +44,7 @@ public class HttpSnoopClientHandler extends ChannelInboundMessageHandlerAdapter< System.out.println(); } - if (response.isChunked()) { + if (response.getTransferEncoding().isMultiple()) { readingChunks = true; System.out.println("CHUNKED CONTENT {"); } else { diff --git a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java index 2d99d18f95..1adad5709d 100644 --- a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java +++ b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java @@ -84,7 +84,7 @@ public class HttpSnoopServerHandler extends ChannelInboundMessageHandlerAdapter< buf.append("\r\n"); } - if (request.isChunked()) { + if (request.getTransferEncoding().isMultiple()) { readingChunks = true; } else { ByteBuf content = request.getContent();