fid telnet url parsing

pull/14/head
dvkt 4 years ago
parent 7e70b4f785
commit 0989f94a85

@ -141,23 +141,26 @@ pub fn request(host: &str, port: &str, selector: &str, try_tls: bool) -> Result<
/// Parses gopher URL into parts.
/// Returns (Type, host, port, sel)
pub fn parse_url(url: &str) -> (Type, &str, &str, &str) {
let url = url.trim_start_matches("gopher://");
let mut url = url.trim_start_matches("gopher://");
let mut typ = Type::Menu;
let mut host;
let mut port = "70";
let mut sel = "";
// simple URLs, ex: "dog.com"
if !url.contains(':') && !url.contains('/') {
return (Type::Menu, url, "70", "");
}
// non-gopher URLs, stick everything in selector
if url.contains("://") {
// telnet urls
if url.starts_with("telnet://") {
typ = Type::Telnet;
url = url.trim_start_matches("telnet://");
} else if url.contains("://") {
// non-gopher URLs, stick everything in selector
return (Type::HTML, "", "", url);
}
let mut typ = Type::Menu;
let mut host;
let mut port = "70";
let mut sel = "";
// check selector first
if let Some(idx) = url.find('/') {
host = &url[..idx];
@ -188,11 +191,13 @@ pub fn parse_url(url: &str) -> (Type, &str, &str, &str) {
}
// ignore type prefix on selector
let mut chars = sel.chars();
if let (Some('/'), Some(c), Some('/')) = (chars.nth(0), chars.nth(0), chars.nth(0)) {
if let Some(t) = Type::from(c) {
typ = t;
sel = &sel[2..];
if typ != Type::Telnet {
let mut chars = sel.chars();
if let (Some('/'), Some(c)) = (chars.nth(0), chars.nth(0)) {
if let Some(t) = Type::from(c) {
typ = t;
sel = &sel[2..];
}
}
}
@ -220,6 +225,7 @@ mod tests {
"::1",
"ssh://kiosk@bitreich.org",
"https://github.com/dvkt/phetch",
"telnet://bbs.impakt.net:6502/",
];
let (typ, host, port, sel) = parse_url(urls[0]);
@ -305,5 +311,11 @@ mod tests {
assert_eq!(host, "");
assert_eq!(port, "");
assert_eq!(sel, "https://github.com/dvkt/phetch");
let (typ, host, port, sel) = parse_url(urls[14]);
assert_eq!(typ, Type::Telnet);
assert_eq!(host, "bbs.impakt.net");
assert_eq!(port, "6502");
assert_eq!(sel, "/");
}
}

@ -149,7 +149,7 @@ impl Menu {
($c:expr, $e:expr) => {{
out.push_str("\x1b[");
out.push_str($c);
out.push_str("m");
out.push('m');
out.push_str(&$e);
out.push_str("\x1b[0m");
}};
@ -725,6 +725,7 @@ impl Menu {
// assemble line info
let parts: Vec<&str> = line.split_terminator('\t').collect();
// first set item description
let mut name = String::from("");
if !parts[0].is_empty() {
name.push_str(&parts[0][1..].trim_end_matches('\r'));
@ -732,6 +733,7 @@ impl Menu {
if name.len() > longest {
longest = name.len();
}
// check for URL:<url> syntax
if parts.len() > 1 && parts[1].starts_with("URL:") {
lines.push(Line {
@ -747,7 +749,12 @@ impl Menu {
}
// assemble regular, gopher-style URL
let mut url = String::from("gopher://");
let mut url = if typ == Type::Telnet {
String::from("telnet://")
} else {
String::from("gopher://")
};
// host
if parts.len() > 2 {
url.push_str(parts[2]);
@ -761,7 +768,7 @@ impl Menu {
}
}
// selector
if parts.len() > 1 {
if parts.len() > 1 && typ != Type::Telnet {
let sel = parts[1].to_string();
if !sel.is_empty() {
// auto-prepend gopher type to selector
@ -819,14 +826,16 @@ i---------------------------------------------------------
1SDF PHLOGOSPHERE (297 phlogs) /phlogs/ gopher.club 70
1SDF GOPHERSPACE (1303 ACTIVE users) /maps/ sdf.org 70
1Geosphere Geosphere earth.rice.edu
8DJ's place a bbs.impakt.net 6502
i---------------------------------------------------------
"
);
assert_eq!(menu.lines.len(), 5);
assert_eq!(menu.links.len(), 3);
assert_eq!(menu.lines.len(), 6);
assert_eq!(menu.links.len(), 4);
assert_eq!(menu.lines[1].url, "gopher://gopher.club/1/phlogs/");
assert_eq!(menu.lines[2].url, "gopher://sdf.org/1/maps/");
assert_eq!(menu.lines[3].url, "gopher://earth.rice.edu/1Geosphere");
assert_eq!(menu.lines[4].url, "telnet://bbs.impakt.net:6502");
}
#[test]

Loading…
Cancel
Save