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

Add abillity to set path prefix #579

Closed
wants to merge 4 commits into from
Closed
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
171 changes: 87 additions & 84 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,100 +78,103 @@ Sometimes this is just a more practical and quick way than doing things properly

## Usage

miniserve 0.15.0
miniserve 0.15.1-alpha.0
Sven-Hendrik Haase <svenstaro@gmail.com>, Boastful Squirrel <boastful.squirrel@gmail.com>
For when you really just want to serve some files over HTTP right now!

USAGE:
miniserve [FLAGS] [OPTIONS] [--] [PATH]

FLAGS:
-D, --dirs-first
List directories first

-r, --enable-tar
Enable uncompressed tar archive generation

-g, --enable-tar-gz
Enable gz-compressed tar archive generation

-z, --enable-zip
Enable zip archive generation

WARNING: Zipping large directories can result in out-of-memory exception because zip generation is done in
memory and cannot be sent on the fly
-u, --upload-files
Enable file uploading

-h, --help
Prints help information

-H, --hidden
Show hidden files

-F, --hide-version-footer
Hide version footer

-P, --no-symlinks
Do not follow symbolic links

-o, --overwrite-files
Enable overriding existing files during file upload

-q, --qrcode
Enable QR code display

--random-route
Generate a random 6-hexdigit route

-V, --version
Prints version information

-v, --verbose
Be verbose, includes emitting access logs


-D, --dirs-first
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the whitespaces here? Thanks for updating the README but let's not introduce unnecessary whitespaces.

List directories first
-r, --enable-tar
Enable uncompressed tar archive generation
-g, --enable-tar-gz
Enable gz-compressed tar archive generation
-z, --enable-zip
Enable zip archive generation
WARNING: Zipping large directories can result in out-of-memory exception because zip generation is done in
memory and cannot be sent on the fly
-u, --upload-files
Enable file uploading
-h, --help
Prints help information
-H, --hidden
Show hidden files
-F, --hide-version-footer
Hide version footer
-P, --no-symlinks
Do not follow symbolic links
-o, --overwrite-files
Enable overriding existing files during file upload
-q, --qrcode
Enable QR code display
--random-path-prefix
Use a random path prefix
-V, --version
Prints version information
-v, --verbose
Be verbose, includes emitting access logs
OPTIONS:
-a, --auth <auth>...
Set authentication. Currently supported formats: username:password, username:sha256:hash,
username:sha512:hash (e.g. joe:123,
joe:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3)
Set authentication. Currently supported formats: username:password, username:sha256:hash,
username:sha512:hash (e.g. joe:123,
joe:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3)
-c, --color-scheme <color-scheme>
Default color scheme [default: squirrel] [possible values: squirrel, archlinux,
zenburn, monokai]
Default color scheme [default: squirrel] [possible values: squirrel, archlinux,
zenburn, monokai]
-d, --color-scheme-dark <color-scheme-dark>
Default color scheme [default: archlinux] [possible values: squirrel, archlinux,
zenburn, monokai]
--header <header>...
Set custom header for responses

--index <index_file>
The name of a directory index file to serve, like "index.html"

Normally, when miniserve serves a directory, it creates a listing for that directory. However, if a
directory contains this file, miniserve will serve that file instead.
-i, --interfaces <interfaces>...
Interface to listen on

-p, --port <port>
Port to use [default: 8080]

--print-completions <shell>
Generate completion file for a shell [possible values: zsh, bash, fish,
powershell, elvish]
-t, --title <title>
Shown instead of host in page title and heading

--tls-cert <tls-cert>
TLS certificate to use

--tls-key <tls-key>
TLS private key to use


Default color scheme [default: archlinux] [possible values: squirrel, archlinux,
zenburn, monokai]
--header <header>...
Set custom header for responses

--index <index_file>
The name of a directory index file to serve, like "index.html"

Normally, when miniserve serves a directory, it creates a listing for that directory. However, if a
directory contains this file, miniserve will serve that file instead.
-i, --interfaces <interfaces>...
Interface to listen on

--path-prefix <path-prefix>
Use a specific path prefix

-p, --port <port>
Port to use [default: 8080]

--print-completions <shell>
Generate completion file for a shell [possible values: zsh, bash, fish,
powershell, elvish]
-t, --title <title>
Shown instead of host in page title and heading

--tls-cert <tls-cert>
TLS certificate to use

--tls-key <tls-key>
TLS private key to use


ARGS:
<PATH>
Which path to serve
<PATH>
Which path to serve

## How to install

Expand Down
10 changes: 7 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ pub struct CliArgs {
)]
pub auth: Vec<auth::RequiredAuth>,

/// Generate a random 6-hexdigit route
#[structopt(long = "random-route")]
pub random_route: bool,
/// Use a specific path prefix
#[structopt(long = "path-prefix")]
pub path_prefix: Option<String>,
Comment on lines +59 to +60
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to rename to "route" IMO to avoid a breaking change and to be distinguished from filesystem path!


/// Use a random path prefix
#[structopt(long = "random-path-prefix", conflicts_with("path-prefix"))]
pub random_path_prefix: bool,

/// Do not follow symbolic links
#[structopt(short = "P", long = "no-symlinks")]
Expand Down
4 changes: 2 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ fn build_unauthorized_response(
if log_error_chain {
errors::log_error_chain(error.to_string());
}
let return_path = match state.random_route {
Some(ref random_route) => format!("/{}", random_route),
let return_path = match state.path_prefix {
Some(ref path_prefix) => format!("/{}", path_prefix),
Comment on lines +112 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let return_path = match state.path_prefix {
Some(ref path_prefix) => format!("/{}", path_prefix),
let return_path = match state.path_prefix {
Some(ref path_prefix) => format!("/{}", path_prefix.trim_matches('/')),

to better handle unexpected user input.

None => "/".to_string(),
};

Expand Down
14 changes: 10 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ pub struct MiniserveConfig {
/// Show hidden files
pub show_hidden: bool,

/// Enable random route generation
pub random_route: Option<String>,
/// Use a specific path prefix
pub path_prefix: Option<String>,
Comment on lines +46 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer this to be just String, where absent route prefix is represented as empty string. This would make it easy to use when building urls.

You may want to take a look at my previous PR 7ea3beb


/// Use a random path prefix
pub random_path_prefix: bool,
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused field.


/// Randomly generated favicon route
pub favicon_route: String,
Expand Down Expand Up @@ -110,7 +113,9 @@ impl MiniserveConfig {
]
};

let random_route = if args.random_route {
let path_prefix = if let Some(path_prefix) = args.path_prefix {
Some(path_prefix)
} else if args.random_path_prefix {
Some(nanoid::nanoid!(6, &ROUTE_ALPHABET))
} else {
None
Expand Down Expand Up @@ -159,7 +164,8 @@ impl MiniserveConfig {
path_explicitly_chosen,
no_symlinks: args.no_symlinks,
show_hidden: args.hidden,
random_route,
path_prefix,
random_path_prefix: args.random_path_prefix,
favicon_route,
css_route,
default_color_scheme,
Expand Down
14 changes: 7 additions & 7 deletions src/file_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn handle_multipart(
pub fn upload_file(
req: HttpRequest,
payload: actix_web::web::Payload,
uses_random_route: bool,
uses_path_prefix: bool,
favicon_route: String,
css_route: String,
default_color_scheme: &str,
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn upload_file(
&return_path,
query_params.sort,
query_params.order,
uses_random_route,
uses_path_prefix,
&favicon_route,
&css_route,
default_color_scheme,
Expand All @@ -160,7 +160,7 @@ pub fn upload_file(
&return_path,
query_params.sort,
query_params.order,
uses_random_route,
uses_path_prefix,
&favicon_route,
&css_route,
default_color_scheme,
Expand All @@ -183,7 +183,7 @@ pub fn upload_file(
&return_path,
query_params.sort,
query_params.order,
uses_random_route,
uses_path_prefix,
&favicon_route,
&css_route,
default_color_scheme,
Expand Down Expand Up @@ -214,7 +214,7 @@ pub fn upload_file(
&return_path,
query_params.sort,
query_params.order,
uses_random_route,
uses_path_prefix,
&favicon_route,
&css_route,
&default_color_scheme,
Expand All @@ -233,7 +233,7 @@ fn create_error_response(
return_path: &str,
sorting_method: Option<SortingMethod>,
sorting_order: Option<SortingOrder>,
uses_random_route: bool,
uses_path_prefix: bool,
favicon_route: &str,
css_route: &str,
default_color_scheme: &str,
Expand All @@ -252,7 +252,7 @@ fn create_error_response(
sorting_method,
sorting_order,
true,
!uses_random_route,
!uses_path_prefix,
favicon_route,
css_route,
default_color_scheme,
Expand Down
11 changes: 5 additions & 6 deletions src/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub fn directory_listing(
skip_symlinks: bool,
show_hidden: bool,
file_upload: bool,
random_route: Option<String>,
path_prefix: Option<String>,
favicon_route: String,
css_route: String,
default_color_scheme: &str,
Expand Down Expand Up @@ -189,10 +189,10 @@ pub fn directory_listing(
}

let base = Path::new(serve_path);
let random_route_abs = format!("/{}", random_route.clone().unwrap_or_default());
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&random_route_abs);
let path_prefix_abs = format!("/{}", path_prefix.clone().unwrap_or_default());
let is_root = base.parent().is_none() || Path::new(&req.path()) == Path::new(&path_prefix_abs);

let encoded_dir = match base.strip_prefix(random_route_abs) {
let encoded_dir = match base.strip_prefix(path_prefix_abs) {
Ok(c_d) => Path::new("/").join(c_d),
Err(_) => base.to_path_buf(),
}
Expand All @@ -205,8 +205,7 @@ pub fn directory_listing(
let decoded = percent_decode_str(&encoded_dir).decode_utf8_lossy();

let mut res: Vec<Breadcrumb> = Vec::new();
let mut link_accumulator =
format!("/{}", random_route.map(|r| r + "/").unwrap_or_default());
let mut link_accumulator = format!("/{}", path_prefix.map(|r| r + "/").unwrap_or_default());

let mut components = Path::new(&*decoded).components().peekable();

Expand Down
Loading