skp_cache_http/
response.rs

1use serde::{Serialize, Deserialize};
2use http::{StatusCode, HeaderMap, HeaderName, HeaderValue};
3use std::collections::HashMap;
4
5/// Serializable wrapper around HTTP response
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct CachedResponse {
8    pub status: u16,
9    pub headers: HashMap<String, String>,
10    pub body: Vec<u8>,
11}
12
13impl CachedResponse {
14    /// Create new cached response
15    pub fn new(status: u16, headers: HashMap<String, String>, body: Vec<u8>) -> Self {
16        Self { status, headers, body }
17    }
18    
19    /// Create from http::Response parts
20    pub fn from_parts(status: StatusCode, headers: &HeaderMap, body: Vec<u8>) -> Self {
21        let mut headers_map = HashMap::new();
22        for (k, v) in headers.iter() {
23            if let Ok(s) = v.to_str() {
24                headers_map.insert(k.to_string(), s.to_string());
25            }
26        }
27        
28        Self {
29            status: status.as_u16(),
30            headers: headers_map,
31            body,
32        }
33    }
34    
35    /// Convert headers to HeaderMap
36    pub fn headers_map(&self) -> HeaderMap {
37        let mut map = HeaderMap::new();
38        for (k, v) in &self.headers {
39             if let (Ok(name), Ok(val)) = (HeaderName::from_bytes(k.as_bytes()), HeaderValue::from_str(v)) {
40                 map.insert(name, val);
41             }
42        }
43        map
44    }
45}