View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://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 org.jboss.netty.handler.codec.http;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  import static org.jboss.netty.handler.codec.http.HttpHeaders.*;
20  
21  import java.util.List;
22  import java.util.Map.Entry;
23  
24  import org.jboss.netty.buffer.ChannelBuffer;
25  import org.jboss.netty.buffer.ChannelBuffers;
26  import org.jboss.netty.channel.ChannelHandler;
27  import org.jboss.netty.channel.ChannelHandlerContext;
28  import org.jboss.netty.channel.ChannelPipeline;
29  import org.jboss.netty.channel.Channels;
30  import org.jboss.netty.channel.MessageEvent;
31  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
32  import org.jboss.netty.handler.codec.frame.TooLongFrameException;
33  import org.jboss.netty.util.CharsetUtil;
34  
35  /**
36   * A {@link ChannelHandler} that aggregates an {@link HttpMessage}
37   * and its following {@link HttpChunk}s into a single {@link HttpMessage} with
38   * no following {@link HttpChunk}s.  It is useful when you don't want to take
39   * care of HTTP messages whose transfer encoding is 'chunked'.  Insert this
40   * handler after {@link HttpMessageDecoder} in the {@link ChannelPipeline}:
41   * <pre>
42   * {@link ChannelPipeline} p = ...;
43   * ...
44   * p.addLast("decoder", new {@link HttpRequestDecoder}());
45   * p.addLast("aggregator", <b>new {@link HttpChunkAggregator}(1048576)</b>);
46   * ...
47   * p.addLast("encoder", new {@link HttpResponseEncoder}());
48   * p.addLast("handler", new HttpRequestHandler());
49   * </pre>
50   *
51   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
52   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
53   * @version $Rev: 2370 $, $Date: 2010-10-19 14:40:44 +0900 (Tue, 19 Oct 2010) $
54   *
55   * @apiviz.landmark
56   * @apiviz.has org.jboss.netty.handler.codec.http.HttpChunk oneway - - filters out
57   */
58  public class HttpChunkAggregator extends SimpleChannelUpstreamHandler {
59  
60      private static final ChannelBuffer CONTINUE = ChannelBuffers.copiedBuffer(
61              "HTTP/1.1 100 Continue\r\n\r\n", CharsetUtil.US_ASCII);
62  
63      private final int maxContentLength;
64      private HttpMessage currentMessage;
65  
66      /**
67       * Creates a new instance.
68       *
69       * @param maxContentLength
70       *        the maximum length of the aggregated content.
71       *        If the length of the aggregated content exceeds this value,
72       *        a {@link TooLongFrameException} will be raised.
73       */
74      public HttpChunkAggregator(int maxContentLength) {
75          if (maxContentLength <= 0) {
76              throw new IllegalArgumentException(
77                      "maxContentLength must be a positive integer: " +
78                      maxContentLength);
79          }
80          this.maxContentLength = maxContentLength;
81      }
82  
83      @Override
84      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
85              throws Exception {
86  
87          Object msg = e.getMessage();
88          HttpMessage currentMessage = this.currentMessage;
89  
90          if (msg instanceof HttpMessage) {
91              HttpMessage m = (HttpMessage) msg;
92  
93              // Handle the 'Expect: 100-continue' header if necessary.
94              // TODO: Respond with 413 Request Entity Too Large
95              //   and discard the traffic or close the connection.
96              //       No need to notify the upstream handlers - just log.
97              //       If decoding a response, just throw an exception.
98              if (is100ContinueExpected(m)) {
99                  write(ctx, succeededFuture(ctx.getChannel()), CONTINUE.duplicate());
100             }
101 
102             if (m.isChunked()) {
103                 // A chunked message - remove 'Transfer-Encoding' header,
104                 // initialize the cumulative buffer, and wait for incoming chunks.
105                 List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
106                 encodings.remove(HttpHeaders.Values.CHUNKED);
107                 if (encodings.isEmpty()) {
108                     m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
109                 }
110                 m.setChunked(false);
111                 m.setContent(ChannelBuffers.dynamicBuffer(e.getChannel().getConfig().getBufferFactory()));
112                 this.currentMessage = m;
113             } else {
114                 // Not a chunked message - pass through.
115                 this.currentMessage = null;
116                 ctx.sendUpstream(e);
117             }
118         } else if (msg instanceof HttpChunk) {
119             // Sanity check
120             if (currentMessage == null) {
121                 throw new IllegalStateException(
122                         "received " + HttpChunk.class.getSimpleName() +
123                         " without " + HttpMessage.class.getSimpleName());
124             }
125 
126             // Merge the received chunk into the content of the current message.
127             HttpChunk chunk = (HttpChunk) msg;
128             ChannelBuffer content = currentMessage.getContent();
129 
130             if (content.readableBytes() > maxContentLength - chunk.getContent().readableBytes()) {
131                 // TODO: Respond with 413 Request Entity Too Large
132                 //   and discard the traffic or close the connection.
133                 //       No need to notify the upstream handlers - just log.
134                 //       If decoding a response, just throw an exception.
135                 throw new TooLongFrameException(
136                         "HTTP content length exceeded " + maxContentLength +
137                         " bytes.");
138             }
139 
140             content.writeBytes(chunk.getContent());
141             if (chunk.isLast()) {
142                 this.currentMessage = null;
143 
144                 // Merge trailing headers into the message.
145                 if (chunk instanceof HttpChunkTrailer) {
146                     HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
147                     for (Entry<String, String> header: trailer.getHeaders()) {
148                         currentMessage.setHeader(header.getKey(), header.getValue());
149                     }
150                 }
151 
152                 // Set the 'Content-Length' header.
153                 currentMessage.setHeader(
154                         HttpHeaders.Names.CONTENT_LENGTH,
155                         String.valueOf(content.readableBytes()));
156 
157                 // All done - generate the event.
158                 Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
159             }
160         } else {
161             // Neither HttpMessage or HttpChunk
162             ctx.sendUpstream(e);
163         }
164     }
165 }