Repository: astral-sh/uv
File: crates/uv-client/src/cached_client.rs (Rust)

Code before:
    }

    async fn send_cached_handle_stale(
        &self,
        req: Request,
        cache_control: CacheControl<'_>,
        cached: DataWithCachePolicy,
        new_cache_policy_builder: CachePolicyBuilder,
    ) -> Result<CachedResponse, Error> {
        let url = DisplaySafeUrl::from(req.url().clone());
        debug!("Sending revalidation request for: {url}");
        let mut response = self
            .0
            .execute(req)
            .instrument(info_span!("revalidation_request", url = url.as_str()))
            .await
            .map_err(|err| ErrorKind::from_reqwest_middleware(url.clone(), err))?;

        // Check for HTTP error status and extract problem details if available
        if let Err(status_error) = response.error_for_status_ref() {
            // Clone the response to extract problem details before the error consumes it
            let problem_details = if response
                .headers()
                .get("content-type")
                .and_then(|ct| ct.to_str().ok())
                .map(|ct| ct.starts_with("application/problem+json"))
                .unwrap_or(false)
            {
                extract_problem_details(response).await
            } else {
                None
            };
            return Err(ErrorKind::from_reqwest_with_problem_details(
                url.clone(),
                status_error,
                problem_details,
            )
            .into());
        }

        // If the user set a custom `Cache-Control` header, override it.
        if let CacheControl::Override(header) = cache_control {
            response.headers_mut().insert(
                http::header::CACHE_CONTROL,
                http::HeaderValue::from_str(header)
                    .expect("Cache-Control header must be valid UTF-8"),
            );
        }

        match cached
            .cache_policy

Review comment (suggestion):
Should this be an equality check rather than `starts_with`? RFC 9457 says that problem detail responses are identified by exactly the `application/problem+json` content-type, so we probably shouldn't accept `application/problem+json+abc` or similar.

Code after:
        })
    }

    async fn send_cached_handle_stale(
        &self,
        req: Request,
        cache_control: CacheControl<'_>,
        cached: DataWithCachePolicy,
        new_cache_policy_builder: CachePolicyBuilder,
    ) -> Result<CachedResponse, Error> {
        let url = DisplaySafeUrl::from(req.url().clone());
        debug!("Sending revalidation request for: {url}");
        let mut response = self
            .0
            .execute(req)
            .instrument(info_span!("revalidation_request", url = url.as_str()))
            .await
            .map_err(|err| ErrorKind::from_reqwest_middleware(url.clone(), err))?;

        // Check for HTTP error status and extract problem details if available
        if let Err(status_error) = response.error_for_status_ref() {
            // Clone the response to extract problem details before the error consumes it
            let problem_details = if response
                .headers()
                .get("content-type")
                .and_then(|ct| ct.to_str().ok())
                .map(|ct| ct == "application/problem+json")
                .unwrap_or(false)
            {
                extract_problem_details(response).await
            } else {
                None
            };
            return Err(ErrorKind::from_reqwest_with_problem_details(
                url.clone(),
                status_error,
                problem_details,
            )
            .into());
        }

        // If the user set a custom `Cache-Control` header, override it.
        if let CacheControl::Override(header) = cache_control {
            response.headers_mut().insert(
                http::header::CACHE_CONTROL,
                http::HeaderValue::from_str(header)
                    .expect("Cache-Control header must be valid UTF-8"),
            );
        }

        match cached