2009-03-10 09:42:19 +01:00
|
|
|
/*
|
2011-12-09 06:18:34 +01:00
|
|
|
* Copyright 2011 The Netty Project
|
2009-03-10 09:42:19 +01:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* 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:
|
2009-03-10 09:42:19 +01:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-03-10 09:42:19 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2009-03-10 09:42:19 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2009-03-10 09:42:19 +01:00
|
|
|
|
2011-12-09 04:38:59 +01:00
|
|
|
import static io.netty.handler.codec.http.HttpHeaders.*;
|
|
|
|
import io.netty.buffer.ChannelBuffer;
|
|
|
|
import io.netty.buffer.ChannelBuffers;
|
|
|
|
import io.netty.channel.ChannelHandler;
|
2012-05-20 07:19:11 +02:00
|
|
|
import io.netty.channel.ChannelInboundHandlerContext;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
2012-05-20 07:19:11 +02:00
|
|
|
import io.netty.handler.codec.MessageToMessageDecoder;
|
2012-05-16 16:02:06 +02:00
|
|
|
import io.netty.handler.codec.TooLongFrameException;
|
2011-12-09 04:38:59 +01:00
|
|
|
import io.netty.util.CharsetUtil;
|
2009-03-10 09:42:19 +01:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2009-03-10 09:42:19 +01:00
|
|
|
/**
|
2009-06-19 17:35:19 +02:00
|
|
|
* A {@link ChannelHandler} that aggregates an {@link HttpMessage}
|
2009-06-19 17:36:42 +02:00
|
|
|
* and its following {@link HttpChunk}s into a single {@link HttpMessage} with
|
|
|
|
* no following {@link HttpChunk}s. It is useful when you don't want to take
|
2009-06-19 17:39:34 +02:00
|
|
|
* care of HTTP messages whose transfer encoding is 'chunked'. Insert this
|
|
|
|
* handler after {@link HttpMessageDecoder} in the {@link ChannelPipeline}:
|
|
|
|
* <pre>
|
2010-02-02 03:00:04 +01:00
|
|
|
* {@link ChannelPipeline} p = ...;
|
2009-06-19 17:39:34 +02:00
|
|
|
* ...
|
2010-02-02 03:00:04 +01:00
|
|
|
* p.addLast("decoder", new {@link HttpRequestDecoder}());
|
|
|
|
* p.addLast("aggregator", <b>new {@link HttpChunkAggregator}(1048576)</b>);
|
2009-06-19 17:39:34 +02:00
|
|
|
* ...
|
2010-02-02 03:00:04 +01:00
|
|
|
* p.addLast("encoder", new {@link HttpResponseEncoder}());
|
2009-06-19 17:39:34 +02:00
|
|
|
* p.addLast("handler", new HttpRequestHandler());
|
|
|
|
* </pre>
|
2009-07-20 05:37:35 +02:00
|
|
|
* @apiviz.landmark
|
2011-12-09 04:38:59 +01:00
|
|
|
* @apiviz.has io.netty.handler.codec.http.HttpChunk oneway - - filters out
|
2009-03-10 09:42:19 +01:00
|
|
|
*/
|
2012-05-20 07:19:11 +02:00
|
|
|
public class HttpChunkAggregator extends MessageToMessageDecoder<Object, HttpMessage> {
|
2009-03-10 09:42:19 +01:00
|
|
|
|
2010-10-19 07:40:44 +02:00
|
|
|
private static final ChannelBuffer CONTINUE = ChannelBuffers.copiedBuffer(
|
|
|
|
"HTTP/1.1 100 Continue\r\n\r\n", CharsetUtil.US_ASCII);
|
|
|
|
|
2009-03-10 09:42:19 +01:00
|
|
|
private final int maxContentLength;
|
2010-02-19 10:00:00 +01:00
|
|
|
private HttpMessage currentMessage;
|
2009-03-10 09:42:19 +01:00
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*
|
|
|
|
* @param maxContentLength
|
|
|
|
* the maximum length of the aggregated content.
|
|
|
|
* If the length of the aggregated content exceeds this value,
|
|
|
|
* a {@link TooLongFrameException} will be raised.
|
|
|
|
*/
|
2009-03-10 09:42:19 +01:00
|
|
|
public HttpChunkAggregator(int maxContentLength) {
|
|
|
|
if (maxContentLength <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"maxContentLength must be a positive integer: " +
|
|
|
|
maxContentLength);
|
|
|
|
}
|
|
|
|
this.maxContentLength = maxContentLength;
|
|
|
|
}
|
|
|
|
|
2012-05-29 22:34:01 +02:00
|
|
|
@Override
|
|
|
|
public boolean isDecodable(Object msg) throws Exception {
|
|
|
|
return msg instanceof HttpMessage || msg instanceof HttpChunk;
|
|
|
|
}
|
2009-03-10 09:42:19 +01:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
@Override
|
|
|
|
public HttpMessage decode(ChannelInboundHandlerContext<Object> ctx, Object msg) throws Exception {
|
2009-03-10 09:42:19 +01:00
|
|
|
HttpMessage currentMessage = this.currentMessage;
|
2010-04-16 06:22:00 +02:00
|
|
|
|
|
|
|
if (msg instanceof HttpMessage) {
|
2009-03-10 09:42:19 +01:00
|
|
|
HttpMessage m = (HttpMessage) msg;
|
2010-10-19 07:40:44 +02:00
|
|
|
|
|
|
|
// Handle the 'Expect: 100-continue' header if necessary.
|
|
|
|
// TODO: Respond with 413 Request Entity Too Large
|
|
|
|
// and discard the traffic or close the connection.
|
|
|
|
// No need to notify the upstream handlers - just log.
|
|
|
|
// If decoding a response, just throw an exception.
|
|
|
|
if (is100ContinueExpected(m)) {
|
2012-05-20 07:19:11 +02:00
|
|
|
ctx.write(CONTINUE.duplicate());
|
2010-10-19 07:40:44 +02:00
|
|
|
}
|
|
|
|
|
2009-03-30 04:19:11 +02:00
|
|
|
if (m.isChunked()) {
|
2009-03-10 09:42:19 +01:00
|
|
|
// A chunked message - remove 'Transfer-Encoding' header,
|
|
|
|
// initialize the cumulative buffer, and wait for incoming chunks.
|
|
|
|
List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
|
|
|
|
encodings.remove(HttpHeaders.Values.CHUNKED);
|
|
|
|
if (encodings.isEmpty()) {
|
|
|
|
m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
|
|
|
|
}
|
2010-08-26 04:07:11 +02:00
|
|
|
m.setChunked(false);
|
2012-05-20 07:19:11 +02:00
|
|
|
m.setContent(ChannelBuffers.dynamicBuffer());
|
2009-03-10 09:42:19 +01:00
|
|
|
this.currentMessage = m;
|
2012-05-20 07:19:11 +02:00
|
|
|
return null;
|
2009-03-10 09:42:19 +01:00
|
|
|
} else {
|
|
|
|
// Not a chunked message - pass through.
|
2010-04-16 06:22:00 +02:00
|
|
|
this.currentMessage = null;
|
2012-05-20 07:19:11 +02:00
|
|
|
return m;
|
2009-03-10 09:42:19 +01:00
|
|
|
}
|
2010-04-16 06:22:00 +02:00
|
|
|
} else if (msg instanceof HttpChunk) {
|
|
|
|
// Sanity check
|
|
|
|
if (currentMessage == null) {
|
|
|
|
throw new IllegalStateException(
|
|
|
|
"received " + HttpChunk.class.getSimpleName() +
|
|
|
|
" without " + HttpMessage.class.getSimpleName());
|
|
|
|
}
|
|
|
|
|
2009-03-10 09:42:19 +01:00
|
|
|
// Merge the received chunk into the content of the current message.
|
|
|
|
HttpChunk chunk = (HttpChunk) msg;
|
|
|
|
ChannelBuffer content = currentMessage.getContent();
|
|
|
|
|
|
|
|
if (content.readableBytes() > maxContentLength - chunk.getContent().readableBytes()) {
|
2010-10-19 07:40:44 +02:00
|
|
|
// TODO: Respond with 413 Request Entity Too Large
|
|
|
|
// and discard the traffic or close the connection.
|
|
|
|
// No need to notify the upstream handlers - just log.
|
|
|
|
// If decoding a response, just throw an exception.
|
2009-03-10 09:42:19 +01:00
|
|
|
throw new TooLongFrameException(
|
|
|
|
"HTTP content length exceeded " + maxContentLength +
|
|
|
|
" bytes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
content.writeBytes(chunk.getContent());
|
|
|
|
if (chunk.isLast()) {
|
|
|
|
this.currentMessage = null;
|
2010-08-26 07:06:40 +02:00
|
|
|
|
|
|
|
// Merge trailing headers into the message.
|
|
|
|
if (chunk instanceof HttpChunkTrailer) {
|
|
|
|
HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
|
|
|
|
for (Entry<String, String> header: trailer.getHeaders()) {
|
|
|
|
currentMessage.setHeader(header.getKey(), header.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the 'Content-Length' header.
|
2009-03-10 09:42:19 +01:00
|
|
|
currentMessage.setHeader(
|
|
|
|
HttpHeaders.Names.CONTENT_LENGTH,
|
|
|
|
String.valueOf(content.readableBytes()));
|
2010-08-26 07:06:40 +02:00
|
|
|
|
2012-05-20 07:19:11 +02:00
|
|
|
// All done
|
|
|
|
return currentMessage;
|
|
|
|
} else {
|
|
|
|
return null;
|
2009-03-10 09:42:19 +01:00
|
|
|
}
|
2010-04-16 06:22:00 +02:00
|
|
|
} else {
|
2012-05-20 07:19:11 +02:00
|
|
|
throw new IllegalStateException(
|
|
|
|
"Only " + HttpMessage.class.getSimpleName() + " and " +
|
|
|
|
HttpChunk.class.getSimpleName() + " are accepted: " + msg.getClass().getName());
|
2009-03-10 09:42:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|