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