2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-11 16:57:04 -06:00
|
|
|
|
|
|
|
import (
|
2021-02-16 22:03:31 -07:00
|
|
|
"fmt"
|
2020-10-11 16:57:04 -06:00
|
|
|
"io"
|
2021-02-16 16:53:41 -07:00
|
|
|
"io/fs"
|
2020-10-11 18:13:53 -06:00
|
|
|
"mime"
|
2021-02-16 22:03:31 -07:00
|
|
|
"net/url"
|
2020-10-11 16:57:04 -06:00
|
|
|
"path"
|
2021-02-16 22:03:31 -07:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2020-10-11 16:57:04 -06:00
|
|
|
)
|
|
|
|
|
2020-10-11 18:13:53 -06:00
|
|
|
func init() {
|
|
|
|
// Add Gemini mime types
|
2021-01-14 20:25:07 -07:00
|
|
|
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
|
|
|
// FileServer returns a handler that serves Gemini requests with the contents
|
|
|
|
// of the provided file system.
|
|
|
|
//
|
2021-02-16 16:53:41 -07:00
|
|
|
// To use the operating system's file system implementation, use os.DirFS:
|
2021-02-14 17:50:38 -07:00
|
|
|
//
|
2021-02-16 16:53:41 -07:00
|
|
|
// gemini.FileServer(os.DirFS("/tmp"))
|
|
|
|
func FileServer(fsys fs.FS) Handler {
|
2021-02-14 17:50:38 -07:00
|
|
|
return fileServer{fsys}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileServer struct {
|
2021-02-16 16:53:41 -07:00
|
|
|
fs.FS
|
2021-02-14 17:50:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs fileServer) ServeGemini(w ResponseWriter, r *Request) {
|
2021-02-16 22:57:44 -07:00
|
|
|
ServeFile(w, fs, path.Clean(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.
|
2021-02-16 16:53:41 -07:00
|
|
|
func ServeFile(w ResponseWriter, fsys fs.FS, name string) {
|
2021-02-16 21:18:30 -07:00
|
|
|
if name == "/" {
|
|
|
|
name = "."
|
|
|
|
} else {
|
2021-02-16 23:36:53 -07:00
|
|
|
name = strings.Trim(name, "/")
|
2021-02-16 21:18:30 -07:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:50:38 -07:00
|
|
|
f, err := fsys.Open(name)
|
2020-10-11 16:57:04 -06:00
|
|
|
if err != nil {
|
2021-02-16 22:03:31 -07:00
|
|
|
w.Status(StatusNotFound)
|
|
|
|
return
|
2020-10-11 16:57:04 -06:00
|
|
|
}
|
2021-02-16 22:03:31 -07:00
|
|
|
defer f.Close()
|
2020-10-11 16:57:04 -06:00
|
|
|
|
2021-02-14 17:50:38 -07:00
|
|
|
stat, err := f.Stat()
|
|
|
|
if err != nil {
|
2021-02-16 22:03:31 -07:00
|
|
|
w.Status(StatusTemporaryFailure)
|
|
|
|
return
|
2021-02-14 17:50:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
2021-02-16 22:03:31 -07:00
|
|
|
// Try opening index file
|
|
|
|
index, err := fsys.Open(path.Join(name, "index.gmi"))
|
|
|
|
if err == nil {
|
|
|
|
defer index.Close()
|
|
|
|
istat, err := index.Stat()
|
|
|
|
if err == nil {
|
|
|
|
f = index
|
|
|
|
stat = istat
|
|
|
|
}
|
2021-02-14 17:50:38 -07:00
|
|
|
}
|
2021-02-16 22:03:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if stat.IsDir() {
|
|
|
|
// Failed to find index file
|
|
|
|
dirList(w, f)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect mimetype from file extension
|
|
|
|
ext := path.Ext(name)
|
|
|
|
mimetype := mime.TypeByExtension(ext)
|
|
|
|
w.Meta(mimetype)
|
|
|
|
io.Copy(w, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dirList(w ResponseWriter, f fs.File) {
|
|
|
|
var entries []fs.DirEntry
|
|
|
|
var err error
|
|
|
|
d, ok := f.(fs.ReadDirFile)
|
|
|
|
if ok {
|
|
|
|
entries, err = d.ReadDir(-1)
|
|
|
|
}
|
|
|
|
if !ok || err != nil {
|
|
|
|
w.Header(StatusTemporaryFailure, "Error reading directory")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
|
|
return entries[i].Name() < entries[j].Name()
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
name := entry.Name()
|
|
|
|
if entry.IsDir() {
|
|
|
|
name += "/"
|
2021-02-14 17:50:38 -07:00
|
|
|
}
|
2021-02-16 22:03:31 -07:00
|
|
|
link := LineLink{
|
|
|
|
Name: name,
|
|
|
|
URL: (&url.URL{Path: name}).EscapedPath(),
|
2020-10-11 16:57:04 -06:00
|
|
|
}
|
2021-02-16 22:03:31 -07:00
|
|
|
fmt.Fprintln(w, link.String())
|
2020-10-11 16:57:04 -06:00
|
|
|
}
|
|
|
|
}
|