1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
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
52
53
54
55
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
67
68 protected WebSocketExtensionFilter extensionEncoderFilter() {
69 return extensionEncoderFilter;
70 }
71
72
73
74
75
76 protected abstract int rsv(WebSocketFrame msg);
77
78
79
80
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
91
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
161 encoder.finishAndReleaseAll();
162 encoder = null;
163 }
164 }
165 }