diff options
Diffstat (limited to '')
-rw-r--r-- | src/args.rs | 6 | ||||
-rw-r--r-- | src/config.rs | 15 | ||||
-rw-r--r-- | src/listing.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/renderer.rs | 7 | ||||
-rw-r--r-- | tests/serve_request.rs | 15 |
6 files changed, 40 insertions, 22 deletions
diff --git a/src/args.rs b/src/args.rs index 88ad8fc..b733769 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 fda2f84..4b9d2dc 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,11 @@ 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), _) if prefix.starts_with('/') => prefix, + (Some(prefix), _) => format!("/{}", prefix), + (_, 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 @@ -185,7 +186,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 9273025..4360c87 100644 --- a/src/listing.rs +++ b/src/listing.rs @@ -162,7 +162,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) { @@ -181,10 +181,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 d5cac00..3cbed1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,9 +165,12 @@ 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| { + if !miniserve_config.route_prefix.is_empty() { + format!("{}{}", url, miniserve_config.route_prefix) + } else { + url + } }) .collect::<Vec<_>>() }; @@ -195,7 +198,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(), HttpAuthentication::basic(auth::handle_auth), 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(()) +} |