Module org.htmlunit

Class HttpClientConverter

java.lang.Object
org.htmlunit.httpclient.HttpClientConverter

public final class HttpClientConverter extends Object
Helper methods to convert from/to HttpClient.
Author:
Ronald Brill
  • Method Details

    • registrableDomain

      public static String registrableDomain(String host)
      Returns the "registrable domain" (effective TLD+1) for the given host, per the public suffix list (https://publicsuffix.org/). Backed by Apache HttpClient's own PublicSuffixMatcher, 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. a co.uk-style suffix, or a private-section entry like github.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

      public static org.apache.http.cookie.CookieOrigin buildCookieOrigin(URL url)
      Helper that builds a CookieOrigin.
      Parameters:
      url - the url to be used
      Returns:
      the new CookieOrigin
    • replaceForCookieIfNecessary

      public static URL replaceForCookieIfNecessary(URL url)
      CookieOrigin doesn'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 - the Set-Cookie header value to parse
      pageUrl - the URL of the page from which the cookie originates
      browserVersion - the browser version used to determine cookie parsing behavior
      Returns:
      a list of parsed Cookie instances
      Throws:
      org.apache.http.cookie.MalformedCookieException - if the cookie string does not conform to the cookie specification
    • toHttpClient

      public static List<org.apache.http.cookie.Cookie> toHttpClient(Collection<Cookie> cookies)
      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 from
      normalizedUrl - the url to match against
      browserVersion - the BrowserVersion
      matches - the set to add