forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(http): [torrust#160] extract functions for percent decoding
- Loading branch information
1 parent
849633d
commit 13a4982
Showing
10 changed files
with
223 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::protocol::info_hash::InfoHash; | ||
use crate::tracker::peer::{self, IdConversionError}; | ||
|
||
/// # Errors | ||
/// | ||
/// Will return `Err` if if the decoded bytes do not represent a valid `InfoHash`. | ||
pub fn percent_decode_info_hash(raw_info_hash: &str) -> Result<InfoHash, binascii::ConvertError> { | ||
let bytes = percent_encoding::percent_decode_str(raw_info_hash).collect::<Vec<u8>>(); | ||
InfoHash::from_str(&hex::encode(bytes)) | ||
} | ||
|
||
/// # Errors | ||
/// | ||
/// Will return `Err` if if the decoded bytes do not represent a valid `peer::Id`. | ||
pub fn percent_decode_peer_id(raw_peer_id: &str) -> Result<peer::Id, IdConversionError> { | ||
let bytes = percent_encoding::percent_decode_str(raw_peer_id).collect::<Vec<u8>>(); | ||
peer::Id::try_from(bytes) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::str::FromStr; | ||
|
||
use crate::http::percent_encoding::{percent_decode_info_hash, percent_decode_peer_id}; | ||
use crate::protocol::info_hash::InfoHash; | ||
use crate::tracker::peer; | ||
|
||
#[test] | ||
fn it_should_decode_a_percent_encoded_info_hash() { | ||
let encoded_infohash = "%3B%24U%04%CF%5F%11%BB%DB%E1%20%1C%EAjk%F4Z%EE%1B%C0"; | ||
|
||
let info_hash = percent_decode_info_hash(encoded_infohash).unwrap(); | ||
|
||
assert_eq!( | ||
info_hash, | ||
InfoHash::from_str("3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0").unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn it_should_fail_decoding_an_invalid_percent_encoded_info_hash() { | ||
let invalid_encoded_infohash = "invalid percent-encoded infohash"; | ||
|
||
let info_hash = percent_decode_info_hash(invalid_encoded_infohash); | ||
|
||
assert!(info_hash.is_err()); | ||
} | ||
|
||
#[test] | ||
fn it_should_decode_a_percent_encoded_peer_id() { | ||
let encoded_peer_id = "%2DqB00000000000000000"; | ||
|
||
let peer_id = percent_decode_peer_id(encoded_peer_id).unwrap(); | ||
|
||
assert_eq!(peer_id, peer::Id(*b"-qB00000000000000000")); | ||
} | ||
|
||
#[test] | ||
fn it_should_fail_decoding_an_invalid_percent_encoded_peer_id() { | ||
let invalid_encoded_peer_id = "invalid percent-encoded peer id"; | ||
|
||
let peer_id = percent_decode_peer_id(invalid_encoded_peer_id); | ||
|
||
assert!(peer_id.is_err()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
pub mod asserts; | ||
pub mod bencode; | ||
pub mod client; | ||
pub mod connection_info; | ||
pub mod requests; | ||
pub mod responses; | ||
pub mod server; | ||
|
||
use percent_encoding::NON_ALPHANUMERIC; | ||
|
||
pub type ByteArray20 = [u8; 20]; | ||
|
||
pub fn percent_encode_byte_array(bytes: &ByteArray20) -> String { | ||
percent_encoding::percent_encode(bytes, NON_ALPHANUMERIC).to_string() | ||
} | ||
|
||
pub struct InfoHash(ByteArray20); | ||
|
||
impl InfoHash { | ||
pub fn new(vec: &[u8]) -> Self { | ||
let mut byte_array_20: ByteArray20 = Default::default(); | ||
byte_array_20.clone_from_slice(vec); | ||
Self(byte_array_20) | ||
} | ||
|
||
pub fn bytes(&self) -> ByteArray20 { | ||
self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.