DocumentLoader Class

Loads the documents of the navigations of the browsing contexts (e.g. the src of an iframe, or location.href = url), the style sheets of the @import rules and, when a BrowsingContextOptions.ScriptEngine is set, the external scripts and the HTTP requests of fetch() and XMLHttpRequest (see DocumentRequest.Destination). The library never fetches anything by itself: the application provides a loader, for instance the HTTP loader returned by DocumentLoader.Create, which decides which resources are available.

public abstract class DocumentLoader

Remarks

The library uses the following headers of the responses (DocumentResource.Headers); the other headers are ignored:

  • Accept-CH, Critical-CH and Clear-Site-Data: the client hints that the origin of a top-level document opts into (see BrowsingContextOptions.AcceptCHCache).
  • Content-Type: the MIME type and the encoding of the resource.
  • Content-Disposition: a navigation to an attachment does not create a document.
  • Content-DPR: the density of an image, which divides its natural size (as in Chromium).
  • Last-Modified: Document.LastModified.
  • Content-Language: the default language of the elements (e.g. :lang()): its first language.
  • Document-Policy: expect-no-linked-resources disables the speculative parsing of the document (see BrowsingContextOptions.SpeculativeParsing).
  • Content-Security-Policy and Content-Security-Policy-Report-Only: the fetch directives of the subresources, the sandbox directive and the Trusted Types directives (see Document.TrustedTypesDirectives), whose violations are reported (the securitypolicyviolation event and the report-uri reports).
  • Referrer-Policy: the referrer policy of the document, which determines DocumentRequest.Referrer and Document.Referrer.
  • Refresh: the declarative refresh of the document (see BrowsingContextOptions.DeclarativeRefreshEnabled).
  • X-Content-Type-Options: nosniff blocks the scripts whose MIME type is not a JavaScript MIME type, and the style sheets whose MIME type is not text/css.

Constructors

NameDescription
DocumentLoader()

DocumentLoader()

protected DocumentLoader()

No documentation is available.

Methods

NameDescription
ConnectWebSocketAsync(DocumentRequest, CancellationToken)Opens the connection of a WebSocket (https://fetch.spec.whatwg.org/#concept-websocket-connection-obtain) and sends its opening handshake (https://websockets.spec.whatwg.org/#concept-websocket-establish): an HTTP/1.1 GET request to DocumentRequest.Url (an http: or https: URL, the URL of the WebSocket with the http or https scheme) whose DocumentRequest.Mode is RequestMode.WebSocket, with the headers of DocumentRequest.Headers (Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Origin...) and the headers of the connection (Host, Cookie...). Returns the response, or null when the connection cannot be established (a network error). The library validates the handshake: the response must have the 101 status, its headers, and the connection on which the frames are read and written (DocumentResource.Connection). The loader must not follow the redirects. The default implementation returns null: the WebSockets cannot connect.
Create(HttpClient?)Returns a loader that gets the documents with HTTP GET requests (only http: and https: URLs; the other URLs are network errors). It sends and stores the cookies of DocumentRequest.CookieJar, and follows the redirects itself (up to 20) when the client does not.
LoadAsync(DocumentRequest, CancellationToken)Sends a request of the fetch algorithm of the library (https://fetch.spec.whatwg.org/#http-network-fetch): the document of a navigation, a subresource of a document (e.g. an external script, a style sheet, an image, see DocumentRequest.Destination), or a request of fetch() or XMLHttpRequest. The library follows the redirects (DocumentRequest.FollowRedirects is false: the loader returns the redirect responses) and checks each response (CORS, mixed content, content security policies...). Returns null when the resource is not available: the navigation then results in an error document with an opaque origin, as a network error (the error event is fired at a script or style element). An exception (including a cancellation) is also a network error.

ConnectWebSocketAsync(DocumentRequest, CancellationToken)

BrowsingContextOptions.cs

Opens the connection of a WebSocket (https://fetch.spec.whatwg.org/#concept-websocket-connection-obtain) and sends its opening handshake (https://websockets.spec.whatwg.org/#concept-websocket-establish): an HTTP/1.1 GET request to DocumentRequest.Url (an http: or https: URL, the URL of the WebSocket with the http or https scheme) whose DocumentRequest.Mode is RequestMode.WebSocket, with the headers of DocumentRequest.Headers (Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Origin...) and the headers of the connection (Host, Cookie...). Returns the response, or null when the connection cannot be established (a network error). The library validates the handshake: the response must have the 101 status, its headers, and the connection on which the frames are read and written (DocumentResource.Connection). The loader must not follow the redirects. The default implementation returns null: the WebSockets cannot connect.

public virtual ValueTask<DocumentResource?> ConnectWebSocketAsync(DocumentRequest request, CancellationToken cancellationToken)

Parameters

request DocumentRequest
cancellationToken CancellationToken

Returns

ValueTask<DocumentResource?>

Remarks

The method is called from a task of the DomEventLoop of the global object of the WebSocket (a window or a worker). cancellationToken is canceled when the WebSocket is closed (or its document is unloaded) before the connection is established. The library disposes the connection.

Create(HttpClient?)

BrowsingContextOptions.cs

Returns a loader that gets the documents with HTTP GET requests (only http: and https: URLs; the other URLs are network errors). It sends and stores the cookies of DocumentRequest.CookieJar, and follows the redirects itself (up to 20) when the client does not.

public static DocumentLoader Create(HttpClient? httpClient = null)

Parameters

httpClient HttpClient?
The client used to send the requests. Its handler should not use its own cookies (UseCookies = false) when BrowsingContextOptions.CookieJar is set. When null, a shared loader is returned, whose client does not follow the redirects, does not use cookies, and decompresses the responses.

Returns

DocumentLoader

Remarks

Any URL a document navigates to is requested (e.g. the src of its iframes). Do not use this loader with untrusted documents when the requests can reach private resources (server-side request forgery): use a client whose handler filters the destinations, or a custom DocumentLoader.

LoadAsync(DocumentRequest, CancellationToken)

Sends a request of the fetch algorithm of the library (https://fetch.spec.whatwg.org/#http-network-fetch): the document of a navigation, a subresource of a document (e.g. an external script, a style sheet, an image, see DocumentRequest.Destination), or a request of fetch() or XMLHttpRequest. The library follows the redirects (DocumentRequest.FollowRedirects is false: the loader returns the redirect responses) and checks each response (CORS, mixed content, content security policies...). Returns null when the resource is not available: the navigation then results in an error document with an opaque origin, as a network error (the error event is fired at a script or style element). An exception (including a cancellation) is also a network error.

public abstract ValueTask<DocumentResource?> LoadAsync(DocumentRequest request, CancellationToken cancellationToken)

Parameters

request DocumentRequest
cancellationToken CancellationToken

Returns

ValueTask<DocumentResource?>

Remarks

The method is called from a task of the DomEventLoop. When it completes asynchronously, the document is created by a task queued on completion (from any thread): use DomEventLoop.RunUntilIdleAsync to wait for it. cancellationToken is canceled when the navigation is aborted (e.g. another navigation of the browsing context starts, or the iframe is removed); the result of an aborted navigation is ignored.