Skip to main content

ordinary_utils/
response.rs

1// Copyright (C) 2026 Ordinary Labs, LLC.
2//
3// SPDX-License-Identifier: AGPL-3.0-only
4
5use crate::get_http_version_str;
6use axum::body::Body;
7use axum::http::{HeaderValue, header};
8use axum::response::IntoResponse;
9
10#[derive(Clone)]
11pub struct IsProxy(pub bool);
12
13pub fn get_response_for_forwarded(via_domain: &str, res: reqwest::Response) -> impl IntoResponse {
14    let version = res.version();
15
16    let out_res = axum::http::Response::from(res);
17    let mut out_res = out_res.map(Body::new);
18
19    out_res.extensions_mut().insert(IsProxy(true));
20
21    let headers = out_res.headers_mut();
22    let res_version = get_http_version_str(version);
23
24    let via = if let Some(src_via) = headers.get(header::VIA)
25        && let Ok(src_via) = src_via.to_str()
26    {
27        format!("{src_via}, {res_version} {via_domain} (ordinaryd)")
28    } else {
29        format!("{res_version} {via_domain} (ordinaryd)")
30    };
31
32    if let Ok(via) = HeaderValue::from_str(&via) {
33        headers.insert(header::VIA, via);
34    }
35
36    out_res.into_response()
37}