1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.httpclient;
16
17 import java.util.Date;
18 import java.util.List;
19
20 import org.apache.http.client.CookieStore;
21 import org.apache.http.cookie.Cookie;
22
23 /**
24 * A {@link CookieStore} used for the response of a request to an insecure origin (http);
25 * like the browsers, a cookie that would overwrite or shadow a secure cookie is ignored
26 * (RFC 6265bis section 5.7, step 16). All other operations are delegated.
27 *
28 * @author Ronald Brill
29 * @see HttpClientConverter#shadowsSecureCookie(org.htmlunit.http.Cookie, java.util.Collection)
30 */
31 public final class HtmlUnitInsecureOriginCookieStore implements CookieStore {
32
33 private final CookieStore delegate_;
34
35 /**
36 * Ctor.
37 *
38 * @param delegate the cookie store
39 */
40 public HtmlUnitInsecureOriginCookieStore(final CookieStore delegate) {
41 delegate_ = delegate;
42 }
43
44 /**
45 * Adds the cookie if it does not overwrite or shadow a secure cookie.
46 * {@inheritDoc}
47 */
48 @Override
49 public void addCookie(final Cookie cookie) {
50 if (!cookie.isSecure() && HttpClientConverter.shadowsSecureCookie(cookie, delegate_.getCookies())) {
51 return;
52 }
53 delegate_.addCookie(cookie);
54 }
55
56 /**
57 * {@inheritDoc}
58 */
59 @Override
60 public List<Cookie> getCookies() {
61 return delegate_.getCookies();
62 }
63
64 /**
65 * {@inheritDoc}
66 */
67 @Override
68 public boolean clearExpired(final Date date) {
69 return delegate_.clearExpired(date);
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 public void clear() {
77 delegate_.clear();
78 }
79 }