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, Vidya m
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addMatching(Set<Cookie> cookies, URL normalizedUrl, BrowserVersion browserVersion, Set<Cookie> matches)
    Adds all matching cookies to the provided set.
    static org.apache.http.cookie.CookieOrigin
    Helper that builds a CookieOrigin.
    static boolean
    Same-site comparison per the HTML "same site" algorithm: same scheme and same registrable domain (eTLD+1), ignoring port and subdomain.
    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).
    static List<Cookie>
    parseCookie(String cookieString, URL pageUrl, BrowserVersion browserVersion)
    Parses the specified cookie string into HtmlUnit cookie objects.
    static String
    Returns the "registrable domain" (effective TLD+1) for the given host, per the public suffix list (https://publicsuffix.org/).
    static URL
    CookieOrigin doesn't like empty hosts and negative ports, but these things happen if we're dealing with a local file.
    static boolean
    shadowsSecureCookie(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>
    Converts the specified collection of cookies into a collection of HttpClient cookies.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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)
    • isSameSite

      public static boolean isSameSite(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.
      Parameters:
      a - first URL
      b - 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

      public static boolean shadowsSecureCookie(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.
      Parameters:
      cookie - the new cookie
      existingCookies - the stored cookies
      Returns:
      true if the cookie has to be ignored
    • 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