use axum::response::Response;
pub fn ascii_ify(s: &str) -> String {
let mut result = String::with_capacity(s.len());
for c in s.chars() {
let code = c as u32;
if (0x20..=0x7E).contains(&code) {
result.push(c);
} else {
result.push_str(&format!("\\u{:04x}", code));
}
}
result
}
pub fn free_cors_headers() -> Vec<(&'static str, &'static str)> {
vec![
("access-control-allow-origin", "*"),
("access-control-allow-methods", "*"),
("access-control-allow-headers", "*"),
("access-control-expose-headers", "*"),
]
}
pub fn apply_free_cors(response: &mut Response) {
let headers = response.headers_mut();
for (name, value) in free_cors_headers() {
headers.insert(
axum::http::header::HeaderName::from_static(name),
axum::http::header::HeaderValue::from_static(value),
);
}
}
pub const FIREFOX_EXTRA_NEWLINES: usize = 240;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ascii_ify_simple() {
assert_eq!(ascii_ify("hello"), "hello");
assert_eq!(ascii_ify("test 123"), "test 123");
}
#[test]
fn test_ascii_ify_unicode() {
assert_eq!(ascii_ify("héllo"), "h\\u00e9llo");
assert_eq!(ascii_ify("日本語"), "\\u65e5\\u672c\\u8a9e");
}
#[test]
fn test_ascii_ify_control_chars() {
assert_eq!(ascii_ify("hello\nworld"), "hello\\u000aworld");
assert_eq!(ascii_ify("tab\there"), "tab\\u0009here");
}
#[test]
fn test_free_cors_headers() {
let headers = free_cors_headers();
assert_eq!(headers.len(), 4);
assert!(headers
.iter()
.any(|(k, _)| *k == "access-control-allow-origin"));
}
}