java.lang.Object
org.htmlunit.httpclient.HttpClientConverter
Helper methods to convert from/to HttpClient.
- Author:
- Ronald Brill, Vidya m
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddMatching(Set<Cookie> cookies, URL normalizedUrl, BrowserVersion browserVersion, Set<Cookie> matches) Adds all matching cookies to the provided set.static org.apache.http.cookie.CookieOriginbuildCookieOrigin(URL url) Helper that builds a CookieOrigin.static booleanisSameSite(URL a, URL b) Same-site comparison per the HTML "same site" algorithm: same scheme and same registrable domain (eTLD+1), ignoring port and subdomain.static booleanisSecureCookieOrigin(org.apache.http.cookie.CookieOrigin origin) Returns whether the origin is handled as secure for cookies: https and, like the browsers, localhost and the loopback addresses (potentially trustworthy origins).parseCookie(String cookieString, URL pageUrl, BrowserVersion browserVersion) Parses the specified cookie string into HtmlUnit cookie objects.static StringregistrableDomain(String host) Returns the "registrable domain" (effective TLD+1) for the given host, per the public suffix list (https://publicsuffix.org/).static URLCookieOrigindoesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file.static booleanshadowsSecureCookie(Cookie cookie, Collection<Cookie> existingCookies) Returns whether a (non-secure) cookie from an insecure origin would overwrite or shadow one of the given secure cookies; such a cookie is ignored (RFC 6265bis section 5.7, step 16): a secure cookie with the same name, a domain that domain-matches the domain of the new cookie (or the other way round) and a path that is path-matched by the path of the new cookie.static List<org.apache.http.cookie.Cookie>toHttpClient(Collection<Cookie> cookies) Converts the specified collection of cookies into a collection of HttpClient cookies.
-
Method Details
-
registrableDomain
Returns the "registrable domain" (effective TLD+1) for the given host, per the public suffix list (https://publicsuffix.org/). Backed by Apache HttpClient's ownPublicSuffixMatcher, already on the classpath as part of the httpclient dependency this class already uses elsewhere for cookie handling - no new dependency needed, and it avoids the wrong results a naive "last two labels" heuristic would give for domains under multi-part public suffixes (e.g. aco.uk-style suffix, or a private-section entry likegithub.io).Note this is deliberately unrelated to
HtmlUnitDomainHandler's cookie domain-matching: RFC 6265 cookie domain-match is a simple suffix/subdomain check against a single cookie-supplied domain, not a registrable-domain computation between two hosts, so there is nothing to share between the two.- Parameters:
host- the host part of a URL- Returns:
- the registrable domain, or the host itself if it doesn't have one (e.g.
a bare public suffix, a single-label host like
localhost, or an IP address - which per the HTML "obtain a site" algorithm is always its own site, never run through the public suffix list at all)
-
isSameSite
Same-site comparison per the HTML "same site" algorithm: same scheme and same registrable domain (eTLD+1), ignoring port and subdomain.- Parameters:
a- first URLb- second URL- Returns:
- whether both URLs are considered "same-site"
-
isSecureCookieOrigin
public static boolean isSecureCookieOrigin(org.apache.http.cookie.CookieOrigin origin) Returns whether the origin is handled as secure for cookies: https and, like the browsers, localhost and the loopback addresses (potentially trustworthy origins).- Parameters:
origin- the origin- Returns:
- true if the origin is handled as secure
- See Also:
-
shadowsSecureCookie
Returns whether a (non-secure) cookie from an insecure origin would overwrite or shadow one of the given secure cookies; such a cookie is ignored (RFC 6265bis section 5.7, step 16): a secure cookie with the same name, a domain that domain-matches the domain of the new cookie (or the other way round) and a path that is path-matched by the path of the new cookie.- Parameters:
cookie- the new cookieexistingCookies- the stored cookies- Returns:
- true if the cookie has to be ignored
-
buildCookieOrigin
Helper that builds a CookieOrigin.- Parameters:
url- the url to be used- Returns:
- the new CookieOrigin
-
replaceForCookieIfNecessary
CookieOrigindoesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file. This method allows us to work around this limitation in HttpClient by feeding it a bogus host and port.- Parameters:
url- the URL to replace if necessary- Returns:
- the replacement URL, or the original URL if no replacement was necessary
-
parseCookie
public static List<Cookie> parseCookie(String cookieString, URL pageUrl, BrowserVersion browserVersion) throws org.apache.http.cookie.MalformedCookieException Parses the specified cookie string into HtmlUnit cookie objects.- Parameters:
cookieString- theSet-Cookieheader value to parsepageUrl- the URL of the page from which the cookie originatesbrowserVersion- the browser version used to determine cookie parsing behavior- Returns:
- a list of parsed
Cookieinstances - Throws:
org.apache.http.cookie.MalformedCookieException- if the cookie string does not conform to the cookie specification
-
toHttpClient
Converts the specified collection of cookies into a collection of HttpClient cookies.- Parameters:
cookies- the cookies to be converted- Returns:
- the specified cookies, as HttpClient cookies
-
addMatching
public static void addMatching(Set<Cookie> cookies, URL normalizedUrl, BrowserVersion browserVersion, Set<Cookie> matches) Adds all matching cookies to the provided set.- Parameters:
cookies- the cookies to select fromnormalizedUrl- the url to match againstbrowserVersion- theBrowserVersionmatches- the set to add
-