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