ordinary-utils 0.8.3

Utils for Ordinary
Documentation
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

use crate::get_http_version_str;
use axum::body::Body;
use axum::http::{HeaderValue, header};
use axum::response::IntoResponse;

pub fn get_response_for_forwarded(via_domain: &str, res: reqwest::Response) -> impl IntoResponse {
    let version = res.version();

    let out_res = axum::http::Response::from(res);
    let mut out_res = out_res.map(Body::new);

    let headers = out_res.headers_mut();
    let res_version = get_http_version_str(version);

    let via = if let Some(src_via) = headers.get(header::VIA)
        && let Ok(src_via) = src_via.to_str()
    {
        format!("{src_via}, {res_version} {via_domain} (ordinaryd)")
    } else {
        format!("{res_version} {via_domain} (ordinaryd)")
    };

    if let Ok(via) = HeaderValue::from_str(&via) {
        headers.insert(header::VIA, via);
    }

    out_res.into_response()
}