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

net/http: allow customization of FileServer's directory HTML and styling #47838

Closed
rbucker opened this issue Aug 20, 2021 · 2 comments
Closed
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@rbucker
Copy link

rbucker commented Aug 20, 2021

go/src/net/http/fs.go

Lines 128 to 165 in e9e0d1e

func dirList(w ResponseWriter, r *Request, f File) {
// Prefer to use ReadDir instead of Readdir,
// because the former doesn't require calling
// Stat on every entry of a directory on Unix.
var dirs anyDirs
var err error
if d, ok := f.(fs.ReadDirFile); ok {
var list dirEntryDirs
list, err = d.ReadDir(-1)
dirs = list
} else {
var list fileInfoDirs
list, err = f.Readdir(-1)
dirs = list
}
if err != nil {
logf(r, "http: error reading directory: %v", err)
Error(w, "Error reading directory", StatusInternalServerError)
return
}
sort.Slice(dirs, func(i, j int) bool { return dirs.name(i) < dirs.name(j) })
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprintf(w, "<pre>\n")
for i, n := 0, dirs.len(); i < n; i++ {
name := dirs.name(i)
if dirs.isDir(i) {
name += "/"
}
// name may contain '?' or '#', which must be escaped to remain
// part of the URL path, and not indicate the start of a query
// string or fragment.
url := url.URL{Path: name}
fmt.Fprintf(w, "<a href=\"%s\">%s</a>\n", url.String(), htmlReplacer.Replace(name))
}
fmt.Fprintf(w, "</pre>\n")
}

The overarching purpose of the fileserver is to serve files in a safe way. But then it has this side effect of being able to generate a directory list. The directory list is a very basic PRE block with some A links. The code also sets the content-type. However, there is something missing.

  • is the HTML that it generates simply the smallest code possible?
  • is there a way to style that fragment either with a template or some CSS etc
  • since it's just a PRE block should I wrap the handler in a handler and then write a header, then call the handler, then write the footer
  • maybe there should be a callback instead of hardcoded
  • maybe there should be a template feature

At the very least please update the documentation to set the intent.

@mknyszek
Copy link
Contributor

If I understand correctly, the file server is meant as a very basic server. Very useful for one-offs and non-critical situations, but I don't think that this is ever intended to be a solution that is very customizable.

CC @neild

@mknyszek mknyszek added FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 20, 2021
@mknyszek mknyszek added this to the Backlog milestone Aug 20, 2021
@mknyszek mknyszek changed the title how or why templating the fileserver dirlist net/http: allow customization of FileServer's directory HTML and styling Aug 20, 2021
@mknyszek
Copy link
Contributor

Actually, see #29945. This has been declined more than once. Closing.

@golang golang locked and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants