View Javadoc
1   /*
2    * Copyright 2014 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    *   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 io.netty.handler.codec.http.websocketx.extensions.compression;
17  
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
20  import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
21  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
22  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
23  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtension;
24  
25  import java.util.List;
26  
27  /**
28   * Per-message implementation of deflate decompressor.
29   */
30  class PerMessageDeflateDecoder extends DeflateDecoder {
31  
32      private boolean compressing;
33  
34      /**
35       * Constructor
36       * @param noContext true to disable context takeover.
37       */
38      public PerMessageDeflateDecoder(boolean noContext) {
39          super(noContext);
40      }
41  
42      @Override
43      public boolean acceptInboundMessage(Object msg) throws Exception {
44          return ((msg instanceof TextWebSocketFrame ||
45                   msg instanceof BinaryWebSocketFrame) &&
46                      (((WebSocketFrame) msg).rsv() & WebSocketExtension.RSV1) > 0) ||
47                  (msg instanceof ContinuationWebSocketFrame && compressing);
48      }
49  
50      @Override
51      protected int newRsv(WebSocketFrame msg) {
52          return (msg.rsv() & WebSocketExtension.RSV1) > 0 ?
53                  msg.rsv() ^ WebSocketExtension.RSV1 : msg.rsv();
54      }
55  
56      @Override
57      protected boolean appendFrameTail(WebSocketFrame msg) {
58          return msg.isFinalFragment();
59      }
60  
61      @Override
62      protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg,
63          List<Object> out) throws Exception {
64          super.decode(ctx, msg, out);
65  
66          if (msg.isFinalFragment()) {
67              compressing = false;
68          } else if (msg instanceof TextWebSocketFrame || msg instanceof BinaryWebSocketFrame) {
69              compressing = true;
70          }
71      }
72  }