Likvido.Http
1.2.0
See the version list below for details.
dotnet add package Likvido.Http --version 1.2.0
NuGet\Install-Package Likvido.Http -Version 1.2.0
<PackageReference Include="Likvido.Http" Version="1.2.0" />
<PackageVersion Include="Likvido.Http" Version="1.2.0" />
<PackageReference Include="Likvido.Http" />
paket add Likvido.Http --version 1.2.0
#r "nuget: Likvido.Http, 1.2.0"
#:package Likvido.Http@1.2.0
#addin nuget:?package=Likvido.Http&version=1.2.0
#tool nuget:?package=Likvido.Http&version=1.2.0
Likvido.Http

HTTP client helpers for Likvido services. It depends only on Microsoft.Extensions.Http and
Microsoft.Extensions.Logging.Abstractions, so web projects, robots and other libraries can all use it.
Guarded clients
Use a guarded client for every server-side fetch of a URL that comes from outside data, such as a
download link in a third party's reply. Register it with AddGuardedHttpClient instead of
AddHttpClient:
services.AddGuardedHttpClient<IPdfService, PdfService>(
new OutboundRequestPolicy(
["https://api.pdf.co", "https://pdf-temp-files.s3.us-west-2.amazonaws.com"],
maxResponseBytes: 20 * 1024 * 1024))
.ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(30))
.AddStandardResilienceHandler();
A guarded client:
- sends a request only to an origin its policy allows, and refuses any other before anything is sent;
- opens a connection only to a public IP address. It checks every address the host resolves to and connects to the address it checked, so DNS rebinding cannot swap in a private address;
- follows no redirects, and refuses a redirect response;
- refuses a response whose
Content-Lengthis over the cap, and stops reading any body once it passes the cap. When a response is decompressed, itsContent-Lengthis gone, and the decompressed bytes read are counted; - uses no proxy, no cookies and no HTTP/3.
The policy
Origins are exact: a scheme, a host and, when it is not the default, a port, such as
https://api.pdf.co. There are no wildcards. Listinghttps://example.comdoes not allowhttp://example.com; an http origin is allowed only when it is listed itself.maxResponseBytescaps the response body. With automatic decompression, it caps the decompressed body.AutomaticDecompressionis off by default. Set it for a host that sends compressed responses:new OutboundRequestPolicy(["http://regnskaber.virk.dk"], maxResponseBytes: 1024 * 1024) { AutomaticDecompression = DecompressionMethods.GZip }MaxConnectionsPerServerlimits how many connections the client opens to one server at a time. There is no limit by default. Set it for a client whose URLs an outsider chooses, so one slow host cannot tie up an unbounded number of sockets.
Any https origin
When the hosts cannot be listed in advance, such as an OAuth client's metadata document at a URL the client chooses, allow any https origin instead:
services.AddGuardedHttpClient(
"ClientMetadata",
OutboundRequestPolicy.ForAnyHttpsOrigin(maxResponseBytes: 64 * 1024, maxConnectionsPerServer: 8))
.ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(5));
Such a client sends a request to any https URL on the default port (443). It refuses an http URL and
an https URL with another port as UnlistedOrigin, before anything is sent. Every other check
still applies: the connection goes only to a public address, redirects are refused and the response
is capped. Automatic decompression is off.
A policy can also be built from the service provider, for example from configuration:
services.AddGuardedHttpClient<IAttachmentDownloader, AttachmentDownloader>(serviceProvider =>
new OutboundRequestPolicy(
[serviceProvider.GetRequiredService<IOptions<StorageOptions>>().Value.AccountOrigin],
maxResponseBytes: 25 * 1024 * 1024));
There are overloads for a typed client with an implementation (AddGuardedHttpClient<TClient, TImplementation>),
a typed client without one (AddGuardedHttpClient<TClient>) and a named client
(AddGuardedHttpClient(name, policy)). Each returns the client's IHttpClientBuilder.
Refusals
A refused request throws OutboundRequestRefusedException. Its Reason is one of UnlistedOrigin
(the policy does not allow the origin),
NonPublicAddress, ResponseTooLarge and Redirect, and its Origin is the scheme, the host and any non-default port. It
does not derive from HttpRequestException, so a resilience handler does not retry it.
The guard logs each refusal once, at Warning, in the category Likvido.Http.OutboundRequestGuard,
with the client's name, the origin, and the reason. It never logs the path or query string,
because a signed storage link carries its credential in the query string. Neither does the
exception's message.
Adding handlers
The guard is the client's primary handler, so it runs inside every handler the caller adds, such as
AddStandardResilienceHandler. It sees each request exactly as it is sent.
Do not call ConfigurePrimaryHttpMessageHandler or UseSocketsHttpHandler on a guarded client
outside tests: either replaces the guard.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Microsoft.Extensions.Http (>= 10.0.14)
- Microsoft.Extensions.Logging.Abstractions (>= 10.0.14)
-
net11.0
- Microsoft.Extensions.Http (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.