Module org.htmlunit

Class CorsLoader

java.lang.Object
org.htmlunit.javascript.host.fetch.CorsLoader

public final class CorsLoader extends Object
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
The network part of fetch() and XMLHttpRequest for requests in 'cors' mode: the CORS checks, the preflight requests and the redirect handling of the fetch standard.
Author:
Ronald Brill
See Also:
  • Constructor Details

    • CorsLoader

      public CorsLoader(WebWindow webWindow, CorsLoader.CredentialsMode credentialsMode, CorsLoader.RedirectMode redirectMode, int timeout)
      Ctor.
      Parameters:
      webWindow - the window of the page that sends the request
      credentialsMode - the credentials mode
      redirectMode - the redirect mode
      timeout - the timeout for the preflight requests (0 means no timeout)
    • CorsLoader

      public CorsLoader(WebWindow webWindow, CorsLoader.CredentialsMode credentialsMode, CorsLoader.RedirectMode redirectMode, int timeout, CorsLoader.RequestMode requestMode)
      Ctor.
      Parameters:
      webWindow - the window of the page that sends the request
      credentialsMode - the credentials mode
      redirectMode - the redirect mode
      timeout - the timeout for the preflight requests (0 means no timeout)
      requestMode - the request mode
  • Method Details

    • ensureConnectionAllowed

      public void ensureConnectionAllowed(URL url, URL reportedUrl) throws IOException
      Checks the connect-src directive of the Content-Security-Policy of the page.
      Parameters:
      url - the url to connect to (the request url or a redirect target)
      reportedUrl - the url reported in case of a violation (the url of the request)
      Throws:
      IOException - if the connection is not allowed
    • load

      public CorsLoader.Result load(WebRequest webRequest, boolean sameOrigin) throws IOException
      Loads the response of the request and follows the redirects the way the fetch standard describes it for a request in cors mode.
      • only http and https locations are followed, not more than 20 times
      • as soon as a url of another origin is involved, all responses (also the ones that redirect) have to pass the CORS check and cookies are only sent if credentials are included
      • the Origin header is the one of the page; after a redirect from one foreign origin to another one it is 'null'
      • a redirect to a foreign url with credentials is a network error
      • 301/302 (for POST) and 303 switch to GET and drop the body; 307/308 keep method and body
      • the Authorization header and the credentials of the request are dropped if the redirect leaves the origin
      The preflight of the (first) request has to be done by the caller.
      Parameters:
      webRequest - the request
      sameOrigin - whether the url of the request has the origin of the page
      Returns:
      the result
      Throws:
      IOException - in case of a network error
    • serializeOrigin

      public static String serializeOrigin(URL url)
      Serializes the origin of the url (scheme, host and port).
      Parameters:
      url - the url
      Returns:
      the origin
    • isCorsAllowed

      public boolean isCorsAllowed(WebResponse response, String origin)
      The CORS check of the fetch standard.
      Parameters:
      response - the response
      origin - the origin sent with the request
      Returns:
      whether the response passes the check
    • sendPreflight

      public WebResponse sendPreflight(WebRequest request) throws IOException
      Sends the preflight request for the given request.
      Parameters:
      request - the request
      Returns:
      the response of the preflight request
      Throws:
      IOException - in case of a network error
    • isPreflight

      public static boolean isPreflight(WebRequest request)
      Returns whether the request requires a preflight request (if it is a request to another origin).
      Parameters:
      request - the request
      Returns:
      whether the request requires a preflight request
    • isPreflightAuthorized

      public boolean isPreflightAuthorized(WebRequest request, WebResponse preflightResponse)
      Checks the response of the preflight request.
      Parameters:
      request - the request
      preflightResponse - the response of the preflight request
      Returns:
      whether the request is allowed
    • isForbiddenResponseHeader

      public static boolean isForbiddenResponseHeader(String name)
      Set-Cookie and Set-Cookie2 are never exposed to scripts, otherwise HttpOnly cookies are readable.
      Parameters:
      name - the header name
      Returns:
      true if the header is a forbidden response header
      See Also:
    • isExposedResponseHeader

      public static boolean isExposedResponseHeader(WebResponse response, String name, boolean corsTainting, boolean includeCredentials)
      Returns whether the response header is visible for scripts. Forbidden response headers are never visible; for a response of a cross-origin request (response tainting 'cors') only the CORS-safelisted response headers and the headers listed in Access-Control-Expose-Headers are visible.
      Parameters:
      response - the response
      name - the header name
      corsTainting - whether the response has the response tainting 'cors'
      includeCredentials - whether the request included credentials (the wildcard is not supported then)
      Returns:
      true if the header is visible
      See Also: