diff --git a/src/main.rs b/src/main.rs index e735c58..d9d38a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -190,46 +190,65 @@ fn input() -> String{ return s; } -fn parse_url(url: String, previous_host: &String) -> (String, String, String) { +fn parse_url(url: String, previous_host: &String) -> (String, String, String, String) { let mut host: String = previous_host.to_string(); let mut port: String = "3477".to_string(); let mut path: String = "/".to_string(); + let mut protocol: String = "mttp".to_string(); let mttp_regex = Regex::new(r"^mttp:\/\/(.*?)\/(.*?)$").unwrap(); // mttp://example.com/index.md let mttp_regex_no_path = Regex::new(r"^mttp:\/\/(.*?)$").unwrap(); // mttp://example.com let accept_this_as_mttp = Regex::new(r"^(.*?)$").unwrap(); // example.com let no_protocol_but_path = Regex::new(r"^(.*?)/(.*?)$").unwrap(); // example.com/index.md let path_change = Regex::new(r"^/(.*?)$").unwrap(); // /index.md + let http_regex = Regex::new(r"^http:\/\/(.*?)\/(.*?)$").unwrap(); // http://example.com/index.md + let http_regex_no_path = Regex::new(r"^http:\/\/(.*?)$").unwrap(); // http://example.com if let Some(caps) = mttp_regex.captures(&url) { host = caps[1].to_string(); port = "3477".to_string(); path = caps[2].to_string(); + protocol = "mttp".to_string(); } else if let Some(caps) = mttp_regex_no_path.captures(&url) { host = caps[1].to_string(); port = "3477".to_string(); path = "/".to_string(); + protocol = "mttp".to_string(); } else if let Some(caps) = path_change.captures(&url) { - println!("path change"); host = previous_host.to_string(); port = "3477".to_string(); path = caps[1].to_string(); + protocol = "mttp".to_string(); } else if let Some(caps) = no_protocol_but_path.captures(&url) { host = caps[1].to_string(); port = "3477".to_string(); path = caps[2].to_string(); + protocol = "mttp".to_string(); } else if let Some(caps) = accept_this_as_mttp.captures(&url) { host = caps[1].to_string(); port = "3477".to_string(); path = "/".to_string(); + protocol = "mttp".to_string(); } - println!("{}:{}/{}",host,port,path); + else if let Some(caps) = http_regex.captures(&url) { + host = caps[1].to_string(); + port = "80".to_string(); + path = caps[2].to_string(); + protocol = "http".to_string(); + } + else if let Some(caps) = http_regex_no_path.captures(&url) { + host = caps[1].to_string(); + port = "80".to_string(); + path = "/".to_string(); + protocol = "http".to_string(); + } + println!("{}://{}:{}/{}",protocol,host,port,path); - return (host, port, path); + return (host, port, path, protocol); } fn main() { @@ -241,7 +260,7 @@ fn main() { std::process::exit(0); } - let (mut host, mut port, mut path) = parse_url(url, &"example.com".to_string()); // Must pass + let (mut host, mut port, mut path, mut protocol) = parse_url(url, &"example.com".to_string()); // Must pass // 'previous // host' let mut load_page: bool = true; @@ -280,7 +299,7 @@ fn main() { else { if let Ok(number) = link_to_follow.parse::() { if number < links.len() { - (host, port, path) = parse_url(links[number].clone(), &host); + (host, port, path, protocol) = parse_url(links[number].clone(), &host); } else { println!("Invalid reference id"); load_page = false;