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

Move directory listing initialization into the corresponding module #373

Merged
merged 3 commits into from
Apr 28, 2024
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
17 changes: 16 additions & 1 deletion src/directory_listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::io;
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::{http_ext::MethodExt, Context, Result};
use crate::{handler::RequestHandlerOpts, http_ext::MethodExt, Context, Result};

/// Non-alphanumeric characters to be percent-encoded
/// excluding the "unreserved characters" because allowed in a URI.
Expand Down Expand Up @@ -59,6 +59,21 @@ pub struct DirListOpts<'a> {
pub ignore_hidden_files: bool,
}

/// Initializes directory listings.
pub fn init(enabled: bool, order: u8, format: DirListFmt, handler_opts: &mut RequestHandlerOpts) {
handler_opts.dir_listing = enabled;
server_info!("directory listing: enabled={enabled}");

handler_opts.dir_listing_order = order;
server_info!("directory listing order code: {order}");

handler_opts.dir_listing_format = format;
server_info!(
"directory listing format: {:?}",
handler_opts.dir_listing_format
);
}

/// 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.
Expand Down
37 changes: 11 additions & 26 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use {
hyper::service::{make_service_fn, service_fn},
};

#[cfg(feature = "directory-listing")]
use crate::directory_listing;
#[cfg(feature = "fallback-page")]
use crate::fallback_page;

Expand Down Expand Up @@ -214,26 +216,6 @@ impl Server {
);
}

// Directory listing options
#[cfg(feature = "directory-listing")]
let dir_listing = general.directory_listing;
#[cfg(feature = "directory-listing")]
server_info!("directory listing: enabled={}", dir_listing);
// Directory listing order number
#[cfg(feature = "directory-listing")]
let dir_listing_order = general.directory_listing_order;
#[cfg(feature = "directory-listing")]
server_info!("directory listing order code: {}", dir_listing_order);
// Directory listing format
#[cfg(feature = "directory-listing")]
let dir_listing_format = general.directory_listing_format;
#[cfg(feature = "directory-listing")]
server_info!("directory listing format: {:?}", dir_listing_format);

// Cache control headers option
let cache_control_headers = general.cache_control_headers;
server_info!("cache control headers: enabled={}", cache_control_headers);

// Log remote address option
let log_remote_address = general.log_remote_address;
server_info!("log remote address: enabled={}", log_remote_address);
Expand Down Expand Up @@ -267,12 +249,6 @@ impl Server {
// Request handler options, some settings will be filled in by modules
let mut handler_opts = RequestHandlerOpts {
root_dir,
#[cfg(feature = "directory-listing")]
dir_listing,
#[cfg(feature = "directory-listing")]
dir_listing_order,
#[cfg(feature = "directory-listing")]
dir_listing_format,
page404: page404.clone(),
page50x: page50x.clone(),
log_remote_address,
Expand All @@ -283,6 +259,15 @@ impl Server {
..Default::default()
};

// Directory listing options
#[cfg(feature = "directory-listing")]
directory_listing::init(
general.directory_listing,
general.directory_listing_order,
general.directory_listing_format,
&mut handler_opts,
);

// Fallback page option
#[cfg(feature = "fallback-page")]
fallback_page::init(&general.page_fallback, &mut handler_opts);
Expand Down
Loading