Skip to main content

anvil/server/beacon/
utils.rs

1use axum::http::header;
2use hyper::HeaderMap;
3
4/// Helper function to determine if the Accept header indicates a preference for SSZ (octet-stream)
5/// over JSON.
6pub fn must_be_ssz(headers: &HeaderMap) -> bool {
7    headers.get(header::ACCEPT).and_then(|v| v.to_str().ok()).is_some_and(|accept_str| {
8        let mut octet_stream_q = 0.0;
9        let mut json_q = 0.0;
10
11        // Parse each media type in the Accept header
12        for media_type in accept_str.split(',') {
13            let media_type = media_type.trim();
14            let quality = media_type
15                .split(';')
16                .find_map(|param| {
17                    let param = param.trim();
18                    if let Some(q) = param.strip_prefix("q=") {
19                        q.parse::<f32>().ok()
20                    } else {
21                        None
22                    }
23                })
24                .unwrap_or(1.0); // Default quality factor is 1.0
25
26            if media_type.starts_with("application/octet-stream") {
27                octet_stream_q = quality;
28            } else if media_type.starts_with("application/json") {
29                json_q = quality;
30            }
31        }
32
33        // Prefer octet-stream if it has higher quality factor
34        octet_stream_q > json_q
35    })
36}