Path validation

This commit is contained in:
Matt Brubeck 2020-05-17 12:25:39 -07:00
parent 0e6163dcd9
commit 7eeba3959e

View file

@ -5,14 +5,12 @@ use {
task,
},
async_tls::TlsAcceptor,
rustls::{
internal::pemfile::{certs, rsa_private_keys},
},
rustls::internal::pemfile::{certs, rsa_private_keys},
std::{
error::Error,
fs::File,
io::BufReader,
path::Path,
path::{Path, PathBuf},
sync::Arc,
},
url::Url,
@ -59,6 +57,14 @@ async fn connection(acceptor: TlsAcceptor, stream: TcpStream) -> Result {
let url = Url::parse(request.trim())?;
eprintln!("Got request: {:?}", url);
let path: PathBuf = url.path_segments().unwrap().collect();
eprintln!("Path: {:?}", path);
let path = Path::new(".").join(path).canonicalize()?;
eprintln!("Path: {:?}", path);
// TODO: Return a not found error
assert!(path.starts_with(std::env::current_dir()?));
let mut stream = stream.into_inner();
stream.write_all(b"20 text/gemini\r\n").await?;
stream.write_all(b"=> ").await?;