diff options
author | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2022-02-06 10:12:19 +0000 |
---|---|---|
committer | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2022-02-06 10:12:19 +0000 |
commit | 8e0930cc9f25e44a69f61763ac5ff38ba58bb3dd (patch) | |
tree | e9500f4d2180984fe7981205c44ac89048f1019c | |
parent | Merge pull request #725 from aliemjay/update_actix_web (diff) | |
parent | Apply alimjays suggestion (diff) | |
download | miniserve-8e0930cc9f25e44a69f61763ac5ff38ba58bb3dd.tar.gz miniserve-8e0930cc9f25e44a69f61763ac5ff38ba58bb3dd.zip |
Merge 'jikstra/feat_route_prefix' #682
-rw-r--r-- | src/args.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 14 | ||||
-rw-r--r-- | src/listing.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | src/renderer.rs | 7 | ||||
-rw-r--r-- | tests/serve_request.rs | 15 |
6 files changed, 34 insertions, 23 deletions
diff --git a/src/args.rs b/src/args.rs index 7a6e41e..77925a2 100644 --- a/src/args.rs +++ b/src/args.rs @@ -60,8 +60,12 @@ pub struct CliArgs { )] pub auth: Vec<auth::RequiredAuth>, + /// Use a specific route prefix + #[clap(long = "route-prefix")] + pub route_prefix: Option<String>, + /// Generate a random 6-hexdigit route - #[clap(long = "random-route")] + #[clap(long = "random-route", conflicts_with("route-prefix"))] pub random_route: bool, /// Do not follow symbolic links diff --git a/src/config.rs b/src/config.rs index ccff7e3..ae260af 100644 --- a/src/config.rs +++ b/src/config.rs @@ -47,8 +47,8 @@ pub struct MiniserveConfig { /// Show hidden files pub show_hidden: bool, - /// Enable random route generation - pub random_route: Option<String>, + /// Route prefix + pub route_prefix: String, /// Randomly generated favicon route pub favicon_route: String, @@ -131,10 +131,10 @@ impl MiniserveConfig { ] }; - let random_route = if args.random_route { - Some(nanoid::nanoid!(6, &ROUTE_ALPHABET)) - } else { - None + let route_prefix = match (args.route_prefix, args.random_route) { + (Some(prefix), _) => format!("/{}", prefix.trim_matches('/')), + (_, true) => format!("/{}", nanoid::nanoid!(6, &ROUTE_ALPHABET)), + _ => "".to_owned(), }; // Generate some random routes for the favicon and css so that they are very unlikely to conflict with @@ -197,7 +197,7 @@ impl MiniserveConfig { path_explicitly_chosen, no_symlinks: args.no_symlinks, show_hidden: args.hidden, - random_route, + route_prefix, favicon_route, css_route, default_color_scheme, diff --git a/src/listing.rs b/src/listing.rs index 9e02598..05e800c 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -161,7 +161,7 @@ pub fn directory_listing( let serve_path = req.path(); let base = Path::new(serve_path); - let random_route_abs = format!("/{}", conf.random_route.clone().unwrap_or_default()); + let random_route_abs = format!("/{}", conf.route_prefix); let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs); let encoded_dir = match base.strip_prefix(random_route_abs) { @@ -180,10 +180,8 @@ pub fn directory_listing( let decoded = percent_decode_str(&encoded_dir).decode_utf8_lossy(); let mut res: Vec<Breadcrumb> = Vec::new(); - let mut link_accumulator = match &conf.random_route { - Some(random_route) => format!("/{}/", random_route), - None => "/".to_string(), - }; + + let mut link_accumulator = format!("{}/", &conf.route_prefix); let mut components = Path::new(&*decoded).components().peekable(); diff --git a/src/main.rs b/src/main.rs index 7f1944f..732abac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,10 +165,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { Some(_) => format!("https://{}", addr), None => format!("http://{}", addr), }) - .map(|url| match miniserve_config.random_route { - Some(ref random_route) => format!("{}/{}", url, random_route), - None => url, - }) + .map(|url| format!("{}{}", url, miniserve_config.route_prefix)) .collect::<Vec<_>>() }; @@ -195,7 +192,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { ) .route(&format!("/{}", inside_config.css_route), web::get().to(css)) .service( - web::scope(inside_config.random_route.as_deref().unwrap_or("")) + web::scope(&inside_config.route_prefix) .wrap(middleware::Condition::new( !inside_config.auth.is_empty(), actix_web::middleware::Compat::new(HttpAuthentication::basic( diff --git a/src/renderer.rs b/src/renderer.rs index c2c4cb5..0ac5077 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -25,10 +25,7 @@ pub fn page( return raw(entries, is_root); } - let upload_route = match conf.random_route { - Some(ref random_route) => format!("/{}/upload", random_route), - None => "/upload".to_string(), - }; + let upload_route = format!("{}/upload", &conf.route_prefix); let (sort_method, sort_order) = (query_params.sort, query_params.order); let upload_action = build_upload_action(&upload_route, encoded_dir, sort_method, sort_order); @@ -578,7 +575,7 @@ pub fn render_error( p { (error) } } // WARN don't expose random route! - @if conf.random_route.is_none() { + @if conf.route_prefix.is_empty() { div.error-nav { a.error-back href=(return_address) { "Go back to file listing" diff --git a/tests/serve_request.rs b/tests/serve_request.rs index ec2c978..1491be9 100644 --- a/tests/serve_request.rs +++ b/tests/serve_request.rs @@ -260,3 +260,18 @@ fn serve_index_instead_of_404_in_spa_mode( Ok(()) } + +#[rstest] +fn serves_requests_with_path_prefix( + #[with(["--route-prefix", "foobar"])] server: TestServer, +) -> Result<(), Error> { + let url_without_route = server.url(); + let status = reqwest::blocking::get(url_without_route)?.status(); + assert_eq!(status, StatusCode::NOT_FOUND); + + let url_with_route = format!("{}{}", server.url(), "foobar"); + let status = reqwest::blocking::get(url_with_route)?.status(); + assert_eq!(status, StatusCode::OK); + + Ok(()) +} |