diff options
author | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2021-08-30 06:06:11 +0000 |
---|---|---|
committer | Ali MJ Al-Nasrawy <alimjalnasrawy@gmail.com> | 2021-08-30 21:27:59 +0000 |
commit | 0b49d1a89ccf752ee2bedfb44f0a4b7fc85894a7 (patch) | |
tree | f7ffaf5c418b06bd392f39bec0328397d1c3c97b | |
parent | Fix clippy::too_many_arguments and rework error .. (diff) | |
download | miniserve-0b49d1a89ccf752ee2bedfb44f0a4b7fc85894a7.tar.gz miniserve-0b49d1a89ccf752ee2bedfb44f0a4b7fc85894a7.zip |
Exclude embedded routes from authentication
-rw-r--r-- | src/main.rs | 78 |
1 files changed, 30 insertions, 48 deletions
diff --git a/src/main.rs b/src/main.rs index f3ae50b..e5cc596 100644 --- a/src/main.rs +++ b/src/main.rs @@ -202,11 +202,6 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { App::new() .wrap(configure_header(&inside_config.clone())) .app_data(inside_config.clone()) - // we should use `actix_web_httpauth::middleware::HttpAuthentication` - // but it is unfortuantrly broken - // see: https://github.com/actix/actix-extras/issues/127 - // TODO replace this when fixed upstream - .wrap_fn(auth::auth_middleware) .wrap_fn(errors::error_page_middleware) .wrap(middleware::Logger::default()) .route( @@ -214,7 +209,15 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> { web::get().to(favicon), ) .route(&format!("/{}", inside_config.css_route), web::get().to(css)) - .configure(|c| configure_app(c, &inside_config)) + .service( + web::scope(inside_config.random_route.as_deref().unwrap_or("")) + // we should use `actix_web_httpauth::middleware::HttpAuthentication` + // but it is unfortuantrly broken + // see: https://github.com/actix/actix-extras/issues/127 + // TODO replace this when fixed upstream + .wrap_fn(auth::auth_middleware) + .configure(|c| configure_app(c, &inside_config)), + ) .default_service(web::get().to(error_404)) }); @@ -287,55 +290,34 @@ fn configure_header(conf: &MiniserveConfig) -> middleware::DefaultHeaders { /// Configures the Actix application fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) { - let path = &conf.path; - let random_route = conf.random_route.clone().unwrap_or_default(); - let full_route = format!("/{}", random_route); - - let upload_route = if let Some(random_route) = conf.random_route.clone() { - format!("/{}/upload", random_route) - } else { - "/upload".to_string() - }; - - let serve_path = { - if path.is_file() { - None - } else { - // build `Files` service using configuraion parameters - let files = actix_files::Files::new(&full_route, path); - let files = match &conf.index { - Some(index_file) => files.index_file(index_file.to_string_lossy()), - None => files, - }; - let files = match conf.show_hidden { - true => files.use_hidden_files(), - false => files, - }; - let files = files - .show_files_listing() - .files_listing_renderer(listing::directory_listing) - .prefer_utf8(true) - .redirect_to_slash_directory() - .default_handler(web::to(error_404)); - Some(files) - } + let files_service = || { + let files = actix_files::Files::new("", &conf.path); + let files = match &conf.index { + Some(index_file) => files.index_file(index_file.to_string_lossy()), + None => files, + }; + let files = match conf.show_hidden { + true => files.use_hidden_files(), + false => files, + }; + files + .show_files_listing() + .files_listing_renderer(listing::directory_listing) + .prefer_utf8(true) + .redirect_to_slash_directory() + .default_handler(web::to(error_404)) }; - if let Some(serve_path) = serve_path { + if !conf.path.is_file() { if conf.file_upload { // Allow file upload - app.service( - web::resource(&upload_route).route(web::post().to(file_upload::upload_file)), - ) - // Handle directories - .service(serve_path); - } else { - // Handle directories - app.service(serve_path); + app.service(web::resource("/upload").route(web::post().to(file_upload::upload_file))); } + // Handle directories + app.service(files_service()); } else { // Handle single files - app.service(web::resource(&full_route).route(web::to(listing::file_handler))); + app.service(web::resource(["", "/"]).route(web::to(listing::file_handler))); } } |