查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2015 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 org.jboss.netty.handler.codec.http.cookie;
17  
18  import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieNameOctet;
19  import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.firstInvalidCookieValueOctet;
20  import static org.jboss.netty.handler.codec.http.cookie.CookieUtil.unwrapValue;
21  
22  import java.nio.CharBuffer;
23  
24  import org.jboss.netty.logging.InternalLogger;
25  import org.jboss.netty.logging.InternalLoggerFactory;
26  
27  /**
28   * Parent of Client and Server side cookie decoders
29   */
30  public abstract class CookieDecoder {
31  
32      private final InternalLogger logger = InternalLoggerFactory.getInstance(getClass());
33  
34      private final boolean strict;
35  
36      protected CookieDecoder(boolean strict) {
37          this.strict = strict;
38      }
39  
40      protected DefaultCookie initCookie(String header, int nameBegin, int nameEnd, int valueBegin, int valueEnd) {
41          if (nameBegin == -1 || nameBegin == nameEnd) {
42              logger.debug("Skipping cookie with null name");
43              return null;
44          }
45  
46          if (valueBegin == -1) {
47              logger.debug("Skipping cookie with null value");
48              return null;
49          }
50  
51          CharSequence wrappedValue = CharBuffer.wrap(header, valueBegin, valueEnd);
52          CharSequence unwrappedValue = unwrapValue(wrappedValue);
53          if (unwrappedValue == null) {
54              if (logger.isDebugEnabled()) {
55                  logger.debug("Skipping cookie because starting quotes are not properly balanced in '"
56                      + wrappedValue + "'");
57              }
58              return null;
59          }
60  
61          final String name = header.substring(nameBegin, nameEnd);
62  
63          int invalidOctetPos;
64          if (strict && (invalidOctetPos = firstInvalidCookieNameOctet(name)) >= 0) {
65              if (logger.isDebugEnabled()) {
66                  logger.debug("Skipping cookie because name '" + name + "' contains invalid char '"
67                      + name.charAt(invalidOctetPos) + "'");
68              }
69              return null;
70          }
71  
72          final boolean wrap = unwrappedValue.length() != valueEnd - valueBegin;
73  
74          if (strict && (invalidOctetPos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
75              if (logger.isDebugEnabled()) {
76                  logger.debug("Skipping cookie because value '" + unwrappedValue
77                      + "' contains invalid char '" + unwrappedValue.charAt(invalidOctetPos) + "'");
78              }
79              return null;
80          }
81  
82          DefaultCookie cookie = new DefaultCookie(name, unwrappedValue.toString());
83          cookie.setWrap(wrap);
84          return cookie;
85      }
86  }