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