View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.impl.cookie;
29  
30  import org.apache.hc.client5.http.cookie.Cookie;
31  import org.apache.hc.client5.http.cookie.CookieOrigin;
32  import org.apache.hc.client5.http.cookie.CookieSpec;
33  import org.apache.hc.client5.http.cookie.CookieSpecFactory;
34  import org.apache.hc.client5.http.cookie.MalformedCookieException;
35  import org.apache.hc.client5.http.psl.PublicSuffixMatcher;
36  import org.apache.hc.client5.http.utils.DateUtils;
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.http.protocol.HttpContext;
40  
41  /**
42   * {@link CookieSpecFactory} implementation that provides an instance of
43   * RFC 6265 conformant cookie policy. The instance returned by this factory
44   * can be shared by multiple threads.
45   *
46   * @since 4.4
47   */
48  @Contract(threading = ThreadingBehavior.SAFE)
49  public class RFC6265CookieSpecFactory implements CookieSpecFactory {
50  
51      public enum CompatibilityLevel {
52          STRICT,
53          RELAXED,
54          IE_MEDIUM_SECURITY
55      }
56  
57      private final CompatibilityLevel compatibilityLevel;
58      private final PublicSuffixMatcher publicSuffixMatcher;
59  
60      private volatile CookieSpec cookieSpec;
61  
62      public RFC6265CookieSpecFactory(
63              final CompatibilityLevel compatibilityLevel,
64              final PublicSuffixMatcher publicSuffixMatcher) {
65          super();
66          this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.RELAXED;
67          this.publicSuffixMatcher = publicSuffixMatcher;
68      }
69  
70      public RFC6265CookieSpecFactory(final PublicSuffixMatcher publicSuffixMatcher) {
71          this(CompatibilityLevel.RELAXED, publicSuffixMatcher);
72      }
73  
74      public RFC6265CookieSpecFactory() {
75          this(CompatibilityLevel.RELAXED, null);
76      }
77  
78      @Override
79      public CookieSpec create(final HttpContext context) {
80          if (cookieSpec == null) {
81              synchronized (this) {
82                  if (cookieSpec == null) {
83                      switch (this.compatibilityLevel) {
84                          case STRICT:
85                              this.cookieSpec = new RFC6265StrictSpec(
86                                      BasicPathHandler.INSTANCE,
87                                      PublicSuffixDomainFilter.decorate(
88                                              BasicDomainHandler.INSTANCE, this.publicSuffixMatcher),
89                                      BasicMaxAgeHandler.INSTANCE,
90                                      BasicSecureHandler.INSTANCE,
91                                      BasicHttpOnlyHandler.INSTANCE,
92                                      new BasicExpiresHandler(DateUtils.STANDARD_PATTERNS));
93                              break;
94                          case IE_MEDIUM_SECURITY:
95                              this.cookieSpec = new RFC6265LaxSpec(
96                                      new BasicPathHandler() {
97                                          @Override
98                                          public void validate(
99                                                  final Cookie cookie,
100                                                 final CookieOrigin origin) throws MalformedCookieException {
101                                             // No validation
102                                         }
103                                     },
104                                     PublicSuffixDomainFilter.decorate(
105                                             BasicDomainHandler.INSTANCE, this.publicSuffixMatcher),
106                                     BasicMaxAgeHandler.INSTANCE,
107                                     BasicSecureHandler.INSTANCE,
108                                     BasicHttpOnlyHandler.INSTANCE,
109                                     new BasicExpiresHandler(DateUtils.STANDARD_PATTERNS));
110                             break;
111                         default:
112                             this.cookieSpec = new RFC6265LaxSpec(
113                                     BasicPathHandler.INSTANCE,
114                                     PublicSuffixDomainFilter.decorate(
115                                             BasicDomainHandler.INSTANCE, this.publicSuffixMatcher),
116                                     LaxMaxAgeHandler.INSTANCE,
117                                     BasicSecureHandler.INSTANCE,
118                                     LaxExpiresHandler.INSTANCE);
119                     }
120                 }
121             }
122         }
123         return this.cookieSpec;
124     }
125 
126 }