http stuff in regex

This commit is contained in:
deadvey 2025-01-12 02:29:11 +00:00
parent bb75241a3c
commit 88200eb354

View File

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