Expand description
The HTTP client every HTTP connector uses (RFC 0013, #880).
Client wraps a reqwest::Client and adds what each HTTP connector
used to write for itself, sometimes wrongly:
- Redirects that drop credentials. Redirects are followed here, not by
reqwest, so a hop to another origin (scheme, host or port) drops the
Authorization,CookieandProxy-Authorizationheaders, every header marked sensitive, and every header named withClientBuilder::sensitive_header(an API-key header). reqwest’s own policy only knows the standard headers. - Redirect targets are checked. A redirect into a link-local or cloud
instance-metadata address is always refused. Under a reference policy
(
ClientBuilder::reference_policy, or one scoped over a run withwith_reference_policy) the first request and every redirect hop must passReferencePolicy::check_url. - Bounded response reads.
Client::bytes/text/jsonrefuse a body abovemax_response_bytesinstead of buffering it whole. - One error shape. A non-2xx response becomes
FaucetError::HttpStatus(body truncated, URL without credentials), a429/503withRetry-AfterbecomesFaucetError::RateLimited, and a transport error keeps reqwest’s timeout / connect classification with the credentials removed from its URL. - Retries through
CallContext.Client::executeruns a request under a context’s timeout, deadline, retry policy, cancellation and metering.
use faucet_core::http::Client;
use faucet_core::io::CallContext;
use std::time::Duration;
let client = Client::builder()
.timeout(Duration::from_secs(30))
.sensitive_header("x-api-key")
.build()?;
let ctx = CallContext::new().with_retry(faucet_core::RetryPolicy::default());
let resp = client
.execute(&ctx, "page", || client.get("https://api.example.com/items"))
.await?;
let body: serde_json::Value = client.json(resp).await?;reqwest options this builder does not name (compression, mutual TLS, root
certificates) are set through ClientBuilder::configure.
Structs§
- Client
- An HTTP client for connectors. Cheap to clone; build one in
new()and reuse it. See the module docs. - Client
Builder - Builds a
Client. - Response
TooLarge - A response body was larger than the client’s
max_response_bytes. Carried inFaucetError::Custom; downcast to read it.
Constants§
- DEFAULT_
MAX_ REDIRECTS - The most redirects one request follows by default.
- DEFAULT_
MAX_ RESPONSE_ BYTES - The largest body
Client::bytesbuffers by default: 1 GiB.
Functions§
- current_
reference_ policy - The reference policy scoped over the current task, if any.
- inherit_
reference_ policy futunder the reference policy scoped over the current task, for a future about to be spawned onto another task.- is_
cross_ origin - Whether
bis a different origin froma: another scheme, host or port. - status_
error - The error for a non-2xx response:
FaucetError::RateLimitedfor a429(or a503withRetry-After) that states its wait, otherwiseFaucetError::HttpStatuswith the body truncated toDEFAULT_ERROR_BODY_MAX_LENbytes and the URL without credentials. Consumes the response. - strip_
credentials - Remove every credential-bearing header: the standard ones, every value
marked sensitive, and the
extranames. - transport_
error - A transport error with the credentials removed from its URL.
- with_
reference_ policy - Run
futwithpolicyas the reference policy everyClientwithout its own (ClientBuilder::reference_policy) checks its requests and redirect hops against. A host scopes the policy that admitted a config over that config’s run; a task spawned fromfutmust be wrapped again (current_reference_policy).