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;
17  
18  import io.netty.handler.codec.http.HttpHeaderNames;
19  import io.netty.handler.codec.http.HttpHeaderValues;
20  import io.netty.handler.codec.http.HttpHeaders;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Map.Entry;
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  public final class WebSocketExtensionUtil {
32  
33      private static final String EXTENSION_SEPARATOR = ",";
34      private static final String PARAMETER_SEPARATOR = ";";
35      private static final char PARAMETER_EQUAL = '=';
36  
37      private static final Pattern PARAMETER = Pattern.compile("^([^=]+)(=[\\\"]?([^\\\"]+)[\\\"]?)?$");
38  
39      static boolean isWebsocketUpgrade(HttpHeaders headers) {
40          return headers.containsValue(HttpHeaderNames.CONNECTION, HttpHeaderValues.UPGRADE, true) &&
41                  headers.contains(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true);
42      }
43  
44      public static List<WebSocketExtensionData> extractExtensions(String extensionHeader) {
45          String[] rawExtensions = extensionHeader.split(EXTENSION_SEPARATOR);
46          if (rawExtensions.length > 0) {
47              List<WebSocketExtensionData> extensions = new ArrayList<WebSocketExtensionData>(rawExtensions.length);
48              for (String rawExtension : rawExtensions) {
49                  String[] extensionParameters = rawExtension.split(PARAMETER_SEPARATOR);
50                  String name = extensionParameters[0].trim();
51                  Map<String, String> parameters;
52                  if (extensionParameters.length > 1) {
53                      parameters = new HashMap<String, String>(extensionParameters.length - 1);
54                      for (int i = 1; i < extensionParameters.length; i++) {
55                          String parameter = extensionParameters[i].trim();
56                          Matcher parameterMatcher = PARAMETER.matcher(parameter);
57                          if (parameterMatcher.matches() && parameterMatcher.group(1) != null) {
58                              parameters.put(parameterMatcher.group(1), parameterMatcher.group(3));
59                          }
60                      }
61                  } else {
62                      parameters = Collections.emptyMap();
63                  }
64                  extensions.add(new WebSocketExtensionData(name, parameters));
65              }
66              return extensions;
67          } else {
68              return Collections.emptyList();
69          }
70      }
71  
72      static String appendExtension(String currentHeaderValue, String extensionName,
73              Map<String, String> extensionParameters) {
74  
75          StringBuilder newHeaderValue = new StringBuilder(
76                  currentHeaderValue != null ? currentHeaderValue.length() : extensionName.length() + 1);
77          if (currentHeaderValue != null && !currentHeaderValue.trim().isEmpty()) {
78              newHeaderValue.append(currentHeaderValue);
79              newHeaderValue.append(EXTENSION_SEPARATOR);
80          }
81          newHeaderValue.append(extensionName);
82          for (Entry<String, String> extensionParameter : extensionParameters.entrySet()) {
83              newHeaderValue.append(PARAMETER_SEPARATOR);
84              newHeaderValue.append(extensionParameter.getKey());
85              if (extensionParameter.getValue() != null) {
86                  newHeaderValue.append(PARAMETER_EQUAL);
87                  newHeaderValue.append(extensionParameter.getValue());
88              }
89          }
90          return newHeaderValue.toString();
91      }
92  
93      private WebSocketExtensionUtil() {
94          // Unused
95      }
96  }