Repository: servo/servo
File: components/net/http_loader.rs (Rust)

Code before:
    CONTENT_LANGUAGE,
    CONTENT_LOCATION,
    CONTENT_TYPE,
];

/// <https://fetch.spec.whatwg.org/#concept-response-location-url>
fn location_url_for_response(
    response: &mut Response,
    request_fragment: Option<&str>,
) -> Option<Result<ServoUrl, String>> {
    // Step 1. If response’s status is not a redirect status, then return null.
    assert!(
        response
            .actual_response()
            .status
            .try_code()
            .is_some_and(is_redirect_status)
    );
    // Step 2. Let location be the result of extracting header list values given `Location` and response’s header list.
    let mut location = response
        .actual_response()
        .headers
        .get(header::LOCATION)
        .and_then(|v| {
            HeaderValue::to_str(v)
                .map(|l| {
                    // Step 3. If location is a header value, then set location to the result of parsing location with response’s URL.
                    ServoUrl::parse_with_base(response.actual_response().url(), l)
                        .map_err(|err| err.to_string())
                })
                .ok()
        });

    // Step 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
    if let Some(Ok(ref mut location)) = location {
        if location.fragment().is_none() {
            location.set_fragment(request_fragment);
        }
    }
    // Step 5. Return location.
    response.actual_response_mut().location_url = location.clone();
    location
}

/// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch)
#[async_recursion]
pub async fn http_redirect_fetch(
    fetch_params: &mut FetchParams,
    cache: &mut CorsCache,
    mut response: Response,
    cors_flag: bool,

Review comment (suggestion):
```suggestion
                .map(|location_string| {
```

Code after:
static REQUEST_BODY_HEADER_NAMES: &[HeaderName] = &[
    CONTENT_ENCODING,
    CONTENT_LANGUAGE,
    CONTENT_LOCATION,
    CONTENT_TYPE,
];

/// <https://fetch.spec.whatwg.org/#concept-response-location-url>
fn location_url_for_response(
    response: &Response,
    request_fragment: Option<&str>,
) -> Option<Result<ServoUrl, String>> {
    // Step 1. If response’s status is not a redirect status, then return null.
    assert!(
        response
            .actual_response()
            .status
            .try_code()
            .is_some_and(is_redirect_status)
    );
    // Step 2. Let location be the result of extracting header list values given `Location` and response’s header list.
    let mut location = response
        .actual_response()
        .headers
        .get(header::LOCATION)
        .and_then(|header_value| {
            HeaderValue::to_str(header_value)
                .map(|location_string| {
                    // Step 3. If location is a header value, then set location to the result of parsing location with response’s URL.
                    ServoUrl::parse_with_base(response.actual_response().url(), location_string)
                        .map_err(|error| error.to_string())
                })
                .ok()
        });

    // Step 4. If location is a URL whose fragment is null, then set location’s fragment to requestFragment.
    if let Some(Ok(ref mut location)) = location {
        if location.fragment().is_none() {
            location.set_fragment(request_fragment);
        }
    }
    // Step 5. Return location.
    location
}

/// [HTTP redirect fetch](https://fetch.spec.whatwg.org#http-redirect-fetch)
#[async_recursion]
pub async fn http_redirect_fetch(
    fetch_params: &mut FetchParams,
    cache: &mut CorsCache,
    mut response: Response,