1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.httpclient;
16
17 import java.net.IDN;
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.apache.http.conn.util.InetAddressUtils;
26 import org.apache.http.conn.util.PublicSuffixMatcher;
27 import org.apache.http.conn.util.PublicSuffixMatcherLoader;
28 import org.apache.http.cookie.ClientCookie;
29 import org.apache.http.cookie.Cookie;
30 import org.apache.http.cookie.CookieOrigin;
31 import org.apache.http.cookie.CookieSpec;
32 import org.apache.http.cookie.MalformedCookieException;
33 import org.apache.http.impl.cookie.BasicClientCookie;
34 import org.apache.http.message.BufferedHeader;
35 import org.apache.http.util.CharArrayBuffer;
36 import org.htmlunit.BrowserVersion;
37 import org.htmlunit.util.UrlUtils;
38
39
40
41
42
43
44 public final class HttpClientConverter {
45
46 private static final PublicSuffixMatcher PUBLIC_SUFFIX_MATCHER = PublicSuffixMatcherLoader.getDefault();
47
48 private HttpClientConverter() {
49
50 }
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public static String registrableDomain(final String host) {
73 if (InetAddressUtils.isIPv4Address(host) || InetAddressUtils.isIPv6Address(host)) {
74 return host;
75 }
76 final String domainRoot = IDN.toASCII(PUBLIC_SUFFIX_MATCHER.getDomainRoot(host));
77 return domainRoot == null ? host : domainRoot;
78 }
79
80
81
82
83
84
85 public static CookieOrigin buildCookieOrigin(final URL url) {
86 final URL normalizedUrl = replaceForCookieIfNecessary(url);
87
88 int port = normalizedUrl.getPort();
89 if (port == -1) {
90 port = normalizedUrl.getDefaultPort();
91 }
92
93 return new CookieOrigin(
94 normalizedUrl.getHost(),
95 port,
96 normalizedUrl.getPath(),
97 "https".equals(normalizedUrl.getProtocol()));
98 }
99
100
101
102
103
104
105
106
107
108 public static URL replaceForCookieIfNecessary(URL url) {
109 final String protocol = url.getProtocol();
110 final boolean file = "file".equals(protocol);
111 if (file) {
112 try {
113 url = UrlUtils.getUrlWithNewHostAndPort(url,
114 HtmlUnitBrowserCompatCookieSpec.LOCAL_FILESYSTEM_DOMAIN, 0);
115 }
116 catch (final MalformedURLException e) {
117 throw new RuntimeException(e);
118 }
119 }
120 return url;
121 }
122
123
124
125
126
127
128
129
130
131
132 public static List<org.htmlunit.http.Cookie> parseCookie(final String cookieString, final URL pageUrl,
133 final BrowserVersion browserVersion)
134 throws MalformedCookieException {
135 final CharArrayBuffer buffer = new CharArrayBuffer(cookieString.length() + 22);
136 buffer.append("Set-Cookie: ");
137 buffer.append(cookieString);
138
139 final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(browserVersion);
140 final List<Cookie> cookies = cookieSpec.parse(new BufferedHeader(buffer), buildCookieOrigin(pageUrl));
141
142 final List<org.htmlunit.http.Cookie> htmlUnitCookies = new ArrayList<>(cookies.size());
143 for (final Cookie cookie : cookies) {
144 final org.htmlunit.http.Cookie htmlUnitCookie = new HttpClientCookie((ClientCookie) cookie);
145 htmlUnitCookies.add(htmlUnitCookie);
146 }
147 return htmlUnitCookies;
148 }
149
150
151
152
153
154
155 public static List<Cookie> toHttpClient(final Collection<org.htmlunit.http.Cookie> cookies) {
156 final ArrayList<Cookie> array = new ArrayList<>(cookies.size());
157 for (final org.htmlunit.http.Cookie cookie : cookies) {
158 array.add(toHttpClient(cookie));
159 }
160 return array;
161 }
162
163
164
165
166
167
168
169
170 public static void addMatching(final Set<org.htmlunit.http.Cookie> cookies,
171 final URL normalizedUrl, final BrowserVersion browserVersion,
172 final Set<org.htmlunit.http.Cookie> matches) {
173 if (!cookies.isEmpty()) {
174 final CookieOrigin cookieOrigin = HttpClientConverter.buildCookieOrigin(normalizedUrl);
175 final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(browserVersion);
176 for (final org.htmlunit.http.Cookie cookie : cookies) {
177 if (cookieSpec.match(toHttpClient(cookie), cookieOrigin)) {
178 matches.add(cookie);
179 }
180 }
181 }
182 }
183
184 private static ClientCookie toHttpClient(final org.htmlunit.http.Cookie cookie) {
185 if (cookie instanceof HttpClientCookie) {
186 return ((HttpClientCookie) cookie).getHttpClientCookie();
187 }
188
189 final BasicClientCookie httpClientCookie = new BasicClientCookie(cookie.getName(),
190 cookie.getValue() == null ? "" : cookie.getValue());
191
192 httpClientCookie.setDomain(cookie.getDomain());
193
194 httpClientCookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
195
196 httpClientCookie.setPath(cookie.getPath());
197 httpClientCookie.setExpiryDate(cookie.getExpires());
198
199 httpClientCookie.setSecure(cookie.isSecure());
200 if (cookie.isHttpOnly()) {
201 httpClientCookie.setAttribute("httponly", "true");
202 }
203
204 if (cookie.getSameSite() != null) {
205 httpClientCookie.setAttribute("samesite", cookie.getSameSite());
206 }
207
208 return httpClientCookie;
209 }
210 }