use axum::http::{HeaderMap, StatusCode, header};
use bytes::Bytes;
use crate::error::ProxyError;
const FORWARDED_REQUEST_HEADERS: &[&str] = &[
"x-api-key",
"authorization",
"anthropic-version",
"anthropic-beta",
"content-type",
];
fn build_request(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> reqwest::RequestBuilder {
let url = format!("{}{path}", base.trim_end_matches('/'));
let mut req = client.post(url);
for name in FORWARDED_REQUEST_HEADERS {
if let Some(value) = headers.get(*name) {
req = req.header(*name, value.clone());
}
}
req.body(body)
}
const RELAYED_RESPONSE_HEADERS: &[&str] = &["retry-after", "request-id", "x-request-id"];
const RELAYED_RESPONSE_HEADER_PREFIXES: &[&str] = &["anthropic-ratelimit-", "x-ratelimit-"];
fn relayed(name: &str) -> bool {
RELAYED_RESPONSE_HEADERS.contains(&name)
|| RELAYED_RESPONSE_HEADER_PREFIXES
.iter()
.any(|p| name.starts_with(p))
}
fn passthrough_headers(response: &reqwest::Response) -> HeaderMap {
let mut out = HeaderMap::new();
if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
out.insert(header::CONTENT_TYPE, content_type.clone());
}
for (name, value) in response.headers() {
if relayed(name.as_str()) {
out.append(name.clone(), value.clone());
}
}
out
}
pub async fn forward_anthropic(
client: &reqwest::Client,
base: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
forward_anthropic_path(client, base, "/v1/messages", headers, body).await
}
pub async fn forward_anthropic_streaming(
client: &reqwest::Client,
base: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, reqwest::Response), ProxyError> {
let response = build_request(client, base, "/v1/messages", headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers(&response);
Ok((status, out_headers, response))
}
pub async fn forward_anthropic_path(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
let response = build_request(client, base, path, headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers(&response);
let body = response.bytes().await?;
Ok((status, out_headers, body))
}
const FORWARDED_GEMINI_REQUEST_HEADERS: &[&str] = &["x-goog-api-key", "content-type"];
const RELAYED_GEMINI_RESPONSE_HEADERS: &[&str] = &["retry-after", "x-request-id"];
const RELAYED_GEMINI_RESPONSE_HEADER_PREFIXES: &[&str] = &["x-ratelimit-", "x-goog-"];
fn relayed_gemini(name: &str) -> bool {
RELAYED_GEMINI_RESPONSE_HEADERS.contains(&name)
|| RELAYED_GEMINI_RESPONSE_HEADER_PREFIXES
.iter()
.any(|p| name.starts_with(p))
}
fn build_gemini_request(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> reqwest::RequestBuilder {
let url = format!("{}{path}", base.trim_end_matches('/'));
let mut req = client.post(url);
for name in FORWARDED_GEMINI_REQUEST_HEADERS {
if let Some(value) = headers.get(*name) {
req = req.header(*name, value.clone());
}
}
req.body(body)
}
fn passthrough_headers_gemini(response: &reqwest::Response) -> HeaderMap {
let mut out = HeaderMap::new();
if let Some(content_type) = response.headers().get(header::CONTENT_TYPE) {
out.insert(header::CONTENT_TYPE, content_type.clone());
}
for (name, value) in response.headers() {
if relayed_gemini(name.as_str()) {
out.append(name.clone(), value.clone());
}
}
out
}
pub async fn forward_gemini_path(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
let response = build_gemini_request(client, base, path, headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers_gemini(&response);
let body = response.bytes().await?;
Ok((status, out_headers, body))
}
pub async fn forward_gemini_streaming_path(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, reqwest::Response), ProxyError> {
let response = build_gemini_request(client, base, path, headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers_gemini(&response);
Ok((status, out_headers, response))
}
pub const OPENAI_CHAT_PATH: &str = "/v1/chat/completions";
pub const OPENAI_RESPONSES_PATH: &str = "/v1/responses";
pub async fn forward_openai_path(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, Bytes), ProxyError> {
let response = build_request(client, base, path, headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers(&response);
let body = response.bytes().await?;
Ok((status, out_headers, body))
}
pub async fn forward_openai_streaming_path(
client: &reqwest::Client,
base: &str,
path: &str,
headers: &HeaderMap,
body: Bytes,
) -> Result<(StatusCode, HeaderMap, reqwest::Response), ProxyError> {
let response = build_request(client, base, path, headers, body)
.send()
.await?;
let status = response.status();
let out_headers = passthrough_headers(&response);
Ok((status, out_headers, response))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn passthrough_relays_backoff_and_correlation_headers_only() {
let upstream = axum::http::Response::builder()
.status(429)
.header("content-type", "application/json")
.header("retry-after", "7")
.header("request-id", "req_123")
.header("anthropic-ratelimit-requests-remaining", "0")
.header("x-ratelimit-reset-tokens", "6s")
.header("content-length", "2")
.header("set-cookie", "a=b")
.body("{}")
.unwrap_or_default();
let out = passthrough_headers(&reqwest::Response::from(upstream));
for (k, v) in [
("content-type", "application/json"),
("retry-after", "7"),
("request-id", "req_123"),
("anthropic-ratelimit-requests-remaining", "0"),
("x-ratelimit-reset-tokens", "6s"),
] {
assert_eq!(out.get(k).and_then(|h| h.to_str().ok()), Some(v), "{k}");
}
assert!(
out.get("content-length").is_none(),
"framing is not relayed"
);
assert!(
out.get("set-cookie").is_none(),
"unlisted headers are not relayed"
);
}
}