Skip to content

Commit

Permalink
Add -x/--strip-extensions to allow serving /file.html as /file
Browse files Browse the repository at this point in the history
Closes #113
  • Loading branch information
nabijaczleweli committed Sep 27, 2020
1 parent 22cd586 commit 4b6f57d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
7 changes: 7 additions & 0 deletions http.md
Expand Up @@ -182,6 +182,13 @@ pass parameters like what port to use.

This is false by default because it's useful for reducing bandwidth usage.

-x --strip-extensions

Allow stripping index extentions from served paths:
a request to /file might get served by /file.[s]htm[l].

This is false by defailt

-q --quiet...

Suppress increasing amounts of output.
Expand Down
11 changes: 10 additions & 1 deletion src/ops/mod.rs
Expand Up @@ -116,6 +116,7 @@ pub struct HttpHandler {
pub follow_symlinks: bool,
pub sandbox_symlinks: bool,
pub check_indices: bool,
pub strip_extensions: bool,
/// (at all, log_colour)
pub log: (bool, bool),
pub webdav: bool,
Expand Down Expand Up @@ -153,6 +154,7 @@ impl HttpHandler {
follow_symlinks: opts.follow_symlinks,
sandbox_symlinks: opts.sandbox_symlinks,
check_indices: opts.check_indices,
strip_extensions: opts.strip_extensions,
log: (opts.loglevel < LogLevel::NoServeStatus, opts.log_colour),
webdav: opts.webdav,
global_auth_data: global_auth_data,
Expand Down Expand Up @@ -325,12 +327,18 @@ impl HttpHandler {
}

fn handle_get(&self, req: &mut Request) -> IronResult<Response> {
let (req_p, symlink, url_err) = self.parse_requested_path(req);
let (mut req_p, symlink, url_err) = self.parse_requested_path(req);

if url_err {
return self.handle_invalid_url(req, "<p>Percent-encoding decoded to invalid UTF-8.</p>");
}

if !req_p.exists() && req_p.extension().is_none() && self.strip_extensions {
if let Some(rp) = INDEX_EXTENSIONS.iter().map(|ext| req_p.with_extension(ext)).find(|rp| rp.exists()) {
req_p = rp;
}
}

if !req_p.exists() || (symlink && !self.follow_symlinks) ||
(symlink && self.follow_symlinks && self.sandbox_symlinks && !is_descendant_of(&req_p, &self.hosted_directory.1)) {
return self.handle_nonexistent(req, req_p);
Expand Down Expand Up @@ -1319,6 +1327,7 @@ impl Clone for HttpHandler {
follow_symlinks: self.follow_symlinks,
sandbox_symlinks: self.sandbox_symlinks,
check_indices: self.check_indices,
strip_extensions: self.strip_extensions,
log: self.log,
webdav: self.webdav,
global_auth_data: self.global_auth_data.clone(),
Expand Down
4 changes: 4 additions & 0 deletions src/options.rs
Expand Up @@ -78,6 +78,8 @@ pub struct Options {
pub temp_directory: (String, PathBuf),
/// Whether to check for index files in served directories before serving a listing. Default: true
pub check_indices: bool,
/// Whether to allow requests to `/file` to return `/file.{INDEX_EXTENSIONS`. Default: false
pub strip_extensions: bool,
/// Whether to allow write operations. Default: false
pub allow_writes: bool,
/// Whether to encode filesystem files. Default: true
Expand Down Expand Up @@ -128,6 +130,7 @@ impl Options {
.arg(Arg::from_usage("-w --allow-write 'Allow for write operations. Default: false'"))
.arg(Arg::from_usage("-i --no-indices 'Always generate dir listings even if index files are available. Default: false'"))
.arg(Arg::from_usage("-e --no-encode 'Do not encode filesystem files. Default: false'"))
.arg(Arg::from_usage("-x --strip-extensions 'Allow stripping index extentions from served paths. Default: false'"))
.arg(Arg::from_usage("-q --quiet... 'Suppress increasing amounts of output'"))
.arg(Arg::from_usage("--no-colour 'Don't colourise the log output'"))
.arg(Arg::from_usage("-d --webdav 'Handle WebDAV requests. Default: false'"))
Expand Down Expand Up @@ -208,6 +211,7 @@ impl Options {
temp_pb.join(suffix))
},
check_indices: !matches.is_present("no-indices"),
strip_extensions: matches.is_present("strip-extensions"),
allow_writes: matches.is_present("allow-write"),
encode_fs: !matches.is_present("no-encode"),
loglevel: matches.occurrences_of("quiet").into(),
Expand Down
2 changes: 1 addition & 1 deletion src/util/mod.rs
Expand Up @@ -106,7 +106,7 @@ pub static PORT_SCAN_HIGHEST: u16 = 9999;
/// The app name and version to use with User-Agent or Server response header.
pub static USER_AGENT: &str = concat!("http/", env!("CARGO_PKG_VERSION"));

/// Index file extensions to look for if `-i` was not specified.
/// Index file extensions to look for if `-i` was not specified and strippable extensions to look for if `-x` was specified.
pub static INDEX_EXTENSIONS: &[&str] = &["html", "htm", "shtml"];


Expand Down

0 comments on commit 4b6f57d

Please sign in to comment.