View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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 io.netty.handler.codec;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.buffer.ByteBufHolder;
20  import io.netty.buffer.CompositeByteBuf;
21  import io.netty.channel.ChannelFuture;
22  import io.netty.channel.ChannelFutureListener;
23  import io.netty.channel.ChannelHandler;
24  import io.netty.channel.ChannelHandlerContext;
25  import io.netty.channel.ChannelPipeline;
26  import io.netty.util.ReferenceCountUtil;
27  
28  import java.util.List;
29  
30  import static io.netty.buffer.Unpooled.EMPTY_BUFFER;
31  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
32  
33  /**
34   * An abstract {@link ChannelHandler} that aggregates a series of message objects into a single aggregated message.
35   * <p>
36   * 'A series of messages' is composed of the following:
37   * <ul>
38   * <li>a single start message which optionally contains the first part of the content, and</li>
39   * <li>1 or more content messages.</li>
40   * </ul>
41   * The content of the aggregated message will be the merged content of the start message and its following content
42   * messages. If this aggregator encounters a content message where {@link #isLastContentMessage(ByteBufHolder)}
43   * return {@code true} for, the aggregator will finish the aggregation and produce the aggregated message and expect
44   * another start message.
45   * </p>
46   *
47   * @param <I> the type that covers both start message and content message
48   * @param <S> the type of the start message
49   * @param <C> the type of the content message (must be a subtype of {@link ByteBufHolder})
50   * @param <O> the type of the aggregated message (must be a subtype of {@code S} and {@link ByteBufHolder})
51   */
52  public abstract class MessageAggregator<I, S, C extends ByteBufHolder, O extends ByteBufHolder>
53          extends MessageToMessageDecoder<I> {
54  
55      private static final int DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS = 1024;
56  
57      private final int maxContentLength;
58      private O currentMessage;
59      private boolean handlingOversizedMessage;
60  
61      private int maxCumulationBufferComponents = DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS;
62      private ChannelHandlerContext ctx;
63      private ChannelFutureListener continueResponseWriteListener;
64  
65      private boolean aggregating;
66      private boolean handleIncompleteAggregateDuringClose = true;
67  
68      /**
69       * Creates a new instance.
70       *
71       * @param maxContentLength
72       *        the maximum length of the aggregated content.
73       *        If the length of the aggregated content exceeds this value,
74       *        {@link #handleOversizedMessage(ChannelHandlerContext, Object)} will be called.
75       */
76      protected MessageAggregator(int maxContentLength) {
77          validateMaxContentLength(maxContentLength);
78          this.maxContentLength = maxContentLength;
79      }
80  
81      protected MessageAggregator(int maxContentLength, Class<? extends I> inboundMessageType) {
82          super(inboundMessageType);
83          validateMaxContentLength(maxContentLength);
84          this.maxContentLength = maxContentLength;
85      }
86  
87      private static void validateMaxContentLength(int maxContentLength) {
88          checkPositiveOrZero(maxContentLength, "maxContentLength");
89      }
90  
91      @Override
92      public boolean acceptInboundMessage(Object msg) throws Exception {
93          // No need to match last and full types because they are subset of first and middle types.
94          if (!super.acceptInboundMessage(msg)) {
95              return false;
96          }
97  
98          @SuppressWarnings("unchecked")
99          I in = (I) msg;
100 
101         if (isAggregated(in)) {
102             return false;
103         }
104 
105         // NOTE: It's tempting to make this check only if aggregating is false. There are however
106         // side conditions in decode(...) in respect to large messages.
107         if (isStartMessage(in)) {
108             return true;
109         } else {
110             return aggregating && isContentMessage(in);
111         }
112     }
113 
114     /**
115      * Returns {@code true} if and only if the specified message is a start message. Typically, this method is
116      * implemented as a single {@code return} statement with {@code instanceof}:
117      * <pre>
118      * return msg instanceof MyStartMessage;
119      * </pre>
120      */
121     protected abstract boolean isStartMessage(I msg) throws Exception;
122 
123     /**
124      * Returns {@code true} if and only if the specified message is a content message. Typically, this method is
125      * implemented as a single {@code return} statement with {@code instanceof}:
126      * <pre>
127      * return msg instanceof MyContentMessage;
128      * </pre>
129      */
130     protected abstract boolean isContentMessage(I msg) throws Exception;
131 
132     /**
133      * Returns {@code true} if and only if the specified message is the last content message. Typically, this method is
134      * implemented as a single {@code return} statement with {@code instanceof}:
135      * <pre>
136      * return msg instanceof MyLastContentMessage;
137      * </pre>
138      * or with {@code instanceof} and boolean field check:
139      * <pre>
140      * return msg instanceof MyContentMessage && msg.isLastFragment();
141      * </pre>
142      */
143     protected abstract boolean isLastContentMessage(C msg) throws Exception;
144 
145     /**
146      * Returns {@code true} if and only if the specified message is already aggregated.  If this method returns
147      * {@code true}, this handler will simply forward the message to the next handler as-is.
148      */
149     protected abstract boolean isAggregated(I msg) throws Exception;
150 
151     /**
152      * Returns the maximum allowed length of the aggregated message in bytes.
153      */
154     public final int maxContentLength() {
155         return maxContentLength;
156     }
157 
158     /**
159      * Returns the maximum number of components in the cumulation buffer.  If the number of
160      * the components in the cumulation buffer exceeds this value, the components of the
161      * cumulation buffer are consolidated into a single component, involving memory copies.
162      * The default value of this property is {@value #DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS}.
163      */
164     public final int maxCumulationBufferComponents() {
165         return maxCumulationBufferComponents;
166     }
167 
168     /**
169      * Sets the maximum number of components in the cumulation buffer.  If the number of
170      * the components in the cumulation buffer exceeds this value, the components of the
171      * cumulation buffer are consolidated into a single component, involving memory copies.
172      * The default value of this property is {@value #DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS}
173      * and its minimum allowed value is {@code 2}.
174      */
175     public final void setMaxCumulationBufferComponents(int maxCumulationBufferComponents) {
176         if (maxCumulationBufferComponents < 2) {
177             throw new IllegalArgumentException(
178                     "maxCumulationBufferComponents: " + maxCumulationBufferComponents +
179                     " (expected: >= 2)");
180         }
181 
182         if (ctx == null) {
183             this.maxCumulationBufferComponents = maxCumulationBufferComponents;
184         } else {
185             throw new IllegalStateException(
186                     "decoder properties cannot be changed once the decoder is added to a pipeline.");
187         }
188     }
189 
190     /**
191      * @deprecated This method will be removed in future releases.
192      */
193     @Deprecated
194     public final boolean isHandlingOversizedMessage() {
195         return handlingOversizedMessage;
196     }
197 
198     protected final ChannelHandlerContext ctx() {
199         if (ctx == null) {
200             throw new IllegalStateException("not added to a pipeline yet");
201         }
202         return ctx;
203     }
204 
205     @Override
206     protected void decode(final ChannelHandlerContext ctx, I msg, List<Object> out) throws Exception {
207         if (isStartMessage(msg)) {
208             aggregating = true;
209             handlingOversizedMessage = false;
210             if (currentMessage != null) {
211                 currentMessage.release();
212                 currentMessage = null;
213                 throw new MessageAggregationException();
214             }
215 
216             @SuppressWarnings("unchecked")
217             S m = (S) msg;
218 
219             // Send the continue response if necessary (e.g. 'Expect: 100-continue' header)
220             // Check before content length. Failing an expectation may result in a different response being sent.
221             Object continueResponse = newContinueResponse(m, maxContentLength, ctx.pipeline());
222             if (continueResponse != null) {
223                 // Cache the write listener for reuse.
224                 ChannelFutureListener listener = continueResponseWriteListener;
225                 if (listener == null) {
226                     continueResponseWriteListener = listener = future -> {
227                         if (!future.isSuccess()) {
228                             ctx.fireExceptionCaught(future.cause());
229                         }
230                     };
231                 }
232 
233                 // Make sure to call this before writing, otherwise reference counts may be invalid.
234                 boolean closeAfterWrite = closeAfterContinueResponse(continueResponse);
235                 handlingOversizedMessage = ignoreContentAfterContinueResponse(continueResponse);
236 
237                 final ChannelFuture future = ctx.writeAndFlush(continueResponse).addListener(listener);
238 
239                 if (closeAfterWrite) {
240                     handleIncompleteAggregateDuringClose = false;
241                     future.addListener(ChannelFutureListener.CLOSE);
242                     return;
243                 }
244                 if (handlingOversizedMessage) {
245                     return;
246                 }
247             } else if (isContentLengthInvalid(m, maxContentLength)) {
248                 // if content length is set, preemptively close if it's too large
249                 invokeHandleOversizedMessage(ctx, m);
250                 return;
251             }
252 
253             if (m instanceof DecoderResultProvider && !((DecoderResultProvider) m).decoderResult().isSuccess()) {
254                 O aggregated;
255                 if (m instanceof ByteBufHolder) {
256                     aggregated = beginAggregation(m, ((ByteBufHolder) m).content().retain());
257                 } else {
258                     aggregated = beginAggregation(m, EMPTY_BUFFER);
259                 }
260                 finishAggregation0(aggregated);
261                 out.add(aggregated);
262                 return;
263             }
264 
265             // A streamed message - initialize the cumulative buffer, and wait for incoming chunks.
266             CompositeByteBuf content = ctx.alloc().compositeBuffer(maxCumulationBufferComponents);
267             if (m instanceof ByteBufHolder) {
268                 appendPartialContent(content, ((ByteBufHolder) m).content());
269             }
270             currentMessage = beginAggregation(m, content);
271         } else if (isContentMessage(msg)) {
272             if (currentMessage == null) {
273                 // it is possible that a TooLongFrameException was already thrown but we can still discard data
274                 // until the begging of the next request/response.
275                 return;
276             }
277 
278             // Merge the received chunk into the content of the current message.
279             CompositeByteBuf content = (CompositeByteBuf) currentMessage.content();
280 
281             @SuppressWarnings("unchecked")
282             final C m = (C) msg;
283             // Handle oversized message.
284             if (content.readableBytes() > maxContentLength - m.content().readableBytes()) {
285                 // By convention, full message type extends first message type.
286                 @SuppressWarnings("unchecked")
287                 S s = (S) currentMessage;
288                 invokeHandleOversizedMessage(ctx, s);
289                 return;
290             }
291 
292             // Append the content of the chunk.
293             appendPartialContent(content, m.content());
294 
295             // Give the subtypes a chance to merge additional information such as trailing headers.
296             aggregate(currentMessage, m);
297 
298             final boolean last;
299             if (m instanceof DecoderResultProvider) {
300                 DecoderResult decoderResult = ((DecoderResultProvider) m).decoderResult();
301                 if (!decoderResult.isSuccess()) {
302                     if (currentMessage instanceof DecoderResultProvider) {
303                         ((DecoderResultProvider) currentMessage).setDecoderResult(
304                                 DecoderResult.failure(decoderResult.cause()));
305                     }
306                     last = true;
307                 } else {
308                     last = isLastContentMessage(m);
309                 }
310             } else {
311                 last = isLastContentMessage(m);
312             }
313 
314             if (last) {
315                 finishAggregation0(currentMessage);
316 
317                 // All done
318                 out.add(currentMessage);
319                 currentMessage = null;
320             }
321         } else {
322             throw new MessageAggregationException();
323         }
324     }
325 
326     private static void appendPartialContent(CompositeByteBuf content, ByteBuf partialContent) {
327         if (partialContent.isReadable()) {
328             content.addComponent(true, partialContent.retain());
329         }
330     }
331 
332     /**
333      * Determine if the message {@code start}'s content length is known, and if it greater than
334      * {@code maxContentLength}.
335      * @param start The message which may indicate the content length.
336      * @param maxContentLength The maximum allowed content length.
337      * @return {@code true} if the message {@code start}'s content length is known, and if it greater than
338      * {@code maxContentLength}. {@code false} otherwise.
339      */
340     protected abstract boolean isContentLengthInvalid(S start, int maxContentLength) throws Exception;
341 
342     /**
343      * Returns the 'continue response' for the specified start message if necessary. For example, this method is
344      * useful to handle an HTTP 100-continue header.
345      *
346      * @return the 'continue response', or {@code null} if there's no message to send
347      */
348     protected abstract Object newContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline)
349             throws Exception;
350 
351     /**
352      * Determine if the channel should be closed after the result of
353      * {@link #newContinueResponse(Object, int, ChannelPipeline)} is written.
354      * @param msg The return value from {@link #newContinueResponse(Object, int, ChannelPipeline)}.
355      * @return {@code true} if the channel should be closed after the result of
356      * {@link #newContinueResponse(Object, int, ChannelPipeline)} is written. {@code false} otherwise.
357      */
358     protected abstract boolean closeAfterContinueResponse(Object msg) throws Exception;
359 
360     /**
361      * Determine if all objects for the current request/response should be ignored or not.
362      * Messages will stop being ignored the next time {@link #isContentMessage(Object)} returns {@code true}.
363      *
364      * @param msg The return value from {@link #newContinueResponse(Object, int, ChannelPipeline)}.
365      * @return {@code true} if all objects for the current request/response should be ignored or not.
366      * {@code false} otherwise.
367      */
368     protected abstract boolean ignoreContentAfterContinueResponse(Object msg) throws Exception;
369 
370     /**
371      * Creates a new aggregated message from the specified start message and the specified content.  If the start
372      * message implements {@link ByteBufHolder}, its content is appended to the specified {@code content}.
373      * This aggregator will continue to append the received content to the specified {@code content}.
374      */
375     protected abstract O beginAggregation(S start, ByteBuf content) throws Exception;
376 
377     /**
378      * Transfers the information provided by the specified content message to the specified aggregated message.
379      * Note that the content of the specified content message has been appended to the content of the specified
380      * aggregated message already, so that you don't need to.  Use this method to transfer the additional information
381      * that the content message provides to {@code aggregated}.
382      */
383     protected void aggregate(O aggregated, C content) throws Exception { }
384 
385     private void finishAggregation0(O aggregated) throws Exception {
386         aggregating = false;
387         finishAggregation(aggregated);
388     }
389 
390     /**
391      * Invoked when the specified {@code aggregated} message is about to be passed to the next handler in the pipeline.
392      */
393     protected void finishAggregation(O aggregated) throws Exception { }
394 
395     private void invokeHandleOversizedMessage(ChannelHandlerContext ctx, S oversized) throws Exception {
396         handlingOversizedMessage = true;
397         currentMessage = null;
398         handleIncompleteAggregateDuringClose = false;
399         try {
400             handleOversizedMessage(ctx, oversized);
401         } finally {
402             // Release the message in case it is a full one.
403             ReferenceCountUtil.release(oversized);
404         }
405     }
406 
407     /**
408      * Invoked when an incoming request exceeds the maximum content length.  The default behvaior is to trigger an
409      * {@code exceptionCaught()} event with a {@link TooLongFrameException}.
410      *
411      * @param ctx the {@link ChannelHandlerContext}
412      * @param oversized the accumulated message up to this point, whose type is {@code S} or {@code O}
413      */
414     protected void handleOversizedMessage(ChannelHandlerContext ctx, S oversized) throws Exception {
415         ctx.fireExceptionCaught(
416                 new TooLongFrameException("content length exceeded " + maxContentLength() + " bytes."));
417     }
418 
419     @Override
420     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
421         // We might need keep reading the channel until the full message is aggregated.
422         //
423         // See https://github.com/netty/netty/issues/6583
424         if (currentMessage != null && !ctx.channel().config().isAutoRead()) {
425             ctx.read();
426         }
427         ctx.fireChannelReadComplete();
428     }
429 
430     @Override
431     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
432         if (aggregating && handleIncompleteAggregateDuringClose) {
433             ctx.fireExceptionCaught(
434                     new PrematureChannelClosureException("Channel closed while still aggregating message"));
435         }
436         try {
437             // release current message if it is not null as it may be a left-over
438             super.channelInactive(ctx);
439         } finally {
440             releaseCurrentMessage();
441         }
442     }
443 
444     @Override
445     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
446         this.ctx = ctx;
447     }
448 
449     @Override
450     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
451         try {
452             super.handlerRemoved(ctx);
453         } finally {
454             // release current message if it is not null as it may be a left-over as there is not much more we can do in
455             // this case
456             releaseCurrentMessage();
457         }
458     }
459 
460     protected final void releaseCurrentMessage() {
461         if (currentMessage != null) {
462             currentMessage.release();
463             currentMessage = null;
464         }
465         handlingOversizedMessage = false;
466         aggregating = false;
467     }
468 }