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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use crate::error::{HttpSigError, HttpSigResult};
use sfv::{Parser, SerializeValue};

type IndexSet<K> = indexmap::IndexSet<K, fxhash::FxBuildHasher>;

/* ---------------------------------------------------------------- */
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
/// Http message component parameters that appends with `;` in the signature input
/// https://datatracker.ietf.org/doc/html/rfc9421#secion-2.1
pub enum HttpMessageComponentParam {
  /// sf: https://datatracker.ietf.org/doc/html/rfc9421#section-2.1.1
  Sf,
  /// key: https://datatracker.ietf.org/doc/html/rfc9421#section-2.1.2
  /// This will be encoded to `;key="..."` in the signature input
  Key(String),
  /// bs: https://datatracker.ietf.org/doc/html/rfc9421#section-2.1.3
  Bs,
  // tr: https://datatracker.ietf.org/doc/html/rfc9421#section-2.1.4
  Tr,
  // req: https://datatracker.ietf.org/doc/html/rfc9421#section-2.4
  Req,
  // name: https://datatracker.ietf.org/doc/html/rfc9421#name-query-parameters
  /// This will be encoded to `;name="..."` in the signature input
  Name(String),
}

impl From<HttpMessageComponentParam> for String {
  fn from(val: HttpMessageComponentParam) -> Self {
    match val {
      HttpMessageComponentParam::Sf => "sf".to_string(),
      HttpMessageComponentParam::Key(val) => format!("key=\"{val}\""),
      HttpMessageComponentParam::Bs => "bs".to_string(),
      HttpMessageComponentParam::Tr => "tr".to_string(),
      HttpMessageComponentParam::Req => "req".to_string(),
      HttpMessageComponentParam::Name(v) => format!("name=\"{v}\""),
    }
  }
}

impl TryFrom<(&str, &sfv::BareItem)> for HttpMessageComponentParam {
  type Error = HttpSigError;
  fn try_from((key, val): (&str, &sfv::BareItem)) -> Result<Self, Self::Error> {
    match key {
      "sf" => Ok(Self::Sf),
      "bs" => Ok(Self::Bs),
      "tr" => Ok(Self::Tr),
      "req" => Ok(Self::Req),
      "name" => {
        let name = val.as_str().ok_or(HttpSigError::InvalidComponentParam(
          "Invalid http field param: name".to_string(),
        ))?;
        Ok(Self::Name(name.to_string()))
      }
      "key" => {
        let key = val.as_str().ok_or(HttpSigError::InvalidComponentParam(
          "Invalid http field param: key".to_string(),
        ))?;
        Ok(Self::Key(key.to_string()))
      }
      _ => Err(HttpSigError::InvalidComponentParam(format!(
        "Invalid http field param: {key}"
      ))),
    }
  }
}

#[derive(PartialEq, Eq, Debug, Clone)]
/// Http message component parameters
pub struct HttpMessageComponentParams(pub IndexSet<HttpMessageComponentParam>);

impl std::hash::Hash for HttpMessageComponentParams {
  fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
    let mut params = self.0.iter().map(|v| v.clone().into()).collect::<Vec<String>>();
    params.sort();
    params.hash(state);
  }
}

impl TryFrom<&sfv::Parameters> for HttpMessageComponentParams {
  type Error = HttpSigError;
  fn try_from(val: &sfv::Parameters) -> Result<Self, Self::Error> {
    let hs = val
      .iter()
      .map(|(k, v)| HttpMessageComponentParam::try_from((k.as_str(), v)))
      .collect::<Result<IndexSet<_>, _>>()?;
    Ok(Self(hs))
  }
}
impl std::fmt::Display for HttpMessageComponentParams {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    if !self.0.is_empty() {
      write!(
        f,
        ";{}",
        self.0.iter().map(|v| v.clone().into()).collect::<Vec<String>>().join(";")
      )
    } else {
      Ok(())
    }
  }
}

/* ---------------------------------------------------------------- */
/// Handle `sf` parameter
pub(super) fn handle_params_sf(field_values: &mut [String]) -> HttpSigResult<()> {
  let parsed_list = field_values
    .iter()
    .map(|v| {
      if let Ok(list) = Parser::parse_list(v.as_bytes()) {
        list.serialize_value()
      } else if let Ok(dict) = Parser::parse_dictionary(v.as_bytes()) {
        dict.serialize_value()
      } else {
        Err("invalid structured field value for sf")
      }
    })
    .collect::<Result<Vec<_>, _>>()
    .map_err(|e| HttpSigError::InvalidComponentParam(format!("Failed to parse structured field value: {e}")))?;

  field_values.iter_mut().zip(parsed_list).for_each(|(v, p)| {
    *v = p;
  });

  Ok(())
}

/* ---------------------------------------------------------------- */
/// Handle `key` parameter, returns new field values
pub(super) fn handle_params_key_into(field_values: &[String], key: &str) -> HttpSigResult<Vec<String>> {
  let dicts = field_values
    .iter()
    .map(|v| Parser::parse_dictionary(v.as_bytes()))
    .collect::<Result<Vec<_>, _>>()
    .map_err(|e| HttpSigError::InvalidComponentParam(format!("Failed to parse structured field value: {e}")))?;

  let found_entries = dicts
    .into_iter()
    .filter_map(|dict| {
      dict.get(key).map(|v| {
        let sfvalue: sfv::List = vec![v.clone()];
        sfvalue.serialize_value()
      })
    })
    .collect::<Result<Vec<_>, _>>()
    .map_err(|e| HttpSigError::InvalidComponentParam(format!("Failed to serialize structured field value: {e}")))?;

  Ok(found_entries)
}

/* ---------------------------------------------------------------- */

mod tests {
  #[allow(unused)]
  use super::*;

  #[test]
  fn parser_test() {
    // Parsing structured field value of Item type.
    let item_header_input = "12.445;foo=bar";
    let item = Parser::parse_item(item_header_input.as_bytes()).unwrap();
    assert_eq!(item.serialize_value().unwrap(), item_header_input);

    // Parsing structured field value of List type.
    let list_header_input = "  1; a=tok, (\"foo\"   \"bar\" );baz, (  )";
    let list = Parser::parse_list(list_header_input.as_bytes()).unwrap();
    assert_eq!(list.serialize_value().unwrap(), "1;a=tok, (\"foo\" \"bar\");baz, ()");

    // Parsing structured field value of Dictionary type.
    let dict_header_input = "a=?0, b, c; foo=bar, rating=1.5, fruits=(apple pear), d";
    let dict = Parser::parse_dictionary(dict_header_input.as_bytes()).unwrap();
    assert_eq!(
      dict.serialize_value().unwrap(),
      "a=?0, b, c;foo=bar, rating=1.5, fruits=(apple pear), d"
    );
  }
}