aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorboastful-squirrel <boastful.squirrel@gmail.com>2019-11-21 22:57:49 +0000
committerboastful-squirrel <boastful.squirrel@gmail.com>2019-11-21 22:57:49 +0000
commit1bc09afa2945f6eadd74193802abe4d4f4e4fe75 (patch)
treeee6fb7967925a225489d37979484fc779964400b /src
parentFix html elements order (diff)
parentMerge pull request #217 from svenstaro/dependabot/cargo/port_check-0.1.2 (diff)
downloadminiserve-1bc09afa2945f6eadd74193802abe4d4f4e4fe75.tar.gz
miniserve-1bc09afa2945f6eadd74193802abe4d4f4e4fe75.zip
Merge branch 'master' into fix-#173
Diffstat (limited to '')
-rw-r--r--src/args.rs19
-rw-r--r--src/auth.rs32
-rw-r--r--src/main.rs20
3 files changed, 37 insertions, 34 deletions
diff --git a/src/args.rs b/src/args.rs
index 925d3dd..8fde78a 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -14,7 +14,9 @@ const ROUTE_ALPHABET: [char; 16] = [
#[derive(StructOpt)]
#[structopt(
name = "miniserve",
- raw(global_settings = "&[structopt::clap::AppSettings::ColoredHelp]")
+ author,
+ about,
+ global_settings = &[structopt::clap::AppSettings::ColoredHelp],
)]
struct CLIArgs {
/// Be verbose, includes emitting access logs
@@ -33,8 +35,8 @@ struct CLIArgs {
#[structopt(
short = "i",
long = "interfaces",
- parse(try_from_str = "parse_interface"),
- raw(number_of_values = "1")
+ parse(try_from_str = parse_interface),
+ number_of_values = 1,
)]
interfaces: Vec<IpAddr>,
@@ -44,8 +46,8 @@ struct CLIArgs {
#[structopt(
short = "a",
long = "auth",
- parse(try_from_str = "parse_auth"),
- raw(number_of_values = "1")
+ parse(try_from_str = parse_auth),
+ number_of_values = 1,
)]
auth: Vec<auth::RequiredAuth>,
@@ -62,10 +64,8 @@ struct CLIArgs {
short = "c",
long = "color-scheme",
default_value = "Squirrel",
- raw(
- possible_values = "&themes::ColorScheme::variants()",
- case_insensitive = "true",
- )
+ possible_values = &themes::ColorScheme::variants(),
+ case_insensitive = true,
)]
color_scheme: themes::ColorScheme,
@@ -172,6 +172,7 @@ pub fn parse_args() -> crate::MiniserveConfig {
mod tests {
use super::*;
use rstest::rstest_parametrize;
+ use pretty_assertions::assert_eq;
/// Helper function that creates a `RequiredAuth` structure
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth {
diff --git a/src/auth.rs b/src/auth.rs
index 2c98622..1c98c10 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -55,19 +55,17 @@ pub fn parse_basic_auth(
/// Return `true` if `basic_auth` is matches any of `required_auth`
pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &[RequiredAuth]) -> bool {
- required_auth.iter().any(
- |RequiredAuth { username, password }|
- basic_auth.username == *username &&
- compare_password(&basic_auth.password, password)
- )
+ required_auth
+ .iter()
+ .any(|RequiredAuth { username, password }| {
+ basic_auth.username == *username && compare_password(&basic_auth.password, password)
+ })
}
/// Return `true` if `basic_auth_pwd` meets `required_auth_pwd`'s requirement
-pub fn compare_password (basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool {
+pub fn compare_password(basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool {
match &required_auth_pwd {
- RequiredAuthPassword::Plain(required_password) => {
- *basic_auth_pwd == *required_password
- }
+ RequiredAuthPassword::Plain(required_password) => *basic_auth_pwd == *required_password,
RequiredAuthPassword::Sha256(password_hash) => {
compare_hash::<Sha256>(basic_auth_pwd, password_hash)
}
@@ -106,16 +104,9 @@ impl Middleware<crate::MiniserveConfig> for Auth {
Ok(auth_req) => auth_req,
Err(err) => {
let auth_err = ContextualError::HTTPAuthenticationError(Box::new(err));
- return Ok(Response::Done(
- HttpResponse::BadRequest().body(
- build_unauthorized_response(
- &req,
- auth_err,
- true,
- StatusCode::BAD_REQUEST,
- ),
- ),
- ));
+ return Ok(Response::Done(HttpResponse::BadRequest().body(
+ build_unauthorized_response(&req, auth_err, true, StatusCode::BAD_REQUEST),
+ )));
}
};
@@ -135,7 +126,7 @@ impl Middleware<crate::MiniserveConfig> for Auth {
ContextualError::InvalidHTTPCredentials,
true,
StatusCode::UNAUTHORIZED,
- ))
+ )),
))
}
}
@@ -178,6 +169,7 @@ fn build_unauthorized_response(
mod tests {
use super::*;
use rstest::{rstest, rstest_parametrize, fixture};
+ use pretty_assertions::assert_eq;
/// Return a hashing function corresponds to given name
fn get_hash_func(name: &str) -> impl FnOnce(&str) -> Vec<u8> {
diff --git a/src/main.rs b/src/main.rs
index 39dd9c0..2482d95 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,12 +2,11 @@
use actix_web::http::{Method, StatusCode};
use actix_web::{fs, middleware, server, App, HttpRequest, HttpResponse};
-use clap::crate_version;
-use simplelog::{Config, LevelFilter, TermLogger, TerminalMode};
use std::io::{self, Write};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::thread;
use std::time::Duration;
+use structopt::clap::crate_version;
use yansi::{Color, Paint};
mod archive;
@@ -74,12 +73,23 @@ fn run() -> Result<(), ContextualError> {
let sys = actix::System::new("miniserve");
let miniserve_config = args::parse_args();
- let _ = if miniserve_config.verbose {
- TermLogger::init(LevelFilter::Info, Config::default(), TerminalMode::default())
+ let log_level = if miniserve_config.verbose {
+ simplelog::LevelFilter::Info
} else {
- TermLogger::init(LevelFilter::Error, Config::default(), TerminalMode::default())
+ simplelog::LevelFilter::Error
};
+ if simplelog::TermLogger::init(
+ log_level,
+ simplelog::Config::default(),
+ simplelog::TerminalMode::Mixed,
+ )
+ .is_err()
+ {
+ simplelog::SimpleLogger::init(log_level, simplelog::Config::default())
+ .expect("Couldn't initialize logger")
+ }
+
if miniserve_config.no_symlinks {
let is_symlink = miniserve_config
.path