Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list directory if index not found #583

Merged
merged 1 commit into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,12 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let uses_random_route = conf.random_route.clone().is_some();
let full_route = format!("/{}", random_route);

let upload_route;
let upload_route = if let Some(random_route) = conf.random_route.clone() {
format!("/{}/upload", random_route)
} else {
"/upload".to_string()
};

let serve_path = {
let path = &conf.path;
let no_symlinks = conf.no_symlinks;
Expand All @@ -299,29 +304,24 @@ fn configure_app(app: &mut web::ServiceConfig, conf: &MiniserveConfig) {
let dirs_first = conf.dirs_first;
let hide_version_footer = conf.hide_version_footer;
let title = conf.title.clone();
upload_route = if let Some(random_route) = conf.random_route.clone() {
format!("/{}/upload", random_route)
} else {
"/upload".to_string()
};

if path.is_file() {
None
} else if let Some(index_file) = &conf.index {
Some(
actix_files::Files::new(&full_route, path).index_file(index_file.to_string_lossy()),
)
} else {
let u_r = upload_route.clone();
let files;
if show_hidden {
files = actix_files::Files::new(&full_route, path)
.show_files_listing()
.use_hidden_files();
} else {
files = actix_files::Files::new(&full_route, path).show_files_listing();
}

// build `Files` service using configuraion parameters
let files = actix_files::Files::new(&full_route, path);
let files = match &conf.index {
svenstaro marked this conversation as resolved.
Show resolved Hide resolved
Some(index_file) => files.index_file(index_file.to_string_lossy()),
None => files,
};
let files = match show_hidden {
true => files.use_hidden_files(),
false => files,
};
let files = files
.show_files_listing()
.files_listing_renderer(move |dir, req| {
listing::directory_listing(
dir,
Expand Down
11 changes: 11 additions & 0 deletions tests/serve_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,14 @@ fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(),

Ok(())
}

#[rstest]
#[case(server(&["--index", FILES[0]]))]
#[case(server(&["--index", "does-not-exist.html"]))]
fn index_fallback_to_listing(#[case] server: TestServer) -> Result<(), Error> {
// If index file is not found, show directory listing instead.
// both cases should return `Ok`
reqwest::blocking::get(server.url())?.error_for_status()?;

Ok(())
}