pub struct ReqwestClient { /* private fields */ }did-web and crate feature reqwest and (crate features did-jwk or did-key or did-web) only.Expand description
An HttpClient over reqwest hardened for fetching DID documents from the public web.
ReqwestClient::new builds a client that:
- speaks HTTPS only, with the platform’s root certificates and
rustls; - follows no redirect, so a document is fetched from the URL its DID maps to and nowhere
else: a 3xx response is returned with its
LocationinHttpResponse::location, for a caller that follows redirects to fetch each target through this client again; - sends
Acceptwhen asked throughHttpClient::get_accepting, andAccept-Encoding: gzip, decoding a gzip-encoded body; - resolves host names itself and refuses loopback, private, link-local and other non-public addresses, so a DID cannot point the resolver at internal services;
- uses no proxy, whatever
HTTPS_PROXY,HTTP_PROXY,ALL_PROXYor the platform’s settings say: a proxy resolves the host itself, so it would reach the addresses refused above; - stops reading a body over
DEFAULT_MAX_BODYbytes, counted after decoding, so a small compressed body cannot expand past the limit; - times out after
DEFAULT_TIMEOUT.
ReqwestClient::from_client wraps a client the application configured itself, keeping
only the HTTPS-only rule, the refusal of non-public IP literals and the body limit; the
other protections are then the application’s. That is also how to fetch through an egress
proxy: configure it on that client, and have the proxy refuse non-public targets, since it
resolves the host names.
§Local development and tests
With the feature local-network, ReqwestClient::builder can let a client reach
a did:web served from the local machine or a private network, such as a test server on
127.0.0.1 or a development deployment in a docker-compose network. It is for
development and tests only; enable the feature under [dev-dependencies], or behind a
feature of the application’s own, so that a release build does not contain it.
dangerous_allow_loopbackallows127.0.0.0/8and::1, anddangerous_allow_private_networkallows RFC 1918 andfc00::/7. Neither takes abool: the choice is made in code.- The client resolves the host first. When every address is in an allowed range, it fetches
the URL over plain
http, on the port the URL names (443 when it names none), connecting to exactly the addresses it checked. Otherwise the rules above apply unchanged: a host with public and local addresses is held to them. - Link-local addresses (including cloud metadata endpoints such as
169.254.169.254) and every other non-public range stay refused; redirects, the timeout and the body limit are unchanged.
Implementations§
Source§impl ReqwestClient
impl ReqwestClient
Sourcepub const fn local_network(&self) -> LocalNetwork
pub const fn local_network(&self) -> LocalNetwork
The local ranges this client may reach; LocalNetwork::NONE unless built with
ReqwestClient::builder.
Source§impl ReqwestClient
impl ReqwestClient
Sourcepub const DEFAULT_MAX_BODY: usize
pub const DEFAULT_MAX_BODY: usize
The largest body new reads: 1 MiB, far above any DID document.
Sourcepub const DEFAULT_TIMEOUT: Duration
pub const DEFAULT_TIMEOUT: Duration
The request timeout of new.
Sourcepub const fn from_client(client: Client) -> Self
pub const fn from_client(client: Client) -> Self
A client over client as the application configured it, still HTTPS-only, refusing
non-public IP literals, and reading at most DEFAULT_MAX_BODY
bytes of body.
Host names are resolved as client resolves them, so refusing the non-public
addresses they resolve to is the application’s: with a DNS resolver like the one
new installs, and, when client uses a proxy, by the proxy itself,
which resolves the host names it is asked to reach.
Sourcepub const fn builder() -> ReqwestClientBuilder
Available on crate feature local-network only.
pub const fn builder() -> ReqwestClientBuilder
local-network only.A builder for a client that may also reach the local network, for development and tests; see Local development and tests.
Sourcepub const fn with_max_body(self, max_body: usize) -> Self
pub const fn with_max_body(self, max_body: usize) -> Self
This client reading at most max_body bytes of body.
Trait Implementations§
Source§impl Clone for ReqwestClient
impl Clone for ReqwestClient
Source§impl Debug for ReqwestClient
impl Debug for ReqwestClient
Source§impl HttpClient for ReqwestClient
impl HttpClient for ReqwestClient
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
url: &'life1 Url,
) -> Pin<Box<dyn Future<Output = Result<HttpResponse, HttpError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
url: &'life1 Url,
) -> Pin<Box<dyn Future<Output = Result<HttpResponse, HttpError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
url, returning the status and the body of the response. Read more