braid-core 0.1.4

Unified Braid Protocol implementation in Rust, including Braid-HTTP, Antimatter CRDT, and BraidFS.
Documentation
//! Server utilities for Braid protocol.

use axum::response::Response;

/// Escape non-ASCII characters in headers for structured header format.
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
}

/// Set permissive CORS headers on a response.
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", "*"),
    ]
}

/// Apply permissive CORS headers to a response builder.
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),
        );
    }
}

/// Number of extra newlines to add for Firefox workaround.
pub const FIREFOX_EXTRA_NEWLINES: usize = 240;