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 the index file doesn't exist #484

Closed
wants to merge 4 commits into from

Conversation

aliemjay
Copy link
Contributor

actix-files doesn't support such behaviour, which makes it necessary to
implement in the "listing renderer".

Should fix #275

@svenstaro
Copy link
Owner

Good stuff! Please do look into that test failure though and see whether perhaps you actually need to change or make new tests to make sure the new behavior is also being covered.

src/listing.rs Show resolved Hide resolved
@aliemjay aliemjay force-pushed the optional-index branch 3 times, most recently from ce9827e to a3be4f9 Compare March 29, 2021 19:13
src/main.rs Outdated Show resolved Hide resolved
src/listing.rs Outdated Show resolved Hide resolved
@svenstaro
Copy link
Owner

This makes me wonder: Should we warn in case the file provided by --index lays outside of the current serving root?

For instance, it's possible to serve /a while providing --index /b/index.html which might be unexpected and potentially a security problem if users assume that --index /b/index.html also sets the serving root to /b/.

In fact, now that I think about it, perhaps we should default to serving the directory the index file is in if no explicit serving path is provided. What do you think?

@aliemjay
Copy link
Contributor Author

aliemjay commented Apr 1, 2021

I don't see the point of allowing the user to set a full path for --index option!
I think that the --index option should only accept a single file name and the user should always be required to set the root directory explicitly. My reasoning is that:

  • the command miniserve --index /b/index.html may be interpreted as serving a single file, yet it would expose the whole tree under /b/
  • It is not always true that the root directory has an index file. In my setup, only some subdirectories have an index file and I expect to see a directory listing for root directory. This was my motivation for this PR.

I believe index option may need a check of something like:
if let Some((0, Component::Normal(_))) = index.components().enumerate().last() {}

@svenstaro
Copy link
Owner

@aliemjay I'm very much interested in merging this. See also this effort in actix-web to offer something similar upstream. However, I'd like to get the path questions that I asked cleaned up and tested. As you said, it probably doesn't make much sense to allow the user to set a path outside the index file path or at worst it's a bad surprise for the user!

How about we change the behavior of the program like this:

  1. Don't let --index take an argument.
  2. If --index is provided, the path for miniserve needs to point to something that looks like a file.
  3. If index doesn't exist, serve base dir of serve path.

Does that make sense to you? This way, we'd establish more intuitive behavior for --index.

@aliemjay
Copy link
Contributor Author

The PR mentioned conflicts with the behaviour implemented here!
I think then we need to offer an option of "What to to do when the index file doesn't exist" and this makes it neccessary to not rely on the upstream implementation of default page in order to allow the user choose.

@svenstaro
Copy link
Owner

svenstaro commented Apr 18, 2021

I agree, we don't necessarily follow upstream. Let's roll with your adapted implementation instead. How do you want to resolve the path problems laid out above?

@aliemjay
Copy link
Contributor Author

aliemjay commented Apr 18, 2021

Does that make sense to you?

I've just gave it a thought and had some problems:

  • First, This behaviour conflicts with serving a single file, which is already supported.

  • First, It doesn't allow such use case:

It is not always true that the root directory has an index file. In my setup, only some subdirectories have an index file and I expect to see a directory listing for root directory. This was my motivation for this PR.

  • Second, I don't know if miniserve --index ./xxx.html, for example, is expected to find and serve xxx.html at root ony (/) or at any route?

@svenstaro
Copy link
Owner

svenstaro commented Apr 18, 2021

Hmm I think in that case it would indeed be the best next thing to just serve each directory's respective index file if that exists and fall back to listing the files otherwise.

The compromise would be that --index can never list a full path but only ever a single file without preceding directories. How does that sound?

@aliemjay
Copy link
Contributor Author

aliemjay commented Apr 18, 2021

The compromise would be that --index can never list a full path but only ever a single file without preceding directories. How does that sound?

If you mean the current behaviour, then I think it's totally fine and intuitive :)

In this case, --index option needs a check to accept only a single file name.

actix-files doesn't support such behaviour, which makes it necessary to
implement in the "listing renderer".
and when path is explicitly specified, make sure that index option is a
single file name, not a full path.
@aliemjay
Copy link
Contributor Author

aliemjay commented May 6, 2021

Rebased and tried to fix the path issue with the following behaviour:

In fact, now that I think about it, perhaps we should default to serving the directory the index file is in if no explicit serving path is provided. What do you think?

Plus, if the serving path is explicitely provided, then --index should only accept a single file name

@svenstaro
Copy link
Owner

Can you rebase this for the new amazing tests? :)

@svenstaro
Copy link
Owner

svenstaro commented Aug 29, 2021

Superseded by #583.

@svenstaro svenstaro closed this Aug 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'miniserve --index index.html' gives 404 when index.html is not present in a directory.
2 participants