Table of Contents

Class HttpAssetProvider

Namespace
VpnHood.Net.Toolkit.Assets
Assembly
VpnHood.Net.Toolkit.dll

Provides the assets a web server serves, each at the base URL followed by the asset path, for a caller with nothing placed beside it.

public class HttpAssetProvider : IAssetProvider
Inheritance
HttpAssetProvider
Implements
Inherited Members

Remarks

Status 404 is an absent asset; any other failure is its own. The body is read as it arrives, forward and not buffered; disposing the stream gives the connection back.

Constructors

HttpAssetProvider(HttpClient, string)

Provides the assets a web server serves, each at the base URL followed by the asset path, for a caller with nothing placed beside it.

public HttpAssetProvider(HttpClient httpClient, string baseUrl)

Parameters

httpClient HttpClient

The HTTP client the requests go through; the caller keeps owning it.

baseUrl string

The URL the asset paths are appended to.

Remarks

Status 404 is an absent asset; any other failure is its own. The body is read as it arrives, forward and not buffered; disposing the stream gives the connection back.

Methods

OpenReadAsync(string, CancellationToken)

Opens the asset at a path, such as fonts/text.ttf.

public Task<Stream> OpenReadAsync(string assetPath, CancellationToken cancellationToken)

Parameters

assetPath string

The asset's path.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<Stream>

A task whose result is a new stream, which the caller owns and disposes.

Remarks

It is asynchronous because a page in a browser fetches over the network and cannot wait. Read the stream forward: whether it can seek is the platform's answer, not a promise; a file can, an HTTP body and an Android asset cannot. A caller that needs more than one pass opens the asset again where it can, and copies it into memory only where it cannot, so the cost is paid where it is decided. A caller that only asks, such as a language falling back to another, takes TryOpenReadAsync, which gives null for an absent asset.

Exceptions

AssetNotFoundException

The provider classes the asset as absent; exactly this type, as a file it finds and cannot open is another failure.

HttpRequestException

The request failed, or its status is a failure other than 404.