aquatic_http: store info_hash and peer_id in [u8; 20]

This commit is contained in:
Joakim Frostegård 2020-07-03 13:17:02 +02:00
parent f28abbb7f6
commit 52a29fc14a
2 changed files with 37 additions and 19 deletions

View file

@ -13,9 +13,9 @@ use serde_helpers::*;
#[serde(transparent)] #[serde(transparent)]
pub struct PeerId( pub struct PeerId(
#[serde( #[serde(
deserialize_with = "deserialize_20_char_string", deserialize_with = "deserialize_20_bytes",
)] )]
pub String pub [u8; 20]
); );
@ -23,9 +23,9 @@ pub struct PeerId(
#[serde(transparent)] #[serde(transparent)]
pub struct InfoHash( pub struct InfoHash(
#[serde( #[serde(
deserialize_with = "deserialize_20_char_string", deserialize_with = "deserialize_20_bytes",
)] )]
pub String pub [u8; 20]
); );
@ -180,8 +180,6 @@ impl Request {
let mut processed = String::new(); let mut processed = String::new();
for (i, part) in query_string.split('%').enumerate(){ for (i, part) in query_string.split('%').enumerate(){
println!("{}", part);
if i == 0 { if i == 0 {
processed.push_str(part); processed.push_str(part);
} else if part.len() >= 2 { } else if part.len() >= 2 {

View file

@ -39,35 +39,55 @@ pub fn deserialize_bool_from_number<'de, D>(
} }
struct TwentyCharStringVisitor;
impl<'de> Visitor<'de> for TwentyCharStringVisitor { /// Decode string of 20 byte-size chars to a [u8; 20]
type Value = String; struct TwentyByteVisitor;
impl<'de> Visitor<'de> for TwentyByteVisitor {
type Value = [u8; 20];
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("string consisting of 20 chars") formatter.write_str("string consisting of 20 bytes")
} }
#[inline] #[inline]
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where E: ::serde::de::Error, where E: ::serde::de::Error,
{ {
if value.chars().count() == 20 { let mut arr = [0u8; 20];
Ok(value.to_string()) let mut char_iter = value.chars();
for a in arr.iter_mut(){
if let Some(c) = char_iter.next(){
if c as u32 > 255 {
return Err(E::custom(format!(
"character not in single byte range: {:#?}",
c
)));
}
*a = c as u8;
} else { } else {
Err(E::custom(format!("not 20 chars: {:#?}", value))) return Err(E::custom(format!("less than 20 bytes: {:#?}", value)));
}
}
if char_iter.next().is_some(){
Err(E::custom(format!("more than 20 bytes: {:#?}", value)))
} else {
Ok(arr)
} }
} }
} }
#[inline] #[inline]
pub fn deserialize_20_char_string<'de, D>( pub fn deserialize_20_bytes<'de, D>(
deserializer: D deserializer: D
) -> Result<String, D::Error> ) -> Result<[u8; 20], D::Error>
where D: Deserializer<'de> where D: Deserializer<'de>
{ {
deserializer.deserialize_any(TwentyCharStringVisitor) deserializer.deserialize_any(TwentyByteVisitor)
} }
@ -85,7 +105,7 @@ impl<'de> Visitor<'de> for InfoHashVecVisitor {
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where E: ::serde::de::Error, where E: ::serde::de::Error,
{ {
match TwentyCharStringVisitor::visit_str::<E>(TwentyCharStringVisitor, value){ match TwentyByteVisitor::visit_str::<E>(TwentyByteVisitor, value){
Ok(arr) => Ok(vec![InfoHash(arr)]), Ok(arr) => Ok(vec![InfoHash(arr)]),
Err(err) => Err(E::custom(format!("got string, but {}", err))) Err(err) => Err(E::custom(format!("got string, but {}", err)))
} }
@ -98,8 +118,8 @@ impl<'de> Visitor<'de> for InfoHashVecVisitor {
let mut info_hashes: Self::Value = Vec::new(); let mut info_hashes: Self::Value = Vec::new();
while let Ok(Some(value)) = seq.next_element::<&str>(){ while let Ok(Some(value)) = seq.next_element::<&str>(){
let arr = TwentyCharStringVisitor::visit_str( let arr = TwentyByteVisitor::visit_str(
TwentyCharStringVisitor, value TwentyByteVisitor, value
)?; )?;
info_hashes.push(InfoHash(arr)); info_hashes.push(InfoHash(arr));