Skip to content
Snippets Groups Projects
Commit 374dcc3e authored by codecraft's avatar codecraft :crocodile:
Browse files

Update `get_post_form_text` to allow for an array of keys to search

parent fe8dba58
No related branches found
No related tags found
1 merge request!1Initial feature merge
...@@ -72,9 +72,9 @@ impl Request<'_> { ...@@ -72,9 +72,9 @@ impl Request<'_> {
} }
} }
// pub fn get_post_form_key<T: FromRequest>(&self, data: Data) -> T {} // pub fn get_post_form_key<T: FromRequest>(&self, data: Data) -> T {}
pub fn get_get_form_keys( pub fn get_get_form_keys<'a>(
&self, &'a self,
keys: Vec<&str>, keys: &'a [&str],
) -> Result<HashMap<&str, &str>, ParseFormError> { ) -> Result<HashMap<&str, &str>, ParseFormError> {
let data = if let Some(val) = self.uri.split_once("?") { let data = if let Some(val) = self.uri.split_once("?") {
val val
...@@ -111,7 +111,11 @@ impl Request<'_> { ...@@ -111,7 +111,11 @@ impl Request<'_> {
} }
Ok(response) Ok(response)
} }
pub fn get_post_text_form_key(&self, key: &str, data: &Data) -> Result<String, ()> { pub fn get_post_text_form_key(
&self,
keys: &[&str],
data: &Data,
) -> Result<Vec<String>, ParseFormError> {
let mut post_type = self let mut post_type = self
.headers .headers
.iter() .iter()
...@@ -134,11 +138,18 @@ impl Request<'_> { ...@@ -134,11 +138,18 @@ impl Request<'_> {
.split("&") .split("&")
.map(|kvp| kvp.split_once("=").unwrap()) .map(|kvp| kvp.split_once("=").unwrap())
.collect::<HashMap<&str, &str>>(); .collect::<HashMap<&str, &str>>();
if let Some(val) = kvps.get(key) {
Ok(val.to_string()) let mut result: Vec<String> = Vec::with_capacity(keys.len());
} else { for key in keys {
Err(()) if let Some(val) = kvps.get(key) {
result.push(val.to_string());
} else {
return Err(ParseFormError {
error: ParseErrors::NoData,
});
}
} }
Ok(result)
} }
Mime::MultipartFormData => { Mime::MultipartFormData => {
let from_req = post_type[1..] let from_req = post_type[1..]
...@@ -158,16 +169,17 @@ impl Request<'_> { ...@@ -158,16 +169,17 @@ impl Request<'_> {
.collect::<Vec<&[u8]>>(); .collect::<Vec<&[u8]>>();
let mut boundary_found = false; let mut boundary_found = false;
let mut key_found = false; let mut current_key: Option<usize> = None;
let mut value = vec![]; let mut result: Vec<Vec<u8>> = vec!["".into(); keys.len()];
for part in parts { for part in parts {
if part == [] { if part == [] {
continue; continue;
} }
if (key_found && part == boundary) || part == end_boundary { if part == end_boundary {
break; break;
} }
if !boundary_found && part == boundary { if !boundary_found && part == boundary {
current_key = None;
boundary_found = true; boundary_found = true;
continue; continue;
} }
...@@ -188,18 +200,27 @@ impl Request<'_> { ...@@ -188,18 +200,27 @@ impl Request<'_> {
.trim_end() .trim_end()
.trim_matches('"') .trim_matches('"')
.to_owned(); .to_owned();
key_found = key == mkey; let mut index = 0;
} else if key_found == true { for i in keys {
value.extend_from_slice(part); if *i == mkey {
value.extend_from_slice(&[b'\n']); current_key = Some(index);
}
index += 1;
}
boundary_found = false;
} else if let Some(key) = current_key {
result[key].extend_from_slice(part);
result[key].extend_from_slice(&[b'\n']);
} }
} }
Ok(String::from_utf8_lossy(&value) Ok(result
.to_owned() .iter()
.trim() .map(|arr| String::from_utf8(arr.to_vec()).unwrap().trim().into())
.to_string()) .collect())
} }
_ => Err(()), _ => Err(ParseFormError {
error: ParseErrors::BadData,
}),
} }
} }
} }
...@@ -232,11 +253,16 @@ value2\r ...@@ -232,11 +253,16 @@ value2\r
}; };
assert_eq!( assert_eq!(
"value1", "value1",
req.get_post_text_form_key("field1", &data).unwrap() req.get_post_text_form_key(&["field1"], &data).unwrap()[0]
); );
assert_eq!( assert_eq!(
"value2", "value2",
req.get_post_text_form_key("field2", &data).unwrap() req.get_post_text_form_key(&["field2"], &data).unwrap()[0]
); );
assert_eq!(
vec!["value1", "value2"],
req.get_post_text_form_key(&["field1", "field2"], &data)
.unwrap()
)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment