Fix directory listing (now displays only filename without full path)
This commit is contained in:
parent
d500319749
commit
ba6237d822
86
src/main.rs
86
src/main.rs
|
@ -1,5 +1,4 @@
|
|||
use std::{fs, io};
|
||||
use std::borrow::Cow;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::io::prelude::*;
|
||||
use std::net::TcpListener;
|
||||
|
@ -48,21 +47,6 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
println!("{}", String::from_utf8_lossy(&buffer[..]));
|
||||
|
||||
/*
|
||||
let hostname = "localhost";
|
||||
let path = "/";
|
||||
let _post = "\r\n";
|
||||
|
||||
|
||||
let get = format!(
|
||||
"{} {} {} {}",
|
||||
hostname,
|
||||
path,
|
||||
_post.len(),
|
||||
_post
|
||||
);*/
|
||||
|
||||
// let _echo = b"$hostname /echo 0\r\n"; // TODO: fix echo behaviour
|
||||
|
||||
let request = String::from_utf8_lossy(&buffer[..]);
|
||||
|
@ -81,7 +65,7 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
}*/
|
||||
|
||||
let get = format!( "{} {} {}\r\n {}\r\n", host, _path, _post_len, _post);
|
||||
let get_bytes = get.as_bytes();
|
||||
let _get_bytes = get.as_bytes();
|
||||
|
||||
let status_line = if Path::new(&format!("{}{}", "/home/g1n/public_spartan", _path).to_string()).exists() {
|
||||
"2 text/gemini"
|
||||
|
@ -106,59 +90,23 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
if exists {
|
||||
_contents = fs::read_to_string(format!("{}{}index.gmi", "/home/g1n/public_spartan", _path)).unwrap();
|
||||
} else {
|
||||
/*
|
||||
let mut entries = fs::read_dir(full_path);
|
||||
let mut lines = vec![];
|
||||
println!("{:?}", entries);
|
||||
for entry in entries {
|
||||
let mut name = entry
|
||||
.file_name()
|
||||
.into_string()
|
||||
.or(Err("Non-Unicode filename"));
|
||||
//entry.to_string().clone();
|
||||
/*.into_string()
|
||||
.or(Err("Non-Unicode filename"));*/
|
||||
if name.starts_with('.') {
|
||||
continue;
|
||||
}
|
||||
if Path::new(&entry).is_dir() {
|
||||
name += "/";
|
||||
}
|
||||
/*let line = match /*percent_encode(*/name.as_bytes().into() {
|
||||
Cow::Owned(url) => format!("=> {:?} {}\n", url, name),
|
||||
Cow::Borrowed(url) => format!("=> {:?}\n", url), // url and name are identical
|
||||
};*/
|
||||
let line = format!("=> {} {}\n", name, name);
|
||||
println!("{}", line);
|
||||
lines.push(line);
|
||||
}
|
||||
lines.sort();
|
||||
for line in lines {
|
||||
_contents += &format!("{}", line).to_string();
|
||||
println!("{}" , line.to_string());
|
||||
}*/
|
||||
/*
|
||||
let listing = fs::read_dir(full_path).unwrap();
|
||||
for file in listing {
|
||||
println!("=> {} {}\n", file.unwrap().path().display(), "name_of_file");
|
||||
}
|
||||
*/
|
||||
//println!("{:?}", directory_listing());
|
||||
_contents = format!("=> ..\n");
|
||||
let files = fs::read_dir(full_path).unwrap();
|
||||
let path_to_folder = full_path.clone();
|
||||
let files = fs::read_dir(path_to_folder).unwrap();
|
||||
for file in files {
|
||||
let path_to_folder = full_path.clone();
|
||||
let path = file.unwrap().path();
|
||||
let file = path.display();
|
||||
let file = path.strip_prefix(path_to_folder);
|
||||
let file = file.unwrap().display();
|
||||
if path.is_dir() {
|
||||
_contents += &format!("=> {}/ {}\n", file, file).to_string();
|
||||
_contents += &format!("=> {}/\n", file).to_string();
|
||||
} else {
|
||||
_contents += &format!("=> {} {}\n", file, file).to_string();
|
||||
_contents += &format!("=> {}\n", file).to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: add listing for folders
|
||||
|
||||
let response = format!(
|
||||
"{}\r\n {}",
|
||||
|
@ -169,18 +117,4 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
stream.write(response.as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
|
||||
}/*
|
||||
fn directory_listing() -> io::Result<()> {
|
||||
let mut entries = fs::read_dir(".")?
|
||||
.map(|res| res.map(|e| e.path()))
|
||||
.collect::<Result<Vec<_>, io::Error>>()?;
|
||||
|
||||
// The order in which `read_dir` returns entries is not guaranteed. If reproducible
|
||||
// // ordering is required the entries should be explicitly sorted.
|
||||
|
||||
entries.sort();
|
||||
|
||||
println!("{:?}", entries);
|
||||
// The entries have now been sorted by their path.
|
||||
Ok(())
|
||||
}*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue