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.http.impl.cookie;
29
30 import org.apache.http.annotation.Contract;
31 import org.apache.http.annotation.ThreadingBehavior;
32 import org.apache.http.conn.util.PublicSuffixMatcher;
33 import org.apache.http.cookie.Cookie;
34 import org.apache.http.cookie.CookieOrigin;
35 import org.apache.http.cookie.CookieSpec;
36 import org.apache.http.cookie.CookieSpecProvider;
37 import org.apache.http.cookie.MalformedCookieException;
38 import org.apache.http.protocol.HttpContext;
39
40 /**
41 * {@link org.apache.http.cookie.CookieSpecProvider} implementation that provides an instance of
42 * RFC 6265 conformant cookie policy. The instance returned by this factory can be shared by
43 * multiple threads.
44 *
45 * @since 4.4
46 */
47 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
48 public class RFC6265CookieSpecProvider implements CookieSpecProvider {
49
50 public enum CompatibilityLevel {
51 STRICT,
52 RELAXED,
53 IE_MEDIUM_SECURITY
54 }
55
56 private final CompatibilityLevel compatibilityLevel;
57 private final PublicSuffixMatcher publicSuffixMatcher;
58
59 private volatile CookieSpec cookieSpec;
60
61 public RFC6265CookieSpecProvider(
62 final CompatibilityLevel compatibilityLevel,
63 final PublicSuffixMatcher publicSuffixMatcher) {
64 super();
65 this.compatibilityLevel = compatibilityLevel != null ? compatibilityLevel : CompatibilityLevel.RELAXED;
66 this.publicSuffixMatcher = publicSuffixMatcher;
67 }
68
69 public RFC6265CookieSpecProvider(final PublicSuffixMatcher publicSuffixMatcher) {
70 this(CompatibilityLevel.RELAXED, publicSuffixMatcher);
71 }
72
73 public RFC6265CookieSpecProvider() {
74 this(CompatibilityLevel.RELAXED, null);
75 }
76
77 @Override
78 public CookieSpec create(final HttpContext context) {
79 if (cookieSpec == null) {
80 synchronized (this) {
81 if (cookieSpec == null) {
82 switch (this.compatibilityLevel) {
83 case STRICT:
84 this.cookieSpec = new RFC6265StrictSpec(
85 new BasicPathHandler(),
86 PublicSuffixDomainFilter.decorate(
87 new BasicDomainHandler(), this.publicSuffixMatcher),
88 new BasicMaxAgeHandler(),
89 new BasicSecureHandler(),
90 new BasicExpiresHandler(RFC6265StrictSpec.DATE_PATTERNS));
91 break;
92 case IE_MEDIUM_SECURITY:
93 this.cookieSpec = new RFC6265LaxSpec(
94 new BasicPathHandler() {
95 @Override
96 public void validate(
97 final Cookie cookie,
98 final CookieOrigin origin) throws MalformedCookieException {
99 // No validation
100 }
101 },
102 PublicSuffixDomainFilter.decorate(
103 new BasicDomainHandler(), this.publicSuffixMatcher),
104 new BasicMaxAgeHandler(),
105 new BasicSecureHandler(),
106 new BasicExpiresHandler(RFC6265StrictSpec.DATE_PATTERNS));
107 break;
108 default:
109 this.cookieSpec = new RFC6265LaxSpec(
110 new BasicPathHandler(),
111 PublicSuffixDomainFilter.decorate(
112 new BasicDomainHandler(), this.publicSuffixMatcher),
113 new LaxMaxAgeHandler(),
114 new BasicSecureHandler(),
115 new LaxExpiresHandler());
116 }
117 }
118 }
119 }
120 return this.cookieSpec;
121 }
122
123 }
124