From 1bce2044197eb3cc6759e19e474b6558cee4e0a6 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Tue, 27 Jun 2023 23:47:10 +0200 Subject: [PATCH] refactor: improve auto index options --- src/directory_listing.rs | 43 ++++++++++++++++++++++++++++--------------- src/static_files.rs | 21 ++++++++++++--------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/directory_listing.rs b/src/directory_listing.rs index 19c67b8..4ef1f40 100644 --- a/src/directory_listing.rs +++ b/src/directory_listing.rs @@ -33,36 +33,49 @@ pub enum DirListFmt { Json, } +/// Directory listing options. +pub struct DirListOpts<'a> { + /// Request method. + pub method: &'a Method, + /// Current Request path. + pub current_path: &'a str, + /// URI Request query + pub uri_query: Option<&'a str>, + /// Request file path. + pub filepath: &'a Path, + /// Directory listing order. + pub dir_listing_order: u8, + /// Directory listing format. + pub dir_listing_format: &'a DirListFmt, + /// Ignore hidden files (dotfiles). + pub ignore_hidden_files: bool, +} + /// Provides directory listing support for the current request. /// Note that this function highly depends on `static_files::composed_file_metadata()` function /// which must be called first. See `static_files::handle()` for more details. -pub fn auto_index<'a>( - method: &'a Method, - current_path: &'a str, - uri_query: Option<&'a str>, - filepath: &'a Path, - dir_listing_order: u8, - dir_listing_format: &'a DirListFmt, - ignore_hidden_files: bool, -) -> impl Future, StatusCode>> + Send + 'a { +pub fn auto_index( + opts: DirListOpts<'_>, +) -> impl Future, StatusCode>> + Send + '_ { // Note: it's safe to call `parent()` here since `filepath` // value always refer to a path with file ending and under // a root directory boundary. // See `composed_file_metadata()` function which sanitizes the requested // path before to be delegated here. + let filepath = opts.filepath; let parent = filepath.parent().unwrap_or(filepath); tokio::fs::read_dir(parent).then(move |res| match res { Ok(dir_reader) => Either::Left(async move { - let is_head = method.is_head(); + let is_head = opts.method.is_head(); match read_dir_entries( dir_reader, - current_path, - uri_query, + opts.current_path, + opts.uri_query, is_head, - dir_listing_order, - dir_listing_format, - ignore_hidden_files, + opts.dir_listing_order, + opts.dir_listing_format, + opts.ignore_hidden_files, ) .await { diff --git a/src/static_files.rs b/src/static_files.rs index 9cb04e4..12ee3f4 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -34,7 +34,10 @@ use crate::exts::path::PathExt; use crate::Result; #[cfg(feature = "directory-listing")] -use crate::{directory_listing, directory_listing::DirListFmt}; +use crate::{ + directory_listing, + directory_listing::{DirListFmt, DirListOpts}, +}; /// Defines all options needed by the static-files handler. pub struct HandleOpts<'a> { @@ -136,15 +139,15 @@ pub async fn handle<'a>(opts: &HandleOpts<'a>) -> Result<(Response, bool), // if it does not contain an `index.html` file (if a proper auto index is generated) #[cfg(feature = "directory-listing")] if opts.dir_listing && !file_path.exists() { - let resp = directory_listing::auto_index( + let resp = directory_listing::auto_index(DirListOpts { method, - uri_path, - opts.uri_query, - file_path, - opts.dir_listing_order, - opts.dir_listing_format, - opts.ignore_hidden_files, - ) + current_path: uri_path, + uri_query: opts.uri_query, + filepath: file_path, + dir_listing_order: opts.dir_listing_order, + dir_listing_format: opts.dir_listing_format, + ignore_hidden_files: opts.ignore_hidden_files, + }) .await?; return Ok((resp, is_precompressed)); -- libgit2 1.7.2