made input's more safe
This commit is contained in:
parent
88200eb354
commit
2aa4a82af9
226
src/main.rs
226
src/main.rs
@ -3,6 +3,13 @@ use std::io::{stdin,stdout,Write};
|
||||
use colored::Colorize;
|
||||
use regex::Regex;
|
||||
|
||||
struct Url {
|
||||
protocol: String,
|
||||
hostname: String,
|
||||
port: u16,
|
||||
path: String,
|
||||
}
|
||||
|
||||
fn clear_screen() {
|
||||
Command::new("clear")
|
||||
.status()
|
||||
@ -132,7 +139,7 @@ fn parse_markdown(page_content: String) -> (String, Vec<String>) {
|
||||
return (parsed_page_content, links);
|
||||
}
|
||||
|
||||
fn fetch_page(host: &String, port: &String, path: &String) -> String {
|
||||
fn fetch_page(host: &String, port: u16, path: &String) -> String {
|
||||
let full_url_formatted = format!("{}:{}/{}", host, port, path);
|
||||
|
||||
// Call curl using Com, mand
|
||||
@ -152,9 +159,9 @@ fn fetch_page(host: &String, port: &String, path: &String) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn render_page(host: String, port: String, path: String) -> Vec<String> {
|
||||
fn render_page(host: String, port: u16, path: String) -> Vec<String> {
|
||||
clear_screen();
|
||||
let mut content = fetch_page(&host, &port, &path);
|
||||
let mut content = fetch_page(&host, port, &path);
|
||||
let mut links = Vec::new();
|
||||
let (screen_width, screen_height) = termion::terminal_size().unwrap();
|
||||
|
||||
@ -190,125 +197,142 @@ fn input() -> String{
|
||||
return s;
|
||||
}
|
||||
|
||||
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();
|
||||
fn parse_url(user_input: String, previous_host: &String) -> Result<Url, u8> {
|
||||
let mut url = Url {
|
||||
protocol: "internal".to_string(),
|
||||
hostname: "home".to_string(),
|
||||
port: 0,
|
||||
path: "/".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 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();
|
||||
if let Some(caps) = mttp_regex.captures(&user_input) {
|
||||
url.hostname = caps[1].to_string();
|
||||
url.port = 3477;
|
||||
url.path = caps[2].to_string();
|
||||
url.protocol = "mttp".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
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) = mttp_regex_no_path.captures(&user_input) {
|
||||
url.hostname = caps[1].to_string();
|
||||
url.port = 3477;
|
||||
url.path = "/".to_string();
|
||||
url.protocol = "mttp".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
else if let Some(caps) = path_change.captures(&url) {
|
||||
host = previous_host.to_string();
|
||||
port = "3477".to_string();
|
||||
path = caps[1].to_string();
|
||||
protocol = "mttp".to_string();
|
||||
else if let Some(caps) = path_change.captures(&user_input) {
|
||||
url.hostname = previous_host.to_string();
|
||||
url.port = 3477;
|
||||
url.path = caps[1].to_string();
|
||||
url.protocol = "mttp".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
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) = no_protocol_but_path.captures(&user_input) {
|
||||
url.hostname = caps[1].to_string();
|
||||
url.port = 3477;
|
||||
url.path = caps[2].to_string();
|
||||
url.protocol = "mttp".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
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();
|
||||
else if let Some(caps) = http_regex.captures(&user_input) {
|
||||
url.hostname = caps[1].to_string();
|
||||
url.port = 80;
|
||||
url.path = caps[2].to_string();
|
||||
url.protocol = "http".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
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(&user_input) {
|
||||
url.hostname = caps[1].to_string();
|
||||
url.port = 80;
|
||||
url.path = "/".to_string();
|
||||
url.protocol = "http".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
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();
|
||||
else if let Some(caps) = accept_this_as_mttp.captures(&user_input) {
|
||||
url.hostname = format!("{}{}{}",caps[1].to_string(),".",caps[2].to_string());
|
||||
url.port = 3477;
|
||||
url.path = "/".to_string();
|
||||
url.protocol = "mttp".to_string();
|
||||
Ok(url)
|
||||
}
|
||||
else {
|
||||
Err(1)
|
||||
}
|
||||
println!("{}://{}:{}/{}",protocol,host,port,path);
|
||||
|
||||
return (host, port, path, protocol);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
clear_screen();
|
||||
println!("Enter a url: ");
|
||||
let url = input();
|
||||
|
||||
if url == "q" {
|
||||
std::process::exit(0);
|
||||
}
|
||||
clear_screen();
|
||||
println!("Enter a url: ");
|
||||
let user_input = input();
|
||||
|
||||
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;
|
||||
let mut links: Vec<String> = Vec::new();
|
||||
if user_input == "q" {
|
||||
std::process::exit(0);
|
||||
}
|
||||
let mut load_page: bool = true;
|
||||
let mut history: Vec<Url> = Vec::new();
|
||||
let mut links: Vec<String> = Vec::new();
|
||||
|
||||
'mainloop: loop {
|
||||
if load_page {
|
||||
links = Vec::new();
|
||||
links = render_page(host.clone(), port.clone(), path.clone());
|
||||
if let Ok(url) = parse_url(user_input, &"example.com".to_string()) {
|
||||
'mainloop: loop {
|
||||
if load_page {
|
||||
history.push(Url {
|
||||
protocol: url.protocol.clone(),
|
||||
hostname: url.hostname.clone(),
|
||||
port: url.port.clone(),
|
||||
path: url.path.clone(),
|
||||
});
|
||||
links = Vec::new();
|
||||
links = render_page(url.hostname.clone(), url.port.clone(), url.path.clone());
|
||||
|
||||
println!("Enter reference number to follow, h for help, or q to quit ");
|
||||
}
|
||||
load_page = true;
|
||||
println!("Enter reference number to follow, h for help, or q to quit ");
|
||||
}
|
||||
load_page = false;
|
||||
|
||||
let link_to_follow = input();
|
||||
if link_to_follow == "q" {
|
||||
break 'mainloop;
|
||||
}
|
||||
else if link_to_follow == "r" {
|
||||
continue;
|
||||
}
|
||||
else if link_to_follow == "h" {
|
||||
println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24
|
||||
");
|
||||
load_page = false;
|
||||
}
|
||||
else if link_to_follow.chars().nth(0).unwrap() == 'o' {
|
||||
let number_str = &link_to_follow[1..];
|
||||
if let Ok(number) = number_str.parse::<usize>() {
|
||||
println!("{}", links[number]);
|
||||
} else {
|
||||
println!("error");
|
||||
}
|
||||
load_page = false;
|
||||
}
|
||||
else {
|
||||
if let Ok(number) = link_to_follow.parse::<usize>() {
|
||||
if number < links.len() {
|
||||
(host, port, path, protocol) = parse_url(links[number].clone(), &host);
|
||||
} else {
|
||||
println!("Invalid reference id");
|
||||
load_page = false;
|
||||
}
|
||||
} else {
|
||||
println!("Invalid input");
|
||||
load_page = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
let user_input = input();
|
||||
if user_input == "q" {
|
||||
break 'mainloop;
|
||||
}
|
||||
else if user_input == "r" {
|
||||
load_page = true;
|
||||
continue;
|
||||
}
|
||||
else if user_input == "h" {
|
||||
println!("Source code: https://git.javalsai.dynv6.net/deadvey/markdown-webbrowser\nq: quit\nh: help\nr: reload\ni: visit root index of this host eg: root index of mttp://deadvey.com/blog/4.md is just deadvey.com\nb: go back in history\nox: print the hyprlink of reference x eg: o5 or o24
|
||||
");
|
||||
}
|
||||
else if user_input.chars().nth(0).unwrap() == 'o' {
|
||||
let number_str = &user_input[1..];
|
||||
if let Ok(number) = number_str.parse::<usize>() {
|
||||
println!("{}", links[number]);
|
||||
} else {
|
||||
println!("error");
|
||||
}
|
||||
}
|
||||
else if let Ok(number) = user_input.parse::<usize>() {
|
||||
if number < links.len() {
|
||||
let url = parse_url(links[number].clone(), &url.hostname).expect("Error parsing URL");
|
||||
load_page = true;
|
||||
} else {
|
||||
println!("Invalid reference id");
|
||||
}
|
||||
}
|
||||
else if let Ok(url) = parse_url(user_input, &url.hostname) {
|
||||
load_page = true;
|
||||
}
|
||||
else {
|
||||
println!("Invalid input");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
println!("Invalid url");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user