Repository navigation
Adding the option to use an fs.FS for the file server middleware - #68
Conversation
Add .gitattributes where EOL is set to LF for testdata in file package so that tests would not fail on Windows systems.
os.DirFS rejects the names http.Dir used to clean, so the default RootPath path stopped serving directory URLs with a trailing slash and paths with "//", "." or ".." segments. Keep http.Dir for RootPath and clean names the same way before handing them to an fs.FS. Join the index file name with path.Join: http.FileSystem names are slash-separated, and filepath.Join made index requests for subdirectories 404 on Windows. Add fstest.MapFS tests, containment tests with hostile names for RootPath and a directory-backed FS, FS notes in the ServerOptions and PathMap docs, and an embed.FS example in the README.
|
Thanks for this, and sorry it sat for four years. Serving an Before merging I compared behaviour with master and found two things:
I pushed a fix to your branch (8e509ad): |
Resolves file/server.go against the v2.4.1 fix: the PathMap target check (withinTarget) and plain 404s now apply to the FS path too, next to cleanFS. Adds TestServerFSStaysInMappedDir with the v2.4.1 escape cases through an fstest.MapFS, and the CHANGELOG entry for ServerOptions.FS.
Brings in the gddo removal (go-ozzo#75) so the branch is up to date with master, as the ruleset requires.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #68 +/- ##
==========================================
+ Coverage 94.85% 94.87% +0.02%
==========================================
Files 21 21
Lines 1010 1014 +4
==========================================
+ Hits 958 962 +4
Misses 52 52
🚀 New features to boost your workflow:
|
kolkov
left a comment
There was a problem hiding this comment.
I merged master into the branch, so it now has the path checks from v2.4.1, and they apply to FS as well (there are tests for that through fstest.MapFS). CI is green on Linux, Windows and macOS. Approving; this goes into v2.5.0. Thanks for your patience!
This pull request adds the option
FSto theServerOptionsof the file server middleware which allows passing anfs.FSas content root.For example, this makes it possible to use static web files directly compiled into the binary using
embed.FS.When the
FSfield is specified, this overrides the setRootPath. Otherwise, whenFSis not specified, the given (or default)RootPathis used viaos.DirFS.Additionally, I've added a
.gitattributewhich specifies the contents offile/testdatato be cloned with LF end of lines. Otherwise, all unit tests fail when cloned on a Windows system because files are converted to CLRF.