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.handler.codec.http.websocketx.extensions.WebSocketExtensionData;
19 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
20 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
21 import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionFilterProvider;
22 import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtension;
23 import io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandshaker;
24
25 import java.util.Collections;
26
27 import static io.netty.util.internal.ObjectUtil.*;
28
29
30
31
32
33 public final class DeflateFrameServerExtensionHandshaker implements WebSocketServerExtensionHandshaker {
34
35 static final String X_WEBKIT_DEFLATE_FRAME_EXTENSION = "x-webkit-deflate-frame";
36 static final String DEFLATE_FRAME_EXTENSION = "deflate-frame";
37
38 private final int compressionLevel;
39 private final WebSocketExtensionFilterProvider extensionFilterProvider;
40
41
42
43
44 public DeflateFrameServerExtensionHandshaker() {
45 this(6);
46 }
47
48
49
50
51
52
53
54 public DeflateFrameServerExtensionHandshaker(int compressionLevel) {
55 this(compressionLevel, WebSocketExtensionFilterProvider.DEFAULT);
56 }
57
58
59
60
61
62
63
64
65
66 public DeflateFrameServerExtensionHandshaker(int compressionLevel,
67 WebSocketExtensionFilterProvider extensionFilterProvider) {
68 if (compressionLevel < 0 || compressionLevel > 9) {
69 throw new IllegalArgumentException(
70 "compressionLevel: " + compressionLevel + " (expected: 0-9)");
71 }
72 this.compressionLevel = compressionLevel;
73 this.extensionFilterProvider = checkNotNull(extensionFilterProvider, "extensionFilterProvider");
74 }
75
76 @Override
77 public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) {
78 if (!X_WEBKIT_DEFLATE_FRAME_EXTENSION.equals(extensionData.name()) &&
79 !DEFLATE_FRAME_EXTENSION.equals(extensionData.name())) {
80 return null;
81 }
82
83 if (extensionData.parameters().isEmpty()) {
84 return new DeflateFrameServerExtension(compressionLevel, extensionData.name(), extensionFilterProvider);
85 } else {
86 return null;
87 }
88 }
89
90 private static class DeflateFrameServerExtension implements WebSocketServerExtension {
91
92 private final String extensionName;
93 private final int compressionLevel;
94 private final WebSocketExtensionFilterProvider extensionFilterProvider;
95
96 DeflateFrameServerExtension(int compressionLevel, String extensionName,
97 WebSocketExtensionFilterProvider extensionFilterProvider) {
98 this.extensionName = extensionName;
99 this.compressionLevel = compressionLevel;
100 this.extensionFilterProvider = extensionFilterProvider;
101 }
102
103 @Override
104 public int rsv() {
105 return RSV1;
106 }
107
108 @Override
109 public WebSocketExtensionEncoder newExtensionEncoder() {
110 return new PerFrameDeflateEncoder(compressionLevel, 15, false,
111 extensionFilterProvider.encoderFilter());
112 }
113
114 @Override
115 public WebSocketExtensionDecoder newExtensionDecoder() {
116 return new PerFrameDeflateDecoder(false, extensionFilterProvider.decoderFilter());
117 }
118
119 @Override
120 public WebSocketExtensionData newReponseData() {
121 return new WebSocketExtensionData(extensionName, Collections.<String, String>emptyMap());
122 }
123 }
124
125 }