Files
tuliprox/src/api/model/model_utils.rs
T

46 lines
1.7 KiB
Rust
Raw Normal View History

2025-02-12 13:14:45 +01:00
use reqwest::{StatusCode};
use std::collections::{HashSet};
use std::str::FromStr;
2025-02-12 13:14:45 +01:00
use reqwest::header::HeaderMap;
2025-05-09 17:13:37 +02:00
use crate::utils::{filter_response_header};
2025-02-12 13:14:45 +01:00
pub fn get_response_headers(headers: &HeaderMap) -> Vec<(String, String)> {
let response_headers: Vec<(String, String)> = headers.iter()
2025-05-09 17:13:37 +02:00
.filter(|(key, _)| filter_response_header(key.as_str()))
.map(|(key, value)| (key.to_string(), value.to_str().unwrap().to_string())).collect();
response_headers
}
2025-03-20 11:57:32 +01:00
pub fn get_stream_response_with_headers(custom: Option<(Vec<(String, String)>, StatusCode)>) -> (axum::http::StatusCode, axum::http::HeaderMap) {
2025-03-11 14:56:10 +01:00
let mut headers = HeaderMap::new();
let mut added_headers: HashSet<String> = HashSet::new();
2025-03-11 14:56:10 +01:00
let mut status = StatusCode::OK;
if let Some((custom_headers, status_code)) = custom {
2025-03-11 14:56:10 +01:00
status = status_code;
for (key, value) in custom_headers {
if let (Ok(name), Ok(val)) = (axum::http::HeaderName::from_str(&key), axum::http::HeaderValue::from_str(&value)) {
headers.insert(name.clone(), val);
added_headers.insert(key);
}
}
}
let default_headers = vec![
2025-05-09 17:13:37 +02:00
("content-type", "application/octet-stream")
];
2025-03-11 14:56:10 +01:00
for (key, value) in default_headers {
if !added_headers.contains(key) {
if let (Ok(name), Ok(val)) = (axum::http::HeaderName::from_str(key), axum::http::HeaderValue::from_str(value)) {
headers.insert(name, val);
}
}
}
2025-03-11 14:56:10 +01:00
if let Ok(date_header) = axum::http::HeaderValue::from_str(&chrono::Utc::now().to_rfc2822()) {
headers.insert(axum::http::HeaderName::from_static("date"), date_header);
}
2025-03-11 14:56:10 +01:00
(status, headers)
}