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