sentinel_modsec/variables/
response.rs

1//! Response data for variable resolution.
2
3use super::collection::HashMapCollection;
4
5/// Response data container.
6#[derive(Debug, Clone, Default)]
7pub struct ResponseData {
8    /// HTTP status code.
9    pub status: u16,
10    /// Response protocol.
11    pub protocol: String,
12    /// Response headers.
13    pub headers: HashMapCollection,
14    /// Response body.
15    pub body: Vec<u8>,
16    /// Content type.
17    pub content_type: String,
18}
19
20impl ResponseData {
21    /// Create new response data.
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Set the status code.
27    pub fn set_status(&mut self, status: u16) {
28        self.status = status;
29    }
30
31    /// Set the protocol.
32    pub fn set_protocol(&mut self, protocol: &str) {
33        self.protocol = protocol.to_string();
34    }
35
36    /// Add a response header.
37    pub fn add_header(&mut self, name: &str, value: &str) {
38        self.headers.add(name.to_lowercase(), value.to_string());
39
40        // Track content-type
41        if name.eq_ignore_ascii_case("content-type") {
42            self.content_type = value.to_string();
43        }
44    }
45
46    /// Append to response body.
47    pub fn append_body(&mut self, data: &[u8]) {
48        self.body.extend_from_slice(data);
49    }
50
51    /// Get body as string.
52    pub fn body_str(&self) -> String {
53        String::from_utf8_lossy(&self.body).to_string()
54    }
55
56    /// Get body length.
57    pub fn body_length(&self) -> usize {
58        self.body.len()
59    }
60}