Bug fix with verification of magnet url's
When I changed it to just check a str slice against a str, it would never panic since I put in the str wrong, AND I made it so it had t be equal to said str to panic, not the other way around. Yikes.
This commit is contained in:
parent
648478bd50
commit
79b360792b
18
src/lib.rs
18
src/lib.rs
|
@ -4,7 +4,6 @@ use regex::Regex;
|
|||
extern crate lazy_static;
|
||||
|
||||
///The regexes used to identify specific parts of the magnet
|
||||
const MAGNET_URL_RE_STR: &str = r"^(stratum-|)magnet:\?";
|
||||
const DISPLAY_NAME_RE_STR: &str = r"dn=([A-Za-z0-9!@#$%^:*<>,?/()_+=.{}\{}\-]*)(&|$|\s)";
|
||||
const EXACT_TOPIC_RE_STR: &str = r"xt=urn:(sha1|btih|ed2k|aich|kzhash|md5|tree:tiger):([A-Fa-f0-9]+|[A-Za-z2-7]+)";
|
||||
const ADDRESS_TRACKER_RE_STR: &str = r"tr=([A-Za-z0-9!@#$%^:*<>,?/()_+=.{}\{}\-]*)(&|$|\s)";
|
||||
|
@ -135,13 +134,24 @@ pub struct Magnet {
|
|||
}
|
||||
|
||||
impl Magnet {
|
||||
pub fn gen_magnet_string(&self) -> String {
|
||||
let mut magnet_string =
|
||||
format!("magnet:?xt=urn:{}:{}&dn={}&xl={}&xs={}&kt={}&ws={}&as={}&mt={}", self.hash_type, self.xt, self.dn, self.xl, self.xs, self.kt, self.ws, self.acceptable_source, self.mt);
|
||||
|
||||
for tracker in &self.tr {
|
||||
magnet_string = format!("{}&tr={}", magnet_string, tracker)
|
||||
}
|
||||
|
||||
magnet_string
|
||||
|
||||
}
|
||||
|
||||
/**Given a magnet URL, identify the specific parts, and return the Magnet struct. If the program
|
||||
can't identify a specific part of the magnet, then it will either give an empty version of what
|
||||
its value would normally be (such as an empty string, an empty vector, or in the case of xl, -1)
|
||||
*/
|
||||
pub fn new (magnet_str: &str) -> Magnet {
|
||||
lazy_static! {
|
||||
static ref MAGNET_URL_RE: Regex = Regex::new(MAGNET_URL_RE_STR).unwrap();
|
||||
static ref DISPLAY_NAME_RE: Regex = Regex::new(DISPLAY_NAME_RE_STR).unwrap();
|
||||
static ref EXACT_TOPIC_RE: Regex = Regex::new(EXACT_TOPIC_RE_STR).unwrap();
|
||||
static ref EXACT_LENGTH_RE: Regex = Regex::new(EXACT_LENGTH_RE_STR).unwrap();
|
||||
|
@ -154,7 +164,7 @@ impl Magnet {
|
|||
}
|
||||
|
||||
// Panicking is a temporary fix, in version 2.0.0 it will instead return an Error
|
||||
if MAGNET_URL_RE.is_match(magnet_str) == false {
|
||||
if &magnet_str[0..8] != "magnet:?" {
|
||||
panic!("Invalid magnet url")
|
||||
}
|
||||
|
||||
|
@ -224,6 +234,8 @@ mod tests {
|
|||
assert_eq!(magnet_link.kt, String::new());
|
||||
assert_eq!(magnet_link.acceptable_source, String::new());
|
||||
assert_eq!(magnet_link.mt, String::new());
|
||||
|
||||
println!("{}", magnet_link.gen_magnet_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Reference in New Issue