diff --git a/TODO.md b/TODO.md index 3997808..5d608fa 100644 --- a/TODO.md +++ b/TODO.md @@ -14,7 +14,8 @@ * compact peer representation in announce response: is implementation correct? * scrape info hash parsing: multiple ought to be accepted * move stuff to common crate with ws: what about Request/InMessage etc? -* info hashes, peer ids: verify that they are 20 bytes +* info hashes, peer ids: check that whole deserialization and url decoding + works as it should. There are suspicously many `\u{fffd}` * AnnounceRequest.compact: parse int to bool ## aquatic_ws diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index 99bf662..0c54503 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -184,13 +184,6 @@ pub fn run_handshake_and_read_requests<'a>( debug!("read request, sending to handler"); - if let Request::Announce(ref request) = request { - for (i, c) in request.info_hash.0.chars().enumerate() { - debug!("{}: {}", i, c.escape_unicode()); - } - debug!("request info hash char count: {}", request.info_hash.0.chars().count()); - } - if let Err(err) = request_channel_sender .send((meta, request)) { diff --git a/aquatic_http/src/lib/protocol/mod.rs b/aquatic_http/src/lib/protocol/mod.rs index d8a26ae..6b50c87 100644 --- a/aquatic_http/src/lib/protocol/mod.rs +++ b/aquatic_http/src/lib/protocol/mod.rs @@ -9,30 +9,6 @@ mod serde_helpers; use serde_helpers::*; -pub fn serialize_response_peers_compact( - response_peers: &Vec, - serializer: S -) -> Result where S: Serializer { - let mut bytes = Vec::with_capacity(response_peers.len() * 6); - - for peer in response_peers { - match peer.ip_address { - IpAddr::V4(ip) => { - bytes.extend_from_slice(&u32::from(ip).to_be_bytes()); - bytes.extend_from_slice(&peer.port.to_be_bytes()) - }, - IpAddr::V6(_) => { - continue - } - } - } - - let text: String = bytes.into_iter().map(|byte| byte as char).collect(); - - serializer.serialize_str(&text) -} - - #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] #[serde(transparent)] pub struct PeerId( diff --git a/aquatic_http/src/lib/protocol/serde_helpers.rs b/aquatic_http/src/lib/protocol/serde_helpers.rs index 00ba4a8..956119c 100644 --- a/aquatic_http/src/lib/protocol/serde_helpers.rs +++ b/aquatic_http/src/lib/protocol/serde_helpers.rs @@ -1,6 +1,8 @@ +use std::net::IpAddr; + use serde::{Serializer, Deserializer, de::{Visitor, SeqAccess}}; -use super::InfoHash; +use super::{InfoHash, ResponsePeer}; struct TwentyCharStringVisitor; @@ -90,4 +92,28 @@ pub fn deserialize_info_hashes<'de, D>( where D: Deserializer<'de>, { Ok(deserializer.deserialize_any(InfoHashVecVisitor).unwrap_or_default()) +} + + +pub fn serialize_response_peers_compact( + response_peers: &Vec, + serializer: S +) -> Result where S: Serializer { + let mut bytes = Vec::with_capacity(response_peers.len() * 6); + + for peer in response_peers { + match peer.ip_address { + IpAddr::V4(ip) => { + bytes.extend_from_slice(&u32::from(ip).to_be_bytes()); + bytes.extend_from_slice(&peer.port.to_be_bytes()) + }, + IpAddr::V6(_) => { + continue + } + } + } + + let text: String = bytes.into_iter().map(|byte| byte as char).collect(); + + serializer.serialize_str(&text) } \ No newline at end of file