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 static io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateDecoder.*;
19  import io.netty.buffer.ByteBuf;
20  import io.netty.buffer.CompositeByteBuf;
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.compression.ZlibCodecFactory;
25  import io.netty.handler.codec.compression.ZlibWrapper;
26  import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
27  import io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame;
28  import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
29  import io.netty.handler.codec.http.websocketx.WebSocketFrame;
30  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
31  
32  import java.util.List;
33  
34  /**
35   * Deflate implementation of a payload compressor for
36   * <tt>io.netty.handler.codec.http.websocketx.WebSocketFrame</tt>.
37   */
38  abstract class DeflateEncoder extends WebSocketExtensionEncoder {
39  
40      private final int compressionLevel;
41      private final int windowSize;
42      private final boolean noContext;
43  
44      private EmbeddedChannel encoder;
45  
46      /**
47       * Constructor
48       * @param compressionLevel compression level of the compressor.
49       * @param windowSize maximum size of the window compressor buffer.
50       * @param noContext true to disable context takeover.
51       */
52      public DeflateEncoder(int compressionLevel, int windowSize, boolean noContext) {
53          this.compressionLevel = compressionLevel;
54          this.windowSize = windowSize;
55          this.noContext = noContext;
56      }
57  
58      /**
59       * @param msg the current frame.
60       * @return the rsv bits to set in the compressed frame.
61       */
62      protected abstract int rsv(WebSocketFrame msg);
63  
64      /**
65       * @param msg the current frame.
66       * @return true if compressed payload tail needs to be removed.
67       */
68      protected abstract boolean removeFrameTail(WebSocketFrame msg);
69  
70      @Override
71      protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg,
72              List<Object> out) throws Exception {
73          if (encoder == null) {
74              encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(
75                      ZlibWrapper.NONE, compressionLevel, windowSize, 8));
76          }
77  
78          encoder.writeOutbound(msg.content().retain());
79  
80          CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer();
81          for (;;) {
82              ByteBuf partCompressedContent = encoder.readOutbound();
83              if (partCompressedContent == null) {
84                  break;
85              }
86              if (!partCompressedContent.isReadable()) {
87                  partCompressedContent.release();
88                  continue;
89              }
90              fullCompressedContent.addComponent(true, partCompressedContent);
91          }
92          if (fullCompressedContent.numComponents() <= 0) {
93              fullCompressedContent.release();
94              throw new CodecException("cannot read compressed buffer");
95          }
96  
97          if (msg.isFinalFragment() && noContext) {
98              cleanup();
99          }
100 
101         ByteBuf compressedContent;
102         if (removeFrameTail(msg)) {
103             int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.length;
104             compressedContent = fullCompressedContent.slice(0, realLength);
105         } else {
106             compressedContent = fullCompressedContent;
107         }
108 
109         WebSocketFrame outMsg;
110         if (msg instanceof TextWebSocketFrame) {
111             outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
112         } else if (msg instanceof BinaryWebSocketFrame) {
113             outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
114         } else if (msg instanceof ContinuationWebSocketFrame) {
115             outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
116         } else {
117             throw new CodecException("unexpected frame type: " + msg.getClass().getName());
118         }
119         out.add(outMsg);
120     }
121 
122     @Override
123     public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
124         cleanup();
125         super.handlerRemoved(ctx);
126     }
127 
128     private void cleanup() {
129         if (encoder != null) {
130             // Clean-up the previous encoder if not cleaned up correctly.
131             if (encoder.finish()) {
132                 for (;;) {
133                     ByteBuf buf = encoder.readOutbound();
134                     if (buf == null) {
135                         break;
136                     }
137                     // Release the buffer
138                     buf.release();
139                 }
140             }
141             encoder = null;
142         }
143     }
144 }