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