Skip to content
Snippets Groups Projects
Unverified Commit 6f248648 authored by LinFeng Qian's avatar LinFeng Qian Committed by GitHub
Browse files

Merge pull request #39 from robatipoor/master

check format input auth is valid
parents a95543a8 7c23939f
No related branches found
No related tags found
No related merge requests found
......@@ -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() {
......
......@@ -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()))
}
}
}
......
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()
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment