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.netty5.handler.codec.http.websocketx.extensions.compression;
17  
18  import io.netty5.handler.codec.compression.ZlibCodecFactory;
19  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketClientExtension;
20  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker;
21  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketExtensionData;
22  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
23  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
24  import io.netty5.handler.codec.http.websocketx.extensions.WebSocketExtensionFilterProvider;
25  
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map.Entry;
29  import java.util.Objects;
30  
31  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.CLIENT_MAX_WINDOW;
32  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.CLIENT_NO_CONTEXT;
33  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.MAX_WINDOW_SIZE;
34  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.MIN_WINDOW_SIZE;
35  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.PERMESSAGE_DEFLATE_EXTENSION;
36  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.SERVER_MAX_WINDOW;
37  import static io.netty5.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker.SERVER_NO_CONTEXT;
38  
39  /**
40   * <a href="https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-18">permessage-deflate</a>
41   * handshake implementation.
42   */
43  public final class PerMessageDeflateClientExtensionHandshaker implements WebSocketClientExtensionHandshaker {
44  
45      private final int compressionLevel;
46      private final boolean allowClientWindowSize;
47      private final int requestedServerWindowSize;
48      private final boolean allowClientNoContext;
49      private final boolean requestedServerNoContext;
50      private final WebSocketExtensionFilterProvider extensionFilterProvider;
51  
52      /**
53       * Constructor with default configuration.
54       */
55      public PerMessageDeflateClientExtensionHandshaker() {
56          this(6, ZlibCodecFactory.isSupportingWindowSizeAndMemLevel(), MAX_WINDOW_SIZE, false, false);
57      }
58  
59      /**
60       * Constructor with custom configuration.
61       *
62       * @param compressionLevel
63       *            Compression level between 0 and 9 (default is 6).
64       * @param allowClientWindowSize
65       *            allows WebSocket server to customize the client inflater window size
66       *            (default is false).
67       * @param requestedServerWindowSize
68       *            indicates the requested sever window size to use if server inflater is customizable.
69       * @param allowClientNoContext
70       *            allows WebSocket server to activate client_no_context_takeover
71       *            (default is false).
72       * @param requestedServerNoContext
73       *            indicates if client needs to activate server_no_context_takeover
74       *            if server is compatible with (default is false).
75       */
76      public PerMessageDeflateClientExtensionHandshaker(int compressionLevel,
77              boolean allowClientWindowSize, int requestedServerWindowSize,
78              boolean allowClientNoContext, boolean requestedServerNoContext) {
79          this(compressionLevel, allowClientWindowSize, requestedServerWindowSize,
80               allowClientNoContext, requestedServerNoContext, WebSocketExtensionFilterProvider.DEFAULT);
81      }
82  
83      /**
84       * Constructor with custom configuration.
85       *
86       * @param compressionLevel
87       *            Compression level between 0 and 9 (default is 6).
88       * @param allowClientWindowSize
89       *            allows WebSocket server to customize the client inflater window size
90       *            (default is false).
91       * @param requestedServerWindowSize
92       *            indicates the requested sever window size to use if server inflater is customizable.
93       * @param allowClientNoContext
94       *            allows WebSocket server to activate client_no_context_takeover
95       *            (default is false).
96       * @param requestedServerNoContext
97       *            indicates if client needs to activate server_no_context_takeover
98       *            if server is compatible with (default is false).
99       * @param extensionFilterProvider
100      *            provides client extension filters for per message deflate encoder and decoder.
101      */
102     public PerMessageDeflateClientExtensionHandshaker(int compressionLevel,
103             boolean allowClientWindowSize, int requestedServerWindowSize,
104             boolean allowClientNoContext, boolean requestedServerNoContext,
105             WebSocketExtensionFilterProvider extensionFilterProvider) {
106 
107         if (requestedServerWindowSize > MAX_WINDOW_SIZE || requestedServerWindowSize < MIN_WINDOW_SIZE) {
108             throw new IllegalArgumentException(
109                     "requestedServerWindowSize: " + requestedServerWindowSize + " (expected: 8-15)");
110         }
111         if (compressionLevel < 0 || compressionLevel > 9) {
112             throw new IllegalArgumentException(
113                     "compressionLevel: " + compressionLevel + " (expected: 0-9)");
114         }
115         this.compressionLevel = compressionLevel;
116         this.allowClientWindowSize = allowClientWindowSize;
117         this.requestedServerWindowSize = requestedServerWindowSize;
118         this.allowClientNoContext = allowClientNoContext;
119         this.requestedServerNoContext = requestedServerNoContext;
120         this.extensionFilterProvider = Objects.requireNonNull(extensionFilterProvider, "extensionFilterProvider");
121     }
122 
123     @Override
124     public WebSocketExtensionData newRequestData() {
125         HashMap<String, String> parameters = new HashMap<>(4);
126         if (requestedServerNoContext) {
127             parameters.put(SERVER_NO_CONTEXT, null);
128         }
129         if (allowClientNoContext) {
130             parameters.put(CLIENT_NO_CONTEXT, null);
131         }
132         if (requestedServerWindowSize != MAX_WINDOW_SIZE) {
133             parameters.put(SERVER_MAX_WINDOW, Integer.toString(requestedServerWindowSize));
134         }
135         if (allowClientWindowSize) {
136             parameters.put(CLIENT_MAX_WINDOW, null);
137         }
138         return new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters);
139     }
140 
141     @Override
142     public WebSocketClientExtension handshakeExtension(WebSocketExtensionData extensionData) {
143         if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) {
144             return null;
145         }
146 
147         boolean succeed = true;
148         int clientWindowSize = MAX_WINDOW_SIZE;
149         int serverWindowSize = MAX_WINDOW_SIZE;
150         boolean serverNoContext = false;
151         boolean clientNoContext = false;
152 
153         Iterator<Entry<String, String>> parametersIterator =
154                 extensionData.parameters().entrySet().iterator();
155         while (succeed && parametersIterator.hasNext()) {
156             Entry<String, String> parameter = parametersIterator.next();
157 
158             if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
159                 // allowed client_window_size_bits
160                 if (allowClientWindowSize) {
161                     clientWindowSize = Integer.parseInt(parameter.getValue());
162                     if (clientWindowSize > MAX_WINDOW_SIZE || clientWindowSize < MIN_WINDOW_SIZE) {
163                         succeed = false;
164                     }
165                 } else {
166                     succeed = false;
167                 }
168             } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) {
169                 // acknowledged server_window_size_bits
170                 serverWindowSize = Integer.parseInt(parameter.getValue());
171                 if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) {
172                     succeed = false;
173                 }
174             } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
175                 // allowed client_no_context_takeover
176                 if (allowClientNoContext) {
177                     clientNoContext = true;
178                 } else {
179                     succeed = false;
180                 }
181             } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) {
182                 // acknowledged server_no_context_takeover
183                 serverNoContext = true;
184             } else {
185                 // unknown parameter
186                 succeed = false;
187             }
188         }
189 
190         if ((requestedServerNoContext && !serverNoContext) ||
191                 requestedServerWindowSize < serverWindowSize) {
192             succeed = false;
193         }
194 
195         if (succeed) {
196             return new PermessageDeflateExtension(serverNoContext, serverWindowSize,
197                     clientNoContext, clientWindowSize, extensionFilterProvider);
198         } else {
199             return null;
200         }
201     }
202 
203     private final class PermessageDeflateExtension implements WebSocketClientExtension {
204 
205         private final boolean serverNoContext;
206         private final int serverWindowSize;
207         private final boolean clientNoContext;
208         private final int clientWindowSize;
209         private final WebSocketExtensionFilterProvider extensionFilterProvider;
210 
211         @Override
212         public int rsv() {
213             return RSV1;
214         }
215 
216         PermessageDeflateExtension(boolean serverNoContext, int serverWindowSize,
217                 boolean clientNoContext, int clientWindowSize,
218                 WebSocketExtensionFilterProvider extensionFilterProvider) {
219             this.serverNoContext = serverNoContext;
220             this.serverWindowSize = serverWindowSize;
221             this.clientNoContext = clientNoContext;
222             this.clientWindowSize = clientWindowSize;
223             this.extensionFilterProvider = extensionFilterProvider;
224         }
225 
226         @Override
227         public WebSocketExtensionEncoder newExtensionEncoder() {
228             return new PerMessageDeflateEncoder(compressionLevel, clientWindowSize, clientNoContext,
229                                                 extensionFilterProvider.encoderFilter());
230         }
231 
232         @Override
233         public WebSocketExtensionDecoder newExtensionDecoder() {
234             return new PerMessageDeflateDecoder(serverNoContext, extensionFilterProvider.decoderFilter());
235         }
236     }
237 
238 }