1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file
/// An HTTP Response to be returned by Request filters.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Default)]
pub struct Response {
status_code: u32,
headers: Vec<(String, String)>,
body: Vec<u8>,
}
impl Response {
/// Creates a new response.
pub fn new(status_code: u32) -> Self {
Self {
status_code,
headers: Vec::new(),
body: Vec::new(),
}
}
/// Consumes the current [`Response`] and returns a new one with a list of `headers` setted.
pub fn with_headers(mut self, headers: impl Into<Vec<(String, String)>>) -> Self {
self.headers = headers.into();
self
}
/// Consumes the current [`Response`] and returns a new one with a `body` setted.
pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
self.body = body.into();
self
}
/// Returns the status code.
pub fn status_code(&self) -> u32 {
self.status_code
}
/// Returns the headers as a list of pairs of name and value.
pub fn headers(&self) -> Vec<(&str, &str)> {
self.headers
.iter()
.map(|(k, v)| (k.as_str(), v.as_str()))
.collect()
}
/// Returns the body if exists in binary format.
pub fn body(&self) -> Option<&[u8]> {
(!self.body.as_slice().is_empty()).then_some(self.body.as_slice())
}
}