Fix EmbeddedFilesEndpoint
not working for index.html
in subdirectories
#825
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Close #824
This is a simple fix of this bug, which is more suitable as a default behavior of
EmbeddedFilesEndpoint
.Its basic logic is:
First:
let path = req.uri().path().trim_start_matches('/');
Then if
path
is empty, andreq.original_uri().path()
does not end with '/', add '/' to the original uri path and redirect.Otherwise, if
req.original_uri().path()
ends with '/', addindex.html
topath
and callEmbeddedFileEndpoint
.Otherwise check if the file pointed to
path
exists, and if so, callEmbeddedFileEndpoint
directly.Otherwise, add
/index.html
and check if the file exists. If it does, add '/' to the original uri path and redirect.Otherwise, call the
EmbeddedFileEndpoint
to return an appropriate 404.However, in the long run, EmbeddedFilesEndpoint should have the same interface and behavior as StaticFilesEndpoint to ensure the consistency of the framework API, while also providing more customization of static resource behavior.
This fix only provides a more reasonable and consistent default behavior without introducing an API Breaking Change.
这是对这个bug的一个简易修复,比较适合作为
EmbeddedFilesEndpoint
的一个默认行为。其基本逻辑是:
首先:
let path = req.uri().path().trim_start_matches('/');
然后如果
path
为空,且req.original_uri().path()
不以'/'结尾,则给original uri path加上'/'之后重定向。否则如果
req.original_uri().path()
以'/'结尾,则给path
加index.html
然后调用EmbeddedFileEndpoint
。否则检查
path
指向的文件是否存在,如果存在则直接调用EmbeddedFileEndpoint
。否则加上
/index.html
之后再看看文件是否存在,存在则给original uri path加上'/'之后重定向。否则直接调用
EmbeddedFileEndpoint
以返回合适的404。但是长期来看,EmbeddedFilesEndpoint应当具备与StaticFilesEndpoint相同的接口与行为,以保证框架API的一致性,同时也会提供对静态资源行为的更多定制性。
本修复只是在不引入API Breaking Change的前提下,提供了一个更合理且更一致的默认行为。