test/serve: fix a descriptor leak, add preliminary directory support
Fix a descriptor leak in the helper, and add some minimal support for clients that might want to scrape HTML of a directory to walk the filesystem. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
This commit is contained in:
parent
36c3f13d85
commit
cbe5823958
|
@ -3,32 +3,57 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"html"
|
||||||
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func sendThatFile(basepath string) func(w http.ResponseWriter, r *http.Request) {
|
func sendThatFile(basepath string) func(w http.ResponseWriter, r *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
filename := filepath.Join(basepath, filepath.Clean(string([]rune{filepath.Separator})+r.URL.Path))
|
filename := filepath.Join(basepath, filepath.Clean(string([]rune{filepath.Separator})+filepath.FromSlash(r.URL.Path)))
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Error(w, "whoops", http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
finfo, err := f.Stat()
|
finfo, err := f.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "whoops", http.StatusInternalServerError)
|
http.Error(w, fmt.Sprintf("checking file info: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.ServeContent(w, r, filename, finfo.ModTime(), f)
|
content := io.ReadSeeker(f)
|
||||||
|
if finfo.IsDir() {
|
||||||
|
names, err := f.ReadDir(-1)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, fmt.Sprintf("reading directory: %v", err), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
var builder strings.Builder
|
||||||
|
builder.WriteString("<body><html>")
|
||||||
|
for _, name := range names {
|
||||||
|
suffix := ""
|
||||||
|
if name.IsDir() {
|
||||||
|
suffix = "/"
|
||||||
|
}
|
||||||
|
builder.WriteString(fmt.Sprintf("<a href=%q>%s</a><br/>\n", path.Join(r.URL.Path, name.Name())+suffix, html.EscapeString(fs.FormatDirEntry(name))))
|
||||||
|
}
|
||||||
|
builder.WriteString("</html></body>")
|
||||||
|
content = strings.NewReader(builder.String())
|
||||||
|
}
|
||||||
|
http.ServeContent(w, r, filename, finfo.ModTime(), content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue