go-gemini/fs.go

128 lines
3.4 KiB
Go
Raw Normal View History

2020-10-24 13:15:32 -06:00
package gemini
2020-10-11 16:57:04 -06:00
import (
"io"
2020-10-11 18:13:53 -06:00
"mime"
2020-10-11 16:57:04 -06:00
"os"
"path"
)
2020-10-11 18:13:53 -06:00
func init() {
// Add Gemini mime types
mime.AddExtensionType(".gmi", "text/gemini")
mime.AddExtensionType(".gemini", "text/gemini")
2020-10-11 18:13:53 -06:00
}
2021-02-14 17:50:38 -07:00
// A FileSystem implements access to a collection of named files. The elements
// in a file path are separated by slash ('/', U+002F) characters, regardless
// of host operating system convention.
type FileSystem interface {
2020-10-11 16:57:04 -06:00
Open(name string) (File, error)
}
2021-02-14 17:50:38 -07:00
// A File is returned by a FileSystem's Open method and can be served by the
// FileServer implementation.
//
// The methods should behave the same as those on an *os.File.
2020-10-11 16:57:04 -06:00
type File interface {
Stat() (os.FileInfo, error)
Read([]byte) (int, error)
Close() error
}
2021-02-14 17:50:38 -07:00
// A Dir implements FileSystem using the native file system restricted
// to a specific directory tree.
//
// While the FileSystem.Open method takes '/'-separated paths, a Dir's string
// value is a filename on the native file system, not a URL, so it is separated
// by filepath.Separator, which isn't necessarily '/'.
//
// Note that Dir could expose sensitive files and directories. Dir will follow
// symlinks pointing out of the directory tree, which can be especially
// dangerous if serving from a directory in which users are able to create
// arbitrary symlinks. Dir will also allow access to files and directories
// starting with a period, which could expose sensitive directories like .git
// or sensitive files like .htpasswd. To exclude files with a leading period,
// remove the files/directories from the server or create a custom FileSystem
// implementation.
//
// An empty Dir is treated as ".".
2020-10-11 16:57:04 -06:00
type Dir string
2021-02-14 17:50:38 -07:00
// Open implements FileSystem using os.Open, opening files for reading
// rooted and relative to the directory d.
2020-10-11 16:57:04 -06:00
func (d Dir) Open(name string) (File, error) {
2021-02-14 17:50:38 -07:00
return os.Open(path.Join(string(d), name))
}
// FileServer returns a handler that serves Gemini requests with the contents
// of the provided file system.
//
// To use the operating system's file system implementation, use gemini.Dir:
//
// gemini.FileServer(gemini.Dir("/tmp"))
func FileServer(fsys FileSystem) Handler {
return fileServer{fsys}
}
type fileServer struct {
FileSystem
}
func (fs fileServer) ServeGemini(w ResponseWriter, r *Request) {
ServeFile(w, fs, r.URL.Path)
2020-10-27 11:32:48 -06:00
}
// ServeFile responds to the request with the contents of the named file
// or directory.
2021-02-14 17:50:38 -07:00
//
// If the provided file or directory name is a relative path, it is interpreted
// relative to the current directory and may ascend to parent directories. If
// the provided name is constructed from user input, it should be sanitized
// before calling ServeFile.
func ServeFile(w ResponseWriter, fsys FileSystem, name string) {
f, err := openFile(fsys, name)
2020-10-27 11:32:48 -06:00
if err != nil {
w.Status(StatusNotFound)
2020-10-27 11:32:48 -06:00
return
}
// Detect mimetype
2020-10-28 13:13:31 -06:00
ext := path.Ext(name)
2020-10-27 11:32:48 -06:00
mimetype := mime.TypeByExtension(ext)
2021-01-09 22:50:35 -07:00
w.Meta(mimetype)
2020-10-27 11:32:48 -06:00
// Copy file to response writer
_, _ = io.Copy(w, f)
2020-10-27 11:32:48 -06:00
}
2021-02-14 17:50:38 -07:00
func openFile(fsys FileSystem, name string) (File, error) {
f, err := fsys.Open(name)
2020-10-11 16:57:04 -06:00
if err != nil {
return nil, err
}
2021-02-14 17:50:38 -07:00
stat, err := f.Stat()
if err != nil {
return nil, err
}
if stat.Mode().IsRegular() {
return f, nil
}
if stat.IsDir() {
// Try opening index.gmi
f, err := fsys.Open(path.Join(name, "index.gmi"))
if err != nil {
return nil, err
}
stat, err := f.Stat()
if err != nil {
return nil, err
}
if stat.Mode().IsRegular() {
return f, nil
2020-10-11 16:57:04 -06:00
}
}
2021-02-14 17:50:38 -07:00
return nil, os.ErrNotExist
2020-10-11 16:57:04 -06:00
}