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    *   http://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 static io.netty.handler.codec.http.websocketx.extensions.compression.
19          DeflateFrameServerExtensionHandshaker.*;
20  import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtension;
21  import io.netty.handler.codec.http.websocketx.extensions.WebSocketClientExtensionHandshaker;
22  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData;
23  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionDecoder;
24  import io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionEncoder;
25  
26  import java.util.Collections;
27  
28  /**
29   * <a href="https://tools.ietf.org/id/draft-tyoshino-hybi-websocket-perframe-deflate-06.txt">perframe-deflate</a>
30   * handshake implementation.
31   */
32  public final class DeflateFrameClientExtensionHandshaker implements WebSocketClientExtensionHandshaker {
33  
34      private final int compressionLevel;
35      private final boolean useWebkitExtensionName;
36  
37      /**
38       * Constructor with default configuration.
39       */
40      public DeflateFrameClientExtensionHandshaker(boolean useWebkitExtensionName) {
41          this(6, useWebkitExtensionName);
42      }
43  
44      /**
45       * Constructor with custom configuration.
46       *
47       * @param compressionLevel
48       *            Compression level between 0 and 9 (default is 6).
49       */
50      public DeflateFrameClientExtensionHandshaker(int compressionLevel, boolean useWebkitExtensionName) {
51          if (compressionLevel < 0 || compressionLevel > 9) {
52              throw new IllegalArgumentException(
53                      "compressionLevel: " + compressionLevel + " (expected: 0-9)");
54          }
55          this.compressionLevel = compressionLevel;
56          this.useWebkitExtensionName = useWebkitExtensionName;
57      }
58  
59      @Override
60      public WebSocketExtensionData newRequestData() {
61          return new WebSocketExtensionData(
62                  useWebkitExtensionName ? X_WEBKIT_DEFLATE_FRAME_EXTENSION : DEFLATE_FRAME_EXTENSION,
63                  Collections.<String, String>emptyMap());
64      }
65  
66      @Override
67      public WebSocketClientExtension handshakeExtension(WebSocketExtensionData extensionData) {
68          if (!X_WEBKIT_DEFLATE_FRAME_EXTENSION.equals(extensionData.name()) &&
69              !DEFLATE_FRAME_EXTENSION.equals(extensionData.name())) {
70              return null;
71          }
72  
73          if (extensionData.parameters().isEmpty()) {
74              return new DeflateFrameClientExtension(compressionLevel);
75          } else {
76              return null;
77          }
78      }
79  
80      private static class DeflateFrameClientExtension implements WebSocketClientExtension {
81  
82          private final int compressionLevel;
83  
84          public DeflateFrameClientExtension(int compressionLevel) {
85              this.compressionLevel = compressionLevel;
86          }
87  
88          @Override
89          public int rsv() {
90              return RSV1;
91          }
92  
93          @Override
94          public WebSocketExtensionEncoder newExtensionEncoder() {
95              return new PerFrameDeflateEncoder(compressionLevel, 15, false);
96          }
97  
98          @Override
99          public WebSocketExtensionDecoder newExtensionDecoder() {
100             return new PerFrameDeflateDecoder(false);
101         }
102     }
103 
104 }