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.buffer.ByteBuf;
19  import io.netty.buffer.CompositeByteBuf;
20  import io.netty.buffer.Unpooled;
21  import io.netty.channel.ChannelHandlerContext;
22  import io.netty.channel.embedded.EmbeddedChannel;
23  import io.netty.handler.codec.CodecException;
24  import io.netty.handler.codec.MessageToMessageDecoder;
25  import io.netty.handler.codec.compression.ZlibCodecFactory;
26  import io.netty.handler.codec.compression.ZlibWrapper;
27  import io.netty.handler.codec.http.DefaultHttpContent;
28  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
29  import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
30  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
31  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
32  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtension;
33  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
34  import io.netty.util.ReferenceCountUtil;
35  
36  import java.util.List;
37  
38  /**
39   * Deflate implementation of a payload decompressor for
40   * <tt>io.netty.handler.codec.http.websocketx.WebSocketFrame</tt>.
41   */
42  abstract class DeflateDecoder extends WebSocketExtensionDecoder {
43  
44      static final byte[] FRAME_TAIL = new byte[] {0x00, 0x00, (byte) 0xff, (byte) 0xff};
45  
46      private final boolean noContext;
47  
48      private EmbeddedChannel decoder;
49  
50      /**
51       * Constructor
52       * @param noContext true to disable context takeover.
53       */
54      public DeflateDecoder(boolean noContext) {
55          this.noContext = noContext;
56      }
57  
58      protected abstract boolean appendFrameTail(WebSocketFrame msg);
59  
60      protected abstract int newRsv(WebSocketFrame msg);
61  
62      @Override
63      protected void decode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
64          if (decoder == null) {
65              if (!(msg instanceof TextWebSocketFrame) && !(msg instanceof BinaryWebSocketFrame)) {
66                  throw new CodecException("unexpected initial frame type: " + msg.getClass().getName());
67              }
68              decoder = new EmbeddedChannel(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.NONE));
69          }
70  
71          decoder.writeInbound(msg.content().retain());
72          if (appendFrameTail(msg)) {
73              decoder.writeInbound(Unpooled.wrappedBuffer(FRAME_TAIL));
74          }
75  
76          CompositeByteBuf compositeUncompressedContent = ctx.alloc().compositeBuffer();
77          for (;;) {
78              ByteBuf partUncompressedContent = decoder.readInbound();
79              if (partUncompressedContent == null) {
80                  break;
81              }
82              if (!partUncompressedContent.isReadable()) {
83                  partUncompressedContent.release();
84                  continue;
85              }
86              compositeUncompressedContent.addComponent(partUncompressedContent);
87              compositeUncompressedContent.writerIndex(compositeUncompressedContent.writerIndex() +
88                      partUncompressedContent.readableBytes());
89          }
90          if (compositeUncompressedContent.numComponents() <= 0) {
91              compositeUncompressedContent.release();
92              throw new CodecException("cannot read uncompressed buffer");
93          }
94  
95          if (msg.isFinalFragment() && noContext) {
96              cleanup();
97          }
98  
99          WebSocketFrame outMsg;
100         if (msg instanceof TextWebSocketFrame) {
101             outMsg = new TextWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
102         } else if (msg instanceof BinaryWebSocketFrame) {
103             outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), newRsv(msg), compositeUncompressedContent);
104         } else if (msg instanceof ContinuationWebSocketFrame) {
105             outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), newRsv(msg),
106                     compositeUncompressedContent);
107         } else {
108             throw new CodecException("unexpected frame type: " + msg.getClass().getName());
109         }
110         out.add(outMsg);
111     }
112 
113     @Override
114     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
115         cleanup();
116         super.handlerRemoved(ctx);
117     }
118 
119     @Override
120     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
121         cleanup();
122         super.channelInactive(ctx);
123     }
124 
125     private void cleanup() {
126         if (decoder != null) {
127             // Clean-up the previous encoder if not cleaned up correctly.
128             if (decoder.finish()) {
129                 for (;;) {
130                     ByteBuf buf = decoder.readOutbound();
131                     if (buf == null) {
132                         break;
133                     }
134                     // Release the buffer
135                     buf.release();
136                 }
137             }
138             decoder = null;
139         }
140     }
141 }