Files
crank/crates/crank-adapter-rest/src/client.rs
T
bsodfather 54ce770682
CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.

Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
  Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
  3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
  revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
  version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
  dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
  resources/list admission fix for bound template publications.

Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
  `just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
  a real MCP Inspector 2.12.0 client smoke.

Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
  runs only on main), story events run the changed packages, the full workspace suite
  stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
  release entry of the tag instead of dying with the runner workspace.

Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.

Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
2026-10-25 09:08:22 +00:00

991 lines
33 KiB
Rust

use std::{
collections::BTreeMap,
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
sync::Arc,
time::Duration,
};
use crank_core::{HttpMethod, RestTarget, RuntimeRequestContext};
use crank_metrics::{UpstreamOperationKind, UpstreamOutcome, UpstreamRequestMetrics};
use crank_trace::{ErrorCategory, Stage, StageOutcome};
use futures_util::StreamExt;
use opentelemetry::{
Context, global,
propagation::Injector,
trace::{SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState},
};
use reqwest::{
Client,
dns::{Addrs, Name, Resolve, Resolving},
header::{HeaderMap, HeaderName, HeaderValue},
redirect,
};
use tracing::{Instrument, Span};
use tracing_opentelemetry::OpenTelemetrySpanExt;
mod path_params;
use path_params::substitute_path_params;
use crate::{
ExternalReferenceFetchError, RestAdapterError, RestRequest, RestResponse,
body::{apply_body, apply_legacy_json_body, selected_request_content_type},
response::{decode_body, normalize_headers, validate_response_contract},
};
#[derive(Clone, Debug)]
pub struct RestAdapter {
client: Result<Client, Arc<str>>,
policy: OutboundHttpPolicy,
}
/// A deliberately separate, GET-only boundary for materializing external
/// OpenAPI documents. It has no execution metrics, tracing propagation, or
/// request construction semantics from [`RestAdapter`].
#[derive(Clone, Debug)]
pub struct ExternalReferenceFetcher {
client: Result<Client, Arc<str>>,
policy: OutboundHttpPolicy,
allowed_url_prefixes: Vec<String>,
max_response_bytes: usize,
timeout: Duration,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OutboundHttpPolicy {
allowed_hosts: Vec<String>,
denied_hosts: Vec<String>,
max_request_bytes: usize,
max_response_bytes: usize,
}
const DEFAULT_MAX_RESPONSE_BYTES: usize = 4 * 1024 * 1024;
const DEFAULT_MAX_REQUEST_BYTES: usize = 4 * 1024 * 1024;
impl Default for RestAdapter {
fn default() -> Self {
Self::new()
}
}
impl RestAdapter {
pub fn new() -> Self {
Self::with_policy(OutboundHttpPolicy::default())
}
pub fn with_policy(policy: OutboundHttpPolicy) -> Self {
let client = outbound_client(&policy);
Self { client, policy }
}
pub async fn execute(
&self,
target: &RestTarget,
request: &RestRequest,
) -> Result<RestResponse, RestAdapterError> {
let request_metrics = UpstreamRequestMetrics::start(UpstreamOperationKind::Rest);
let result = self.execute_inner(target, request, None).await;
let outcome = match &result {
Ok(_) => UpstreamOutcome::Success,
Err(error) => upstream_outcome(error),
};
request_metrics.complete(outcome);
result
}
pub(crate) async fn execute_with_context(
&self,
target: &RestTarget,
request: &RestRequest,
context: &RuntimeRequestContext,
) -> Result<RestResponse, RestAdapterError> {
let request_metrics = UpstreamRequestMetrics::start_with_exemplar(
UpstreamOperationKind::Rest,
context
.trace_context
.is_sampled()
.then(|| {
crank_metrics::ExemplarTraceId::parse(
&context.trace_context.trace_id().to_string(),
)
})
.flatten(),
);
let result = self.execute_inner(target, request, Some(context)).await;
let outcome = match &result {
Ok(_) => UpstreamOutcome::Success,
Err(error) => upstream_outcome(error),
};
request_metrics.complete(outcome);
result
}
async fn execute_inner(
&self,
target: &RestTarget,
request: &RestRequest,
trusted_context: Option<&RuntimeRequestContext>,
) -> Result<RestResponse, RestAdapterError> {
let upstream_span = Stage::UpstreamHttp.span();
if let Some(context) = trusted_context {
set_span_parent_from_traceparent(&upstream_span, context.trace_context.traceparent());
}
let result = async {
let url = build_url(target, request)?;
self.policy.validate_url(&url)?;
let mut header_request = request.clone();
let cookie_header = extract_mapped_cookies(&mut header_request.headers)?;
let mut headers = build_headers(target, &header_request)?;
if let Some(cookie_header) = cookie_header {
if headers.contains_key("cookie") {
return Err(RestAdapterError::InvalidBodyContract {
reason: "mapped cookie conflicts with an existing cookie header".to_owned(),
});
}
let value = HeaderValue::try_from(cookie_header).map_err(|_| {
RestAdapterError::InvalidBodyContract {
reason: "cookie parameter is invalid".to_owned(),
}
})?;
headers.insert("cookie", value);
}
if let Some(context) = trusted_context {
for (name, value) in context.outbound_headers() {
let (Ok(name), Ok(value)) =
(HeaderName::try_from(name), HeaderValue::try_from(value))
else {
continue;
};
headers.insert(name, value);
}
}
apply_current_trace_context(&mut headers);
if !headers.contains_key("traceparent")
&& let Some(context) = trusted_context
&& let Ok(value) = HeaderValue::from_str(context.trace_context.traceparent())
{
headers.insert("traceparent", value);
}
let client =
self.client
.as_ref()
.map_err(|details| RestAdapterError::InvalidConfiguration {
details: details.to_string(),
})?;
if request.body.is_none()
&& (request.body_content_type.is_some()
|| target
.request_contract
.as_ref()
.is_some_and(|contract| contract.body_required))
{
return Err(RestAdapterError::InvalidBodyContract {
reason: "request body contract requires a body".to_owned(),
});
}
let typed_content_type = if request.body.is_some() && target.request_contract.is_some()
{
selected_request_content_type(target, request)?
} else {
if request.body.is_some() && request.body_content_type.is_some() {
selected_request_content_type(target, request)?;
}
None
};
if typed_content_type.is_some() {
headers.remove("content-type");
}
let legacy_has_content_type = headers.contains_key("content-type");
let mut builder = client
.request(to_reqwest_method(target.method), url)
.headers(headers)
.timeout(Duration::from_millis(request.timeout_ms));
if let Some(body) = &request.body {
if let Some(content_type) = typed_content_type {
builder = apply_body(
builder,
body,
&content_type,
target.multipart_field_mime_types.get(&content_type),
self.policy.max_request_bytes,
)
.await?;
} else {
builder = apply_legacy_json_body(
builder,
body,
legacy_has_content_type,
self.policy.max_request_bytes,
)?;
}
}
if let Some(context) = trusted_context {
context.mark_dispatch_started();
}
let response = builder.send().await?;
let status = response.status();
let headers = normalize_headers(response.headers());
if status.is_redirection() {
return Err(RestAdapterError::RedirectNotAllowed);
}
let (body, has_body, valid_json) =
decode_body(response, self.policy.max_response_bytes).await?;
if !status.is_success() {
return Err(RestAdapterError::UnexpectedStatus {
status: status.as_u16(),
body,
});
}
validate_response_contract(target, status.as_u16(), &headers, has_body, valid_json)?;
Ok(RestResponse {
status_code: status.as_u16(),
headers,
body,
})
}
.instrument(upstream_span.clone())
.await;
match &result {
Ok(_) => StageOutcome::Success.record(&upstream_span),
Err(_) => {
StageOutcome::Error.record(&upstream_span);
ErrorCategory::Upstream.record(&upstream_span);
}
}
result
}
}
fn extract_mapped_cookies(
headers: &mut BTreeMap<String, String>,
) -> Result<Option<String>, RestAdapterError> {
let internal = headers
.keys()
.filter(|name| name.starts_with("x-crank-internal-cookie-"))
.cloned()
.collect::<Vec<_>>();
if internal.is_empty() {
return Ok(None);
}
let mut cookies = Vec::with_capacity(internal.len());
for key in internal {
let value = headers.remove(&key).unwrap_or_default();
let (name, value) =
value
.split_once('\t')
.ok_or_else(|| RestAdapterError::InvalidBodyContract {
reason: "cookie parameter is malformed".to_owned(),
})?;
if name.is_empty()
|| name
.chars()
.any(|character| character.is_control() || matches!(character, ';' | '='))
|| value
.chars()
.any(|character| character.is_control() || character == ';')
{
return Err(RestAdapterError::InvalidBodyContract {
reason: "cookie parameter is invalid".to_owned(),
});
}
cookies.push(format!("{name}={value}"));
}
Ok(Some(cookies.join("; ")))
}
impl ExternalReferenceFetcher {
pub fn try_new(
policy: OutboundHttpPolicy,
allowed_url_prefixes: Vec<String>,
max_response_bytes: usize,
timeout: Duration,
) -> Result<Self, ExternalReferenceFetchError> {
if max_response_bytes == 0 || timeout.is_zero() {
return Err(ExternalReferenceFetchError::InvalidConfiguration);
}
let allowed_url_prefixes = allowed_url_prefixes
.into_iter()
.map(|prefix| canonical_external_reference_prefix(&prefix))
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
client: outbound_client(&policy),
policy,
allowed_url_prefixes,
max_response_bytes,
timeout,
})
}
/// Fetches one document with a bounded, headerless `GET`.
///
/// URL fragments are stripped because they address JSON Pointer targets in
/// the fetched document rather than a network resource.
pub async fn get(&self, url: &str) -> Result<Vec<u8>, ExternalReferenceFetchError> {
if self.allowed_url_prefixes.is_empty() {
return Err(ExternalReferenceFetchError::Disabled);
}
let mut url =
reqwest::Url::parse(url).map_err(|_| ExternalReferenceFetchError::InvalidUrl)?;
url.set_fragment(None);
if !self
.allowed_url_prefixes
.iter()
.any(|prefix| matches_external_reference_prefix(&url, prefix))
{
return Err(ExternalReferenceFetchError::TargetNotAllowed);
}
self.policy
.validate_url(&url)
.map_err(external_policy_error)?;
let client = self
.client
.as_ref()
.map_err(|_| ExternalReferenceFetchError::InvalidConfiguration)?;
let response = client
.get(url)
.timeout(self.timeout)
.send()
.await
.map_err(external_transport_error)?;
let status = response.status();
if status.is_redirection() {
return Err(ExternalReferenceFetchError::RedirectNotAllowed);
}
if !status.is_success() {
return Err(ExternalReferenceFetchError::UnexpectedStatus {
status: status.as_u16(),
});
}
read_external_response_bytes(response, self.max_response_bytes).await
}
}
fn outbound_client(policy: &OutboundHttpPolicy) -> Result<Client, Arc<str>> {
let resolver = Arc::new(PolicyDnsResolver {
policy: policy.clone(),
#[cfg(test)]
lookup: None,
});
Client::builder()
.redirect(redirect::Policy::none())
.no_proxy()
.dns_resolver(resolver)
.build()
.map_err(|error| Arc::<str>::from(error.to_string()))
}
fn canonical_external_reference_prefix(
prefix: &str,
) -> Result<String, ExternalReferenceFetchError> {
let url = reqwest::Url::parse(prefix)
.map_err(|_| ExternalReferenceFetchError::InvalidConfiguration)?;
if !matches!(url.scheme(), "http" | "https")
|| url.host_str().is_none()
|| !url.username().is_empty()
|| url.password().is_some()
|| url.query().is_some()
|| url.fragment().is_some()
{
return Err(ExternalReferenceFetchError::InvalidConfiguration);
}
Ok(url.to_string())
}
fn matches_external_reference_prefix(url: &reqwest::Url, prefix: &str) -> bool {
let url = url.as_str();
if !url.starts_with(prefix) {
return false;
}
let Some(next) = url.as_bytes().get(prefix.len()) else {
return true;
};
prefix.ends_with('/') || matches!(next, b'/' | b'?')
}
fn external_policy_error(error: RestAdapterError) -> ExternalReferenceFetchError {
match error {
RestAdapterError::TargetNotAllowed { .. } => ExternalReferenceFetchError::TargetNotAllowed,
_ => ExternalReferenceFetchError::InvalidConfiguration,
}
}
fn external_transport_error(error: reqwest::Error) -> ExternalReferenceFetchError {
ExternalReferenceFetchError::Transport {
timeout: error.is_timeout(),
connect: error.is_connect(),
}
}
async fn read_external_response_bytes(
response: reqwest::Response,
max_response_bytes: usize,
) -> Result<Vec<u8>, ExternalReferenceFetchError> {
if response
.content_length()
.is_some_and(|length| length > max_response_bytes as u64)
{
return Err(ExternalReferenceFetchError::ResponseTooLarge {
limit_bytes: max_response_bytes,
});
}
let mut stream = response.bytes_stream();
let mut bytes = Vec::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(external_transport_error)?;
if bytes.len().saturating_add(chunk.len()) > max_response_bytes {
return Err(ExternalReferenceFetchError::ResponseTooLarge {
limit_bytes: max_response_bytes,
});
}
bytes.extend_from_slice(&chunk);
}
Ok(bytes)
}
fn upstream_outcome(error: &RestAdapterError) -> UpstreamOutcome {
match error {
RestAdapterError::UnexpectedStatus { status, .. } if (400..500).contains(status) => {
UpstreamOutcome::ClientError
}
RestAdapterError::UnexpectedStatus { status, .. } if (500..600).contains(status) => {
UpstreamOutcome::ServerError
}
RestAdapterError::UnexpectedStatus { .. } => UpstreamOutcome::UnexpectedStatus,
RestAdapterError::Transport { timeout: true, .. } => UpstreamOutcome::Timeout,
RestAdapterError::Transport { .. } => UpstreamOutcome::TransportError,
RestAdapterError::ResponseTooLarge { .. } => UpstreamOutcome::ResponseTooLarge,
RestAdapterError::RequestTooLarge { .. } => UpstreamOutcome::InvalidRequest,
RestAdapterError::TargetNotAllowed { .. } | RestAdapterError::RedirectNotAllowed => {
UpstreamOutcome::Rejected
}
RestAdapterError::WindowExpired => UpstreamOutcome::WindowExpired,
RestAdapterError::InvalidSseEvent | RestAdapterError::InvalidResponseContract { .. } => {
UpstreamOutcome::InvalidResponse
}
RestAdapterError::InvalidBaseUrl { .. }
| RestAdapterError::InvalidPathParameter { .. }
| RestAdapterError::InvalidQueryParameter { .. }
| RestAdapterError::InvalidHeaderName { .. }
| RestAdapterError::InvalidHeaderValue { .. }
| RestAdapterError::InvalidBodyContract { .. } => UpstreamOutcome::InvalidRequest,
RestAdapterError::InvalidConfiguration { .. } => UpstreamOutcome::Configuration,
}
}
impl Default for OutboundHttpPolicy {
fn default() -> Self {
Self {
allowed_hosts: Vec::new(),
denied_hosts: Vec::new(),
max_request_bytes: DEFAULT_MAX_REQUEST_BYTES,
max_response_bytes: DEFAULT_MAX_RESPONSE_BYTES,
}
}
}
impl OutboundHttpPolicy {
pub fn try_new(
allowed_hosts: Vec<String>,
denied_hosts: Vec<String>,
max_response_bytes: usize,
) -> Result<Self, RestAdapterError> {
Self::try_new_with_limits(
allowed_hosts,
denied_hosts,
DEFAULT_MAX_REQUEST_BYTES,
max_response_bytes,
)
}
pub fn try_new_with_limits(
allowed_hosts: Vec<String>,
denied_hosts: Vec<String>,
max_request_bytes: usize,
max_response_bytes: usize,
) -> Result<Self, RestAdapterError> {
if max_request_bytes == 0 {
return Err(RestAdapterError::InvalidConfiguration {
details: "outbound request limit must be greater than zero".to_owned(),
});
}
if max_response_bytes == 0 {
return Err(RestAdapterError::InvalidConfiguration {
details: "outbound response limit must be greater than zero".to_owned(),
});
}
Ok(Self {
allowed_hosts: validate_host_patterns(allowed_hosts)?,
denied_hosts: validate_host_patterns(denied_hosts)?,
max_request_bytes,
max_response_bytes,
})
}
pub fn allowing_hosts(hosts: impl IntoIterator<Item = impl Into<String>>) -> Self {
Self {
allowed_hosts: hosts.into_iter().map(Into::into).collect(),
..Self::default()
}
}
pub fn with_max_response_bytes(mut self, max_response_bytes: usize) -> Self {
self.max_response_bytes = max_response_bytes;
self
}
pub fn with_max_request_bytes(mut self, max_request_bytes: usize) -> Self {
self.max_request_bytes = max_request_bytes;
self
}
pub fn validate_base_url(&self, base_url: &str) -> Result<(), RestAdapterError> {
let url = reqwest::Url::parse(base_url).map_err(|_| RestAdapterError::InvalidBaseUrl {
url: "url".to_owned(),
})?;
self.validate_url(&url)
}
fn validate_url(&self, url: &reqwest::Url) -> Result<(), RestAdapterError> {
if !matches!(url.scheme(), "http" | "https")
|| !url.username().is_empty()
|| url.password().is_some()
{
return Err(RestAdapterError::TargetNotAllowed {
target: "url".to_owned(),
});
}
let host = url
.host_str()
.ok_or_else(|| RestAdapterError::TargetNotAllowed {
target: "host".to_owned(),
})?;
self.validate_host(host)?;
if !self.is_explicitly_allowed(host) && is_local_hostname(host) {
return Err(RestAdapterError::TargetNotAllowed {
target: "host".to_owned(),
});
}
if let Ok(address) = host.parse::<IpAddr>()
&& !self.is_explicitly_allowed(host)
&& !is_public_ip(address)
{
return Err(RestAdapterError::TargetNotAllowed {
target: "ip".to_owned(),
});
}
Ok(())
}
fn validate_host(&self, host: &str) -> Result<(), RestAdapterError> {
let host = normalize_host(host);
if self.is_denied_host(&host) {
return Err(RestAdapterError::TargetNotAllowed {
target: "host".to_owned(),
});
}
Ok(())
}
fn is_denied_host(&self, host: &str) -> bool {
let host = normalize_host(host);
self.denied_hosts
.iter()
.any(|pattern| host_matches(pattern, &host))
}
pub(crate) fn is_denied_ip(&self, address: IpAddr) -> bool {
ip_match_hosts(address).iter().any(|host| {
self.denied_hosts
.iter()
.any(|pattern| host_matches(pattern, host))
})
}
pub(crate) fn is_explicitly_allowed(&self, host: &str) -> bool {
let host = normalize_host(host);
self.allowed_hosts
.iter()
.any(|pattern| host_matches(pattern, &host))
}
}
#[derive(Clone)]
struct PolicyDnsResolver {
policy: OutboundHttpPolicy,
// Test-local DNS answers exercise the actual policy resolver and connector.
// Neither this field nor an injection API exists in production builds.
#[cfg(test)]
lookup: Option<Arc<dyn Resolve>>,
}
impl PolicyDnsResolver {
fn lookup(&self, name: Name) -> Resolving {
#[cfg(test)]
if let Some(lookup) = &self.lookup {
return lookup.resolve(name);
}
let host = normalize_host(name.as_str());
Box::pin(async move {
let addresses = tokio::net::lookup_host((host.as_str(), 0))
.await
.map_err(|error| Box::new(error) as Box<dyn std::error::Error + Send + Sync>)?;
let addresses: Vec<_> = addresses.collect();
Ok(Box::new(addresses.into_iter()) as Addrs)
})
}
}
impl Resolve for PolicyDnsResolver {
fn resolve(&self, name: Name) -> Resolving {
let host = normalize_host(name.as_str());
let policy = self.policy.clone();
let resolver = self.clone();
Box::pin(async move {
policy
.validate_host(&host)
.map_err(|error| boxed_io_error(error.to_string()))?;
let explicitly_allowed = policy.is_explicitly_allowed(&host);
let resolved = resolver.lookup(name).await?;
let addresses = resolved
.filter(|address| {
!policy.is_denied_ip(address.ip())
&& (explicitly_allowed || is_public_ip(address.ip()))
})
.collect::<Vec<SocketAddr>>();
if addresses.is_empty() {
return Err(boxed_io_error(
"outbound target did not resolve to an allowed address".to_owned(),
));
}
Ok(Box::new(addresses.into_iter()) as Addrs)
})
}
}
fn boxed_io_error(message: String) -> Box<dyn std::error::Error + Send + Sync> {
Box::new(io::Error::new(io::ErrorKind::PermissionDenied, message))
}
fn validate_host_patterns(
values: impl IntoIterator<Item = String>,
) -> Result<Vec<String>, RestAdapterError> {
values
.into_iter()
.map(|value| {
let wildcard = value.starts_with("*.");
let normalized = normalize_host(value.trim_start_matches("*."));
let valid_ip = !wildcard && normalized.parse::<IpAddr>().is_ok();
if normalized.is_empty()
|| normalized.contains('/')
|| (!valid_ip && normalized.contains(':'))
|| (wildcard && normalized.parse::<IpAddr>().is_ok())
{
return Err(RestAdapterError::InvalidConfiguration {
details: "outbound host pattern is invalid".to_owned(),
});
}
Ok(if wildcard {
format!("*.{normalized}")
} else {
normalized
})
})
.collect()
}
fn normalize_host(host: &str) -> String {
host.trim()
.trim_start_matches('[')
.trim_end_matches(']')
.trim_end_matches('.')
.to_ascii_lowercase()
}
fn is_local_hostname(host: &str) -> bool {
let host = normalize_host(host);
host == "localhost" || host.ends_with(".localhost")
}
fn host_matches(pattern: &str, host: &str) -> bool {
pattern.strip_prefix("*.").map_or_else(
|| pattern == host,
|suffix| host != suffix && host.ends_with(&format!(".{suffix}")),
)
}
fn is_public_ip(address: IpAddr) -> bool {
match address {
IpAddr::V4(address) => is_public_ipv4(address),
IpAddr::V6(address) => is_public_ipv6(address),
}
}
pub(crate) fn ip_match_hosts(address: IpAddr) -> Vec<String> {
match address {
IpAddr::V4(address) => vec![address.to_string()],
IpAddr::V6(address) => {
let mut hosts = vec![address.to_string()];
if let Some(mapped) = address.to_ipv4_mapped() {
hosts.push(mapped.to_string());
} else {
let segments = address.segments();
if segments[..6].iter().all(|segment| *segment == 0) {
let [a, b] = segments[6].to_be_bytes();
let [c, d] = segments[7].to_be_bytes();
hosts.push(Ipv4Addr::new(a, b, c, d).to_string());
}
}
hosts
}
}
}
fn is_public_ipv4(address: Ipv4Addr) -> bool {
let octets = address.octets();
!(address.is_private()
|| address.is_loopback()
|| address.is_link_local()
|| address.is_broadcast()
|| address.is_documentation()
|| address.is_unspecified()
|| address.is_multicast()
|| octets[0] == 0
|| (octets[0] == 100 && (64..=127).contains(&octets[1]))
|| (octets[0] == 192 && octets[1] == 0 && octets[2] == 0)
|| (octets[0] == 198 && (18..=19).contains(&octets[1]))
|| octets[0] >= 240)
}
fn is_public_ipv6(address: Ipv6Addr) -> bool {
let segments = address.segments();
if let Some(address) = address.to_ipv4_mapped() {
return is_public_ipv4(address);
}
if segments[..6].iter().all(|segment| *segment == 0) {
let [a, b] = segments[6].to_be_bytes();
let [c, d] = segments[7].to_be_bytes();
return is_public_ipv4(Ipv4Addr::new(a, b, c, d));
}
!(address.is_unspecified()
|| address.is_loopback()
|| address.is_multicast()
|| (segments[0] & 0xfe00) == 0xfc00
|| (segments[0] & 0xffc0) == 0xfe80
|| (segments[0] & 0xffc0) == 0xfec0
|| (segments[0] == 0x0064
&& segments[1] == 0xff9b
&& segments[2..6].iter().all(|segment| *segment == 0))
|| (segments[0] == 0x0064 && segments[1] == 0xff9b && segments[2] == 1)
|| segments[0] == 0x2002
|| (segments[0] == 0x2001 && matches!(segments[1], 0 | 0x0db8)))
}
fn build_url(target: &RestTarget, request: &RestRequest) -> Result<reqwest::Url, RestAdapterError> {
let base_url =
reqwest::Url::parse(&target.base_url).map_err(|_| RestAdapterError::InvalidBaseUrl {
url: "url".to_owned(),
})?;
let path = substitute_path_params(&target.path_template, &request.path_params)?;
let mut url = if target.request_contract.is_some() || !target.response_contract.is_empty() {
let base_path = base_url.path().trim_end_matches('/');
let target_path = path.trim_start_matches('/');
let combined_path = if target_path.is_empty() {
base_path.to_owned()
} else if base_path.is_empty() {
format!("/{target_path}")
} else {
format!("{base_path}/{target_path}")
};
let mut url = base_url;
url.set_path(&combined_path);
url
} else {
base_url.join(path.trim_start_matches('/')).map_err(|_| {
RestAdapterError::InvalidBaseUrl {
url: "url".to_owned(),
}
})?
};
{
let mut query = url.query_pairs_mut();
for (key, value) in &request.query_params {
query.append_pair(key, value);
}
}
Ok(url)
}
fn build_headers(
target: &RestTarget,
request: &RestRequest,
) -> Result<HeaderMap, RestAdapterError> {
let mut headers = HeaderMap::new();
for (name, value) in &target.static_headers {
insert_header(&mut headers, name, value, HeaderSource::StaticTarget)?;
}
for (name, value) in &request.headers {
let trusted = request
.trusted_header_names
.iter()
.any(|trusted| trusted.eq_ignore_ascii_case(name));
insert_header(
&mut headers,
name,
value,
HeaderSource::PreparedRequest { trusted },
)?;
}
Ok(headers)
}
#[derive(Clone, Copy)]
enum HeaderSource {
StaticTarget,
PreparedRequest { trusted: bool },
}
fn insert_header(
headers: &mut HeaderMap,
name: &str,
value: &str,
source: HeaderSource,
) -> Result<(), RestAdapterError> {
let header_name =
HeaderName::try_from(name).map_err(|_| RestAdapterError::InvalidHeaderName {
header: name.to_owned(),
})?;
if is_ignored_reserved_header(&header_name) {
return Ok(());
}
if is_forbidden_reserved_header(&header_name, source) {
return Err(RestAdapterError::InvalidHeaderName {
header: header_name.as_str().to_owned(),
});
}
let header_value =
HeaderValue::try_from(value).map_err(|_| RestAdapterError::InvalidHeaderValue {
header: name.to_owned(),
})?;
headers.insert(header_name, header_value);
Ok(())
}
fn is_ignored_reserved_header(name: &HeaderName) -> bool {
matches!(
name.as_str(),
"traceparent"
| "tracestate"
| "baggage"
| "x-request-id"
| "x-trace-id"
| "x-correlation-id"
)
}
fn is_forbidden_reserved_header(name: &HeaderName, source: HeaderSource) -> bool {
let framing = matches!(
name.as_str(),
"host" | "content-length" | "transfer-encoding" | "connection" | "upgrade"
);
let credential_or_safety = matches!(
name.as_str(),
"authorization" | "cookie" | "idempotency-key"
);
let untrusted_credential_or_safety = match source {
HeaderSource::StaticTarget => credential_or_safety,
HeaderSource::PreparedRequest { trusted } => credential_or_safety && !trusted,
};
framing || untrusted_credential_or_safety
}
fn set_span_parent_from_traceparent(span: &Span, traceparent: &str) -> bool {
let mut parts = traceparent.split('-');
let (Some("00"), Some(trace_id), Some(parent_id), Some(flags), None) = (
parts.next(),
parts.next(),
parts.next(),
parts.next(),
parts.next(),
) else {
return false;
};
let (Ok(trace_id), Ok(parent_id)) = (TraceId::from_hex(trace_id), SpanId::from_hex(parent_id))
else {
return false;
};
let trace_flags = if flags == "01" {
TraceFlags::SAMPLED
} else if flags == "00" {
TraceFlags::default()
} else {
return false;
};
let parent = SpanContext::new(
trace_id,
parent_id,
trace_flags,
true,
TraceState::default(),
);
span.set_parent(Context::new().with_remote_span_context(parent))
.is_ok()
}
fn apply_current_trace_context(headers: &mut HeaderMap) {
for header in ["tracestate", "baggage"] {
headers.remove(header);
}
let context = Span::current().context();
if !context.span().span_context().is_valid() {
return;
}
headers.remove("traceparent");
global::get_text_map_propagator(|propagator| {
propagator.inject_context(&context, &mut ReqwestHeaderInjector(headers));
});
}
struct ReqwestHeaderInjector<'a>(&'a mut HeaderMap);
impl Injector for ReqwestHeaderInjector<'_> {
fn set(&mut self, key: &str, value: String) {
let Ok(name) = HeaderName::try_from(key) else {
return;
};
let Ok(value) = HeaderValue::try_from(value) else {
return;
};
self.0.insert(name, value);
}
}
fn to_reqwest_method(method: HttpMethod) -> reqwest::Method {
match method {
HttpMethod::Get => reqwest::Method::GET,
HttpMethod::Post => reqwest::Method::POST,
HttpMethod::Put => reqwest::Method::PUT,
HttpMethod::Patch => reqwest::Method::PATCH,
HttpMethod::Delete => reqwest::Method::DELETE,
HttpMethod::Head => reqwest::Method::HEAD,
HttpMethod::Options => reqwest::Method::OPTIONS,
}
}
#[cfg(test)]
#[path = "client_dns_tests.rs"]
mod dns_tests;