Skip to main content

s3_wasi_http/api/
conditional_headers.rs

1use chrono::{DateTime, Utc};
2
3/// Set conditional headers on a request
4///
5/// see [super::S3RequestBuilder::set_conditional_headers]
6#[derive(Default)]
7pub struct ConditionalHeaders {
8    if_match: Option<String>,
9    if_modified_since: Option<DateTime<Utc>>,
10    if_none_match: Option<String>,
11    if_unmodified_since: Option<DateTime<Utc>>,
12}
13
14
15impl ConditionalHeaders {
16    pub fn if_match(&mut self, value: &str) -> &mut Self {
17        self.if_match = Some(value.to_owned());
18        self
19    }
20
21    pub fn if_modified_since(&mut self, datetime: DateTime<Utc>) -> &mut Self {
22        self.if_modified_since = Some(datetime);
23        self
24    }
25
26    pub fn if_none_match(&mut self, value: &str) -> &mut Self {
27        self.if_none_match = Some(value.to_owned());
28        self
29    }
30
31    pub fn if_unmodified_since(&mut self, datetime: DateTime<Utc>) -> &mut Self {
32        self.if_unmodified_since = Some(datetime);
33        self
34    }
35
36    pub(crate) fn get_headers(&self) -> Vec<(String, String)> {
37        let mut headers = Vec::new();
38        if let Some(value) = &self.if_match {
39            headers.push(("If-Match".to_string(), value.to_owned()));
40        }
41        if let Some(datetime) = &self.if_modified_since {
42            headers.push((
43                "If-Modified-Since".to_string(),
44                datetime.format("%A, %d %b %Y %H:%M:%S GMT").to_string(),
45            ));
46        }
47        if let Some(value) = &self.if_none_match {
48            headers.push(("If-None-Match".to_string(), value.to_owned()));
49        }
50        if let Some(datetime) = &self.if_unmodified_since {
51            headers.push((
52                "If-Unmodified-Since".to_string(),
53                datetime.format("%A, %d %b %Y %H:%M:%S GMT").to_string(),
54            ));
55        }
56
57        headers
58    }
59}