// SPDX-License-Identifier: Apache-2.0 package gospa import ( "crypto/sha256" "encoding/hex" "io" "io/fs" "net/http" "strings" "sync" "time" ) // noCache lets a browser keep an answer but check it with the server on every load. const noCache = "no-cache" // folder sends the regular files of one fs.FS, tagging by content each file that carries no date. type folder struct { fsys fs.FS tags sync.Map } // openFile is a regular file of a folder, open to be sent. type openFile struct { name string file fs.File info fs.FileInfo } // open returns the regular file at name, or false when name names none. func (f *folder) open(name string) (openFile, bool) { if !fs.ValidPath(name) { return openFile{}, false } file, err := f.fsys.Open(name) if err != nil { return openFile{}, false } info, err := file.Stat() if err != nil || !info.Mode().IsRegular() { _ = file.Close() return openFile{}, false } return openFile{name: name, file: file, info: info}, true } // send writes open with the type of its name and control as its Cache-Control, answering ranges and checks. func (f *folder) send(w http.ResponseWriter, r *http.Request, open openFile, control string) { defer func() { _ = open.file.Close() }() content, tag, err := f.content(open) if err != nil { http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } header := w.Header() if tag != "" { header.Set("ETag", tag) } describe(header, open.name, control) http.ServeContent(w, r, "", open.info.ModTime(), content) } // describe sets the type of name, control as the Cache-Control, and nosniff. func describe(header http.Header, name, control string) { header.Set("Content-Type", typeOf(name)) header.Set("Cache-Control", control) header.Set("X-Content-Type-Options", "nosniff") } // content returns a seekable reader over open and, when open carries no date, the tag of its content. func (f *folder) content(open openFile) (io.ReadSeeker, string, error) { tag, err := f.tag(open) if err != nil { return nil, "", err } if seeker, ok := open.file.(io.ReadSeeker); ok { return seeker, tag, nil } return &forward{file: open.file, size: open.info.Size()}, tag, nil } // tag returns the tag of the content of open, worked out once for each name, or nothing when open carries a date. func (f *folder) tag(open openFile) (string, error) { if dated(open.info.ModTime()) { return "", nil } if known, ok := f.tags.Load(open.name); ok { return known.(string), nil } file, err := f.fsys.Open(open.name) if err != nil { return "", err } defer func() { _ = file.Close() }() sum := sha256.New() if _, err := io.Copy(sum, file); err != nil { return "", err } known, _ := f.tags.LoadOrStore(open.name, tagFrom(sum.Sum(nil))) return known.(string), nil } // dated reports whether changed is a date http.ServeContent sends, as neither the zero time nor the Unix epoch is. func dated(changed time.Time) bool { return !changed.IsZero() && !changed.Equal(time.Unix(0, 0)) } // tagFrom returns the strong entity tag of a sha256 sum. func tagFrom(sum []byte) string { return `"` + hex.EncodeToString(sum[:16]) + `"` } // readable reports whether r asks to read, answering 405 when it does not. func readable(w http.ResponseWriter, r *http.Request) bool { if r.Method == http.MethodGet || r.Method == http.MethodHead { return true } w.Header().Set("Allow", "GET, HEAD") http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return false } // fileName returns the file name a URL path asks for, or false when one of its segments may not stand in a path. func fileName(urlPath string) (string, bool) { name := strings.TrimPrefix(urlPath, "/") segments := strings.Split(name, "/") for i, segment := range segments { if !allowed(segment, i == len(segments)-1) { return "", false } } return name, true } // allowed reports whether segment may stand in a path, last saying whether it ends the path. func allowed(segment string, last bool) bool { switch { case segment == "": return last case strings.HasPrefix(segment, "."): return segment == ".well-known" } return !strings.ContainsRune(segment, '~') && strings.TrimRight(segment, ". ") == segment } // orNotFound returns handler, or http.NotFound when handler is nil. func orNotFound(handler http.Handler) http.Handler { if handler == nil { return http.HandlerFunc(http.NotFound) } return handler }