View Javadoc
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    *   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.cookie;
17  
18  import io.netty5.util.internal.logging.InternalLogger;
19  import io.netty5.util.internal.logging.InternalLoggerFactory;
20  
21  import java.nio.CharBuffer;
22  
23  import static io.netty5.handler.codec.http.cookie.CookieUtil.firstInvalidCookieNameOctet;
24  import static io.netty5.handler.codec.http.cookie.CookieUtil.firstInvalidCookieValueOctet;
25  import static io.netty5.handler.codec.http.cookie.CookieUtil.unwrapValue;
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              logger.debug("Skipping cookie because starting quotes are not properly balanced in '{}'",
55                      wrappedValue);
56              return null;
57          }
58  
59          final String name = header.substring(nameBegin, nameEnd);
60  
61          int invalidOctetPos;
62          if (strict && (invalidOctetPos = firstInvalidCookieNameOctet(name)) >= 0) {
63              if (logger.isDebugEnabled()) {
64                  logger.debug("Skipping cookie because name '{}' contains invalid char '{}'",
65                          name, name.charAt(invalidOctetPos));
66              }
67              return null;
68          }
69  
70          final boolean wrap = unwrappedValue.length() != valueEnd - valueBegin;
71  
72          if (strict && (invalidOctetPos = firstInvalidCookieValueOctet(unwrappedValue)) >= 0) {
73              if (logger.isDebugEnabled()) {
74                  logger.debug("Skipping cookie because value '{}' contains invalid char '{}'",
75                          unwrappedValue, unwrappedValue.charAt(invalidOctetPos));
76              }
77              return null;
78          }
79  
80          DefaultCookie cookie = new DefaultCookie(name, unwrappedValue.toString());
81          cookie.setWrap(wrap);
82          return cookie;
83      }
84  }