Dir list works but needs cleaned up
This commit is contained in:
53
src/main.rs
53
src/main.rs
@@ -5,12 +5,12 @@ extern crate serde_derive;
|
|||||||
use futures_util::future::TryFutureExt;
|
use futures_util::future::TryFutureExt;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
use std::io::{self, BufReader};
|
use std::io::{self, BufReader};
|
||||||
use std::net::ToSocketAddrs;
|
use std::net::ToSocketAddrs;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::AsyncWriteExt;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
@@ -35,29 +35,13 @@ fn get_tls_config(cfg: config::Config) -> rustls::ServerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_content(mut path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
fn get_content(mut path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
||||||
println!("URL: {}", u);
|
|
||||||
if u.path() == "" || u.path() == "/" {
|
|
||||||
path.push("index.gemini");
|
|
||||||
} else {
|
|
||||||
path.push(u.path().trim_start_matches("/"));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !u.path().ends_with("/") {
|
|
||||||
u.path().to_string().push('/');
|
|
||||||
}
|
|
||||||
println!("{}", path.to_str().unwrap());
|
|
||||||
|
|
||||||
if !path.exists() {
|
|
||||||
return Ok("51 Not found!\r\n".to_string())
|
|
||||||
}
|
|
||||||
let meta = fs::metadata(&path).expect("Unable to read metadata");
|
let meta = fs::metadata(&path).expect("Unable to read metadata");
|
||||||
if meta.is_file() {
|
if meta.is_file() {
|
||||||
return Ok(std::fs::read_to_string(path).expect("Unable to read file"));
|
return Ok(std::fs::read_to_string(path).expect("Unable to read file"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut list = String::from("# Directory Listing\n\n");
|
let mut list = String::from("# Directory Listing\n\n");
|
||||||
if !u.path().ends_with("/") {
|
// needs work
|
||||||
path.push(format!("{}/", u.path().trim_start_matches("/")));
|
|
||||||
}
|
|
||||||
for file in fs::read_dir(path)? {
|
for file in fs::read_dir(path)? {
|
||||||
if let Ok(file) = file {
|
if let Ok(file) = file {
|
||||||
let f = file.file_name().to_str().unwrap().to_owned();
|
let f = file.file_name().to_str().unwrap().to_owned();
|
||||||
@@ -73,13 +57,11 @@ async fn handle_connection(
|
|||||||
mut stream: TlsStream<TcpStream>,
|
mut stream: TlsStream<TcpStream>,
|
||||||
cfg: config::Config,
|
cfg: config::Config,
|
||||||
) -> Result<(), io::Error> {
|
) -> Result<(), io::Error> {
|
||||||
|
|
||||||
let mut buffer = [0; 512];
|
let mut buffer = [0; 512];
|
||||||
stream.read(&mut buffer).await?;
|
stream.read(&mut buffer).await?;
|
||||||
let request = String::from_utf8_lossy(&buffer[..]).to_owned();
|
let request = String::from_utf8_lossy(&buffer[..]).to_owned();
|
||||||
println!("Request: {}", request);
|
println!("Request: {}", request);
|
||||||
|
|
||||||
|
|
||||||
let url = Url::parse(&request).unwrap();
|
let url = Url::parse(&request).unwrap();
|
||||||
|
|
||||||
if url.scheme() != "gemini" {
|
if url.scheme() != "gemini" {
|
||||||
@@ -101,12 +83,35 @@ async fn handle_connection(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let p = PathBuf::from(dir);
|
let mut path = PathBuf::from(dir);
|
||||||
|
if url.path() != "" || url.path() != "/" {
|
||||||
|
path.push(url.path().trim_start_matches("/"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !path.exists() {
|
||||||
|
stream.write_all(&b"51\tNot found!\r\n"[..]).await?;
|
||||||
|
stream.flush().await?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// add error
|
||||||
|
let meta = fs::metadata(&path).expect("Unable to read metadata");
|
||||||
|
if meta.is_dir() {
|
||||||
|
if !url.path().ends_with("/") {
|
||||||
|
stream
|
||||||
|
.write_all(format!("31\t{}/\r\n", url).as_bytes())
|
||||||
|
.await?;
|
||||||
|
stream.flush().await?;
|
||||||
|
}
|
||||||
|
if path.join("index.gemini").exists() {
|
||||||
|
path.push("index.gemini");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stream.write_all(&b"20\ttext/gemini\r\n"[..]).await?;
|
stream.write_all(&b"20\ttext/gemini\r\n"[..]).await?;
|
||||||
stream.flush().await?;
|
stream.flush().await?;
|
||||||
|
|
||||||
let content = get_content(p, url)?;
|
let content = get_content(path, url)?;
|
||||||
stream.write_all(content.as_bytes()).await?;
|
stream.write_all(content.as_bytes()).await?;
|
||||||
stream.flush().await?;
|
stream.flush().await?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user