You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phetch/src/gopher.rs

292 lines
8.9 KiB
Rust

5 years ago
use gopher;
5 years ago
use std::io::{Read, Result, Write};
use std::net::TcpStream;
use std::net::ToSocketAddrs;
use std::os::unix::fs::OpenOptionsExt;
use std::time::Duration;
use termion::input::TermRead;
5 years ago
pub const TCP_TIMEOUT_IN_SECS: u64 = 8;
pub const TCP_TIMEOUT_DURATION: Duration = Duration::from_secs(TCP_TIMEOUT_IN_SECS);
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Type {
5 years ago
Text, // 0 | 96 | cyan
Menu, // 1 | 94 | blue
CSOEntity, // 2
Error, // 3 | 91 | red
Binhex, // 4 | 4 | white underline
DOSFile, // 5 | 4 | white underline
UUEncoded, // 6 | 4 | white underline
Search, // 7 | 0 | white
Telnet, // 8
Binary, // 9 | 4 | white underline
Mirror, // +
GIF, // g | 4 | white underline
Telnet3270, // T
HTML, // h | 92 | green
5 years ago
Image, // I | 4 | white underline
5 years ago
PNG, // p | 4 | white underline
5 years ago
Info, // i | 93 | yellow
Sound, // s | 4 | white underline
Document, // d | 4 | white underline
5 years ago
}
impl Type {
pub fn is_download(self) -> bool {
5 years ago
match self {
Type::Binhex
| Type::DOSFile
| Type::UUEncoded
| Type::Binary
| Type::GIF
5 years ago
| Type::Image
5 years ago
| Type::PNG
5 years ago
| Type::Sound
| Type::Document => true,
_ => false,
}
}
}
5 years ago
pub fn type_for_char(c: char) -> Option<Type> {
match c {
'0' => Some(Type::Text),
'1' => Some(Type::Menu),
'2' => Some(Type::CSOEntity),
'3' => Some(Type::Error),
'4' => Some(Type::Binhex),
'5' => Some(Type::DOSFile),
'6' => Some(Type::UUEncoded),
'7' => Some(Type::Search),
'8' => Some(Type::Telnet),
'9' => Some(Type::Binary),
'+' => Some(Type::Mirror),
'g' => Some(Type::GIF),
'T' => Some(Type::Telnet3270),
'h' => Some(Type::HTML),
5 years ago
'I' => Some(Type::Image),
5 years ago
'p' => Some(Type::PNG),
5 years ago
'i' => Some(Type::Info),
's' => Some(Type::Sound),
'd' => Some(Type::Document),
_ => None,
}
}
5 years ago
macro_rules! error {
($e:expr) => {
std::io::Error::new(std::io::ErrorKind::Other, $e)
};
($e:expr, $($y:expr),*) => {
error!(format!($e, $($y),*));
};
}
5 years ago
// Fetches a gopher URL and returns a raw Gopher response.
5 years ago
pub fn fetch_url(url: &str) -> Result<String> {
5 years ago
let (_, host, port, sel) = parse_url(url);
fetch(host, port, sel)
}
5 years ago
// Fetches a gopher URL by its component parts and returns a raw Gopher response.
5 years ago
pub fn fetch(host: &str, port: &str, selector: &str) -> Result<String> {
let mut body = String::new();
5 years ago
let selector = selector.replace('?', "\t"); // search queries
format!("{}:{}", host, port)
.to_socket_addrs()
5 years ago
.and_then(|mut socks| socks.next().ok_or_else(|| error!("Can't create socket")))
5 years ago
.and_then(|sock| TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION))
.and_then(|mut stream| {
stream.write(format!("{}\r\n", selector).as_ref());
Ok(stream)
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION));
stream.read_to_string(&mut body)?;
Ok(body)
})
}
5 years ago
5 years ago
// Downloads a binary to disk.
// Returns the path it was saved to and the size in bytes.
pub fn download_url(url: &str) -> Result<(String, usize)> {
let (_, host, port, sel) = parse_url(url);
let sel = sel.replace('?', "\t"); // search queries
5 years ago
let filename = sel
.split_terminator('/')
.rev()
.nth(0)
5 years ago
.ok_or_else(|| error!("Bad download filename: {}", sel))?;
5 years ago
let mut path = std::path::PathBuf::from(".");
path.push(filename);
let stdin = termion::async_stdin();
let mut keys = stdin.keys();
5 years ago
format!("{}:{}", host, port)
.to_socket_addrs()
5 years ago
.and_then(|mut socks| socks.next().ok_or_else(|| error!("Can't create socket")))
5 years ago
.and_then(|sock| TcpStream::connect_timeout(&sock, TCP_TIMEOUT_DURATION))
.and_then(|mut stream| {
stream.write(format!("{}\r\n", sel).as_ref());
Ok(stream)
5 years ago
})
.and_then(|mut stream| {
stream.set_read_timeout(Some(TCP_TIMEOUT_DURATION))?;
5 years ago
let mut file = std::fs::OpenOptions::new()
5 years ago
.write(true)
.create(true)
.truncate(true)
.mode(0o770)
.open(path)?;
5 years ago
let mut buf = [0; 1024];
5 years ago
let mut bytes = 0;
while let Ok(count) = stream.read(&mut buf) {
if count == 0 {
break;
}
bytes += count;
5 years ago
file.write(&buf[..count]);
if let Some(Ok(termion::event::Key::Ctrl('c'))) = keys.next() {
return Err(error!("Download canceled"));
}
5 years ago
}
Ok((filename.to_string(), bytes))
})
}
5 years ago
// Parses gopher URL into parts.
// Return (Type, host, port, sel)
pub fn parse_url(url: &str) -> (Type, &str, &str, &str) {
5 years ago
let url = url.trim_start_matches("gopher://");
// simple URLs
if !url.contains(':') && !url.contains('/') {
return (Type::Menu, url, "70", "/");
}
let mut typ = Type::Menu;
let mut host;
5 years ago
let mut port = "70";
let mut sel = "/";
// check selector first
if let Some(idx) = url.find('/') {
host = &url[..idx];
sel = &url[idx..];
} else {
host = &url;
5 years ago
}
// grab port
if let Some(idx) = host.find(':') {
if host.len() > idx + 1 {
port = &host[idx + 1..];
host = &host[..idx];
}
}
5 years ago
// ignore type prefix on selector
5 years ago
let mut chars = sel.chars();
5 years ago
if let (Some('/'), Some(c), Some('/')) = (chars.nth(0), chars.nth(0), chars.nth(0)) {
if let Some(t) = gopher::type_for_char(c) {
typ = t;
sel = &sel[2..];
5 years ago
}
}
(typ, host, port, sel)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple_parse() {
let urls = vec![
"gopher://gopher.club/1/phlogs/",
"gopher://sdf.org:7777/1/maps",
"gopher.floodgap.org",
"gopher.floodgap.com/0/gopher/relevance.txt",
"gopher://gopherpedia.com/7/lookup?Gopher",
"::1",
"gopher://dead:beef:1234:5678:9012:3456:feed:deed",
"1234:2345:dead:4567:7890:1234:beef:1111:7443/1/files",
"2001:cdba:0000:0000:0000:0000:3257:9652",
"2001:cdba::3257:9652",
"2001:cdba::3257:9652:71",
];
let (typ, host, port, sel) = parse_url(urls[0]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "gopher.club");
assert_eq!(port, "70");
assert_eq!(sel, "/phlogs/");
let (typ, host, port, sel) = parse_url(urls[1]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "sdf.org");
assert_eq!(port, "7777");
assert_eq!(sel, "/maps");
let (typ, host, port, sel) = parse_url(urls[2]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "gopher.floodgap.org");
assert_eq!(port, "70");
assert_eq!(sel, "/");
let (typ, host, port, sel) = parse_url(urls[3]);
assert_eq!(typ, Type::Text);
assert_eq!(host, "gopher.floodgap.com");
assert_eq!(port, "70");
assert_eq!(sel, "/gopher/relevance.txt");
let (typ, host, port, sel) = parse_url(urls[4]);
assert_eq!(typ, Type::Search);
assert_eq!(host, "gopherpedia.com");
assert_eq!(port, "70");
assert_eq!(sel, "/lookup?Gopher");
let (typ, host, port, sel) = parse_url(urls[5]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "::1");
assert_eq!(port, "70");
assert_eq!(sel, "/");
let (typ, host, port, sel) = parse_url(urls[6]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "dead:beef:1234:5678:9012:3456:feed:deed");
assert_eq!(port, "70");
assert_eq!(sel, "/");
let (typ, host, port, sel) = parse_url(urls[7]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "1234:2345:dead:4567:7890:1234:beef:1111");
assert_eq!(port, "7443");
assert_eq!(sel, "/files");
let (typ, host, port, sel) = parse_url(urls[8]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "2001:cdba:0000:0000:0000:0000:3257:9652");
assert_eq!(port, "70");
assert_eq!(sel, "/");
let (typ, host, port, sel) = parse_url(urls[9]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "2001:cdba::3257:9652");
assert_eq!(port, "70");
assert_eq!(sel, "/");
let (typ, host, port, sel) = parse_url(urls[10]);
assert_eq!(typ, Type::Menu);
assert_eq!(host, "2001:cdba::3257:9652");
assert_eq!(port, "71");
assert_eq!(sel, "/");
}
}