java.lang.Object
org.htmlunit.javascript.host.fetch.CorsLoader
INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.
The network part of
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe credentials mode of the request.static final classThrown if the CORS check fails.static final classA redirect that is not allowed (or not possible); this is a network error.static enumThe redirect mode of the request.static enumThe mode of the request (the modes 'navigate' and 'websocket' are not supported here).static final recordThe result ofload(WebRequest, boolean). -
Constructor Summary
ConstructorsConstructorDescriptionCorsLoader(WebWindow webWindow, CorsLoader.CredentialsMode credentialsMode, CorsLoader.RedirectMode redirectMode, int timeout) Ctor.CorsLoader(WebWindow webWindow, CorsLoader.CredentialsMode credentialsMode, CorsLoader.RedirectMode redirectMode, int timeout, CorsLoader.RequestMode requestMode) Ctor. -
Method Summary
Modifier and TypeMethodDescriptionvoidensureConnectionAllowed(URL url, URL reportedUrl) Checks the connect-src directive of the Content-Security-Policy of the page.booleanisCorsAllowed(WebResponse response, String origin) The CORS check of the fetch standard.static booleanisExposedResponseHeader(WebResponse response, String name, boolean corsTainting, boolean includeCredentials) Returns whether the response header is visible for scripts.static booleanSet-Cookie and Set-Cookie2 are never exposed to scripts, otherwise HttpOnly cookies are readable.static booleanisPreflight(WebRequest request) Returns whether the request requires a preflight request (if it is a request to another origin).booleanisPreflightAuthorized(WebRequest request, WebResponse preflightResponse) Checks the response of the preflight request.load(WebRequest webRequest, boolean sameOrigin) Loads the response of the request and follows the redirects the way the fetch standard describes it for a request in cors mode.sendPreflight(WebRequest request) Sends the preflight request for the given request.static StringserializeOrigin(URL url) Serializes the origin of the url (scheme, host and port).
-
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 requestcredentialsMode- the credentials moderedirectMode- the redirect modetimeout- 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 requestcredentialsMode- the credentials moderedirectMode- the redirect modetimeout- the timeout for the preflight requests (0 means no timeout)requestMode- the request mode
-
-
Method Details
-
ensureConnectionAllowed
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
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
- Parameters:
webRequest- the requestsameOrigin- whether the url of the request has the origin of the page- Returns:
- the result
- Throws:
IOException- in case of a network error
-
serializeOrigin
Serializes the origin of the url (scheme, host and port).- Parameters:
url- the url- Returns:
- the origin
-
isCorsAllowed
The CORS check of the fetch standard.- Parameters:
response- the responseorigin- the origin sent with the request- Returns:
- whether the response passes the check
-
sendPreflight
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
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
Checks the response of the preflight request.- Parameters:
request- the requestpreflightResponse- the response of the preflight request- Returns:
- whether the request is allowed
-
isForbiddenResponseHeader
Set-Cookie and Set-Cookie2 are never exposed to scripts, otherwise HttpOnly cookies are readable.- Parameters:
name- the header name- Returns:
trueif 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 responsename- the header namecorsTainting- whether the response has the response tainting 'cors'includeCredentials- whether the request included credentials (the wildcard is not supported then)- Returns:
trueif the header is visible- See Also:
-