From 6a118ad66a63be15b8c9ab378d35ae482ebf0144 Mon Sep 17 00:00:00 2001 From: int 80h Date: Tue, 28 Apr 2020 17:54:50 -0400 Subject: [PATCH] Added command line to find config --- src/config.rs | 6 ++++-- src/main.rs | 19 +++++++++++++------ src/status.rs | 10 ---------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/config.rs b/src/config.rs index b018aa8..53afdde 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,8 @@ extern crate serde_derive; extern crate toml; use std::collections::HashMap; use toml::de::Error; +use std::io; +use std::path::Path; #[derive(Debug, Deserialize, Clone)] pub struct Config { @@ -20,10 +22,10 @@ pub struct Server { } impl Config { - pub fn new(file: &str) -> Config { + pub fn new(file: &Path) -> Result { let fd = std::fs::read_to_string(file).unwrap(); let config: Config = toml::from_str(&fd).unwrap(); - return config; + return Ok(config); } pub fn to_map(&self) -> HashMap { let mut map = HashMap::new(); diff --git a/src/main.rs b/src/main.rs index 9851700..9f06ebb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::Arc; +use std::env; use tokio::io::AsyncWriteExt; use tokio::net::TcpListener; use tokio::net::TcpStream; @@ -112,11 +113,6 @@ async fn handle_connection(mut con: conn::Connection) -> Result<(), io::Error> { return Ok(()); } - if url.path().to_string().contains("..") { - con.send_status(status::Status::PermanentFailure, "Not in path!").await?; - return Ok(()); - } - let mut path = PathBuf::from(&con.dir); if url.path() != "" || url.path() != "/" { path.push(url.path().trim_start_matches("/")); @@ -167,7 +163,17 @@ async fn handle_connection(mut con: conn::Connection) -> Result<(), io::Error> { } fn main() -> io::Result<()> { - let cfg = config::Config::new("config.toml"); + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("Please run with the path to the config file."); + return Ok(()); + } + let p = Path::new(&args[1]); + if !p.exists() { + println!("Config file doesn't exist"); + return Ok(()); + } + let cfg = config::Config::new(&p)?; println!("Serving {} vhosts", cfg.server.len()); let addr = format!("{}:{}", cfg.host, cfg.port); @@ -199,6 +205,7 @@ fn main() -> io::Result<()> { let mut dir = String::new(); let mut cgi = String::new(); let mut hostname = String::new(); + for server in &cfg.server { if Some(server.hostname.as_str()) == sni { hostname = sni.unwrap().to_string(); diff --git a/src/status.rs b/src/status.rs index 613f870..0f1e565 100644 --- a/src/status.rs +++ b/src/status.rs @@ -29,16 +29,6 @@ pub enum Status { impl fmt::Display for Status { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) - // or, alternatively: - // fmt::Debug::fmt(self, f) } } -impl Status { - pub fn as_str(&self) -> &'static str { - match *self { - Status::Success => "20\t", - _ => "", - } - } -}