|
|
|
@ -1,4 +1,3 @@
|
|
|
|
|
use std::error::Error;
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
|
use std::net::TcpStream;
|
|
|
|
@ -7,9 +6,9 @@ use types::Type;
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Link {
|
|
|
|
|
pos: usize, // which link in the page
|
|
|
|
|
title: String,
|
|
|
|
|
name: String,
|
|
|
|
|
host: String,
|
|
|
|
|
port: usize,
|
|
|
|
|
port: String,
|
|
|
|
|
selector: String,
|
|
|
|
|
typ: Type,
|
|
|
|
|
}
|
|
|
|
@ -26,25 +25,14 @@ pub struct Page {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Page {
|
|
|
|
|
fn new() -> Page {
|
|
|
|
|
Page {
|
|
|
|
|
raw: String::new(),
|
|
|
|
|
url: String::new(),
|
|
|
|
|
links: vec![],
|
|
|
|
|
link: 0,
|
|
|
|
|
typ: Type::Menu,
|
|
|
|
|
input: String::new(),
|
|
|
|
|
offset: 0,
|
|
|
|
|
}
|
|
|
|
|
pub fn from(url: String, gopher_response: String) -> Page {
|
|
|
|
|
Self::parse(url, gopher_response)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Loads a Page given a URL.
|
|
|
|
|
pub fn load(host: &str, port: &str, selector: &str) -> io::Result<Page> {
|
|
|
|
|
let mut page = Self::new();
|
|
|
|
|
match Self::fetch(host, port, selector) {
|
|
|
|
|
Ok(res) => {
|
|
|
|
|
page.raw = res;
|
|
|
|
|
Ok(page)
|
|
|
|
|
}
|
|
|
|
|
Ok(res) => Ok(Page::from(format!("{}:{}{}", host, port, selector), res)),
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -67,4 +55,69 @@ impl Page {
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parses the links in a raw Gopher response;
|
|
|
|
|
fn parse(url: String, raw: String) -> Page {
|
|
|
|
|
let mut links = vec![];
|
|
|
|
|
let mut start = true;
|
|
|
|
|
let mut is_link = false;
|
|
|
|
|
let mut link = (0, 0, Type::Menu);
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
|
|
|
|
|
for (i, c) in raw.char_indices() {
|
|
|
|
|
if start {
|
|
|
|
|
match c {
|
|
|
|
|
'0' => {
|
|
|
|
|
is_link = true;
|
|
|
|
|
link.0 = i + 1;
|
|
|
|
|
link.2 = Type::Text;
|
|
|
|
|
}
|
|
|
|
|
'1' => {
|
|
|
|
|
is_link = true;
|
|
|
|
|
link.0 = i + 1;
|
|
|
|
|
link.2 = Type::Menu;
|
|
|
|
|
}
|
|
|
|
|
'h' => {
|
|
|
|
|
is_link = true;
|
|
|
|
|
link.0 = i + 1;
|
|
|
|
|
link.2 = Type::HTML;
|
|
|
|
|
}
|
|
|
|
|
'\n' => continue,
|
|
|
|
|
_ => is_link = false,
|
|
|
|
|
}
|
|
|
|
|
start = false;
|
|
|
|
|
} else if c == '\n' {
|
|
|
|
|
start = true;
|
|
|
|
|
if is_link && i > link.0 {
|
|
|
|
|
link.1 = i;
|
|
|
|
|
let mut line = [""; 4];
|
|
|
|
|
for (j, s) in raw[link.0..link.1].split('\t').enumerate() {
|
|
|
|
|
if j < line.len() {
|
|
|
|
|
line[j] = s;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
links.push(Link {
|
|
|
|
|
name: line[0].to_string(),
|
|
|
|
|
selector: line[1].to_string(),
|
|
|
|
|
host: line[2].to_string(),
|
|
|
|
|
port: line[3].trim_end_matches('\r').to_string(),
|
|
|
|
|
typ: link.2,
|
|
|
|
|
pos: count,
|
|
|
|
|
});
|
|
|
|
|
count += 1;
|
|
|
|
|
is_link = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Page {
|
|
|
|
|
raw,
|
|
|
|
|
url,
|
|
|
|
|
links,
|
|
|
|
|
link: 0,
|
|
|
|
|
typ: Type::Menu,
|
|
|
|
|
input: String::new(),
|
|
|
|
|
offset: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|