diff --git a/src/main.rs b/src/main.rs index 546261a0bb8e01f0c499e569c77d878bd717c345..184c9952077242cdb8513dea0e09b30fe91ff1f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -213,6 +213,7 @@ fn main() { let printer = Printer::new(); let color_blue = Some(build_spec(Some(Color::Blue), false)); + let color_red = Some(build_spec(Some(Color::Red), false)); let addr = format!("{}:{}", ip, port); let compression_exts = compress .clone() @@ -276,7 +277,15 @@ fn main() { chain.link_around(CorsMiddleware::with_allow_any()); } if let Some(auth) = auth { - chain.link_before(AuthChecker::new(auth)); + match AuthChecker::new(auth) { + Ok(auth_checker) => { + chain.link_before(auth_checker); + } + Err(e) => { + printer.print_err("{}", &[(&*e, &color_red)]).unwrap(); + return; + } + } } if let Some(ref exts) = compress { if !exts.is_empty() { diff --git a/src/middlewares/auth.rs b/src/middlewares/auth.rs index f977cb47cd9c0d52be736ef7b8738a290453eeef..04066e2d5b988a72ea044c101ee9b7ab16b8eb44 100644 --- a/src/middlewares/auth.rs +++ b/src/middlewares/auth.rs @@ -9,11 +9,15 @@ pub struct AuthChecker { } impl AuthChecker { - pub fn new(s: &str) -> AuthChecker { + pub fn new(s: &str) -> Result<AuthChecker, StringError> { let parts = s.splitn(2, ':').collect::<Vec<&str>>(); - AuthChecker { - username: parts[0].to_owned(), - password: parts[1].to_owned(), + if parts.len() == 2 { + Ok(AuthChecker { + username: parts[0].to_owned(), + password: parts[1].to_owned(), + }) + } else { + Err(StringError("not valid format user & password".to_owned())) } } } diff --git a/src/util.rs b/src/util.rs index cf6918c7ff2e54f516d66c9a6e6bc1beb1f37c05..53d3e9284eb8d426de9713a7c369e46248fd7ab8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,6 +1,7 @@ use std::error::Error; use std::fmt; use std::io; +use std::ops::Deref; use std::time::{SystemTime, UNIX_EPOCH}; use chrono::{DateTime, Local, TimeZone}; @@ -37,6 +38,14 @@ impl Error for StringError { } } +impl Deref for StringError { + type Target = str; + + fn deref(&self) -> &Self::Target { + &*self.0 + } +} + pub fn enable_string(value: bool) -> String { (if value { "enabled" } else { "disabled" }).to_owned() }