aquatic_http_protocol: remove unused utility function "urldecode"

This commit is contained in:
Joakim Frostegård 2020-08-13 00:51:56 +02:00
parent 639b8db072
commit 74e1ecf384

View file

@ -3,7 +3,6 @@ use std::io::Write;
use anyhow::Context; use anyhow::Context;
use serde::{Serializer, Deserializer, de::Visitor}; use serde::{Serializer, Deserializer, de::Visitor};
use smartstring::{SmartString, LazyCompact};
use super::response::ResponsePeer; use super::response::ResponsePeer;
@ -67,44 +66,6 @@ pub fn urldecode_20_bytes(value: &str) -> anyhow::Result<[u8; 20]> {
} }
pub fn urldecode(value: &str) -> anyhow::Result<SmartString<LazyCompact>> {
let mut processed = SmartString::new();
let bytes = value.as_bytes();
let iter = ::memchr::memchr_iter(b'%', bytes);
let mut str_index_after_hex = 0usize;
for i in iter {
match (bytes.get(i), bytes.get(i + 1), bytes.get(i + 2)){
(Some(0..=127), Some(0..=127), Some(0..=127)) => {
if i > 0 {
processed.push_str(&value[str_index_after_hex..i]);
}
str_index_after_hex = i + 3;
let hex = &value[i + 1..i + 3];
let byte = u8::from_str_radix(&hex, 16)?;
processed.push(byte as char);
},
_ => {
return Err(anyhow::anyhow!(
"invalid urlencoded segment at byte {} in {}", i, value
));
}
}
}
if let Some(rest_of_str) = value.get(str_index_after_hex..){
processed.push_str(rest_of_str);
}
Ok(processed)
}
#[inline] #[inline]
pub fn serialize_20_bytes<S>( pub fn serialize_20_bytes<S>(
bytes: &[u8; 20], bytes: &[u8; 20],
@ -340,17 +301,6 @@ mod tests {
input == decoded input == decoded
} }
#[test]
fn test_urldecode(){
assert_eq!(urldecode("").unwrap(), "".to_string());
assert_eq!(urldecode("abc").unwrap(), "abc".to_string());
assert_eq!(urldecode("%21").unwrap(), "!".to_string());
assert_eq!(urldecode("%21%3D").unwrap(), "!=".to_string());
assert_eq!(urldecode("abc%21def%3Dghi").unwrap(), "abc!def=ghi".to_string());
assert!(urldecode("%").is_err());
assert!(urldecode("%å7").is_err());
}
#[quickcheck] #[quickcheck]
fn test_serde_response_peers_ipv4( fn test_serde_response_peers_ipv4(
peers: Vec<ResponsePeer<Ipv4Addr>>, peers: Vec<ResponsePeer<Ipv4Addr>>,