java.lang.Object
org.htmlunit.httpclient.HttpClientConverter
Helper methods to convert from/to HttpClient.
- Author:
- Ronald Brill
-
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.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 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)
-
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
-