1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-04 02:36:12 +02:00
go-gemini/fs.go

185 lines
3.6 KiB
Go
Raw Permalink Normal View History

2020-10-24 21:15:32 +02:00
package gemini
2020-10-12 00:57:04 +02:00
import (
"context"
2021-02-21 15:56:59 +01:00
"errors"
"fmt"
2020-10-12 00:57:04 +02:00
"io"
2021-02-17 00:53:41 +01:00
"io/fs"
2020-10-12 02:13:53 +02:00
"mime"
"net/url"
2020-10-12 00:57:04 +02:00
"path"
"sort"
"strings"
2020-10-12 00:57:04 +02:00
)
2021-02-15 01:50:38 +01:00
// FileServer returns a handler that serves Gemini requests with the contents
// of the provided file system.
//
2021-02-17 00:53:41 +01:00
// To use the operating system's file system implementation, use os.DirFS:
2021-02-15 01:50:38 +01:00
//
2023-06-29 07:50:57 +02:00
// gemini.FileServer(os.DirFS("/tmp"))
2021-02-17 00:53:41 +01:00
func FileServer(fsys fs.FS) Handler {
2021-02-15 01:50:38 +01:00
return fileServer{fsys}
}
type fileServer struct {
2021-02-17 00:53:41 +01:00
fs.FS
2021-02-15 01:50:38 +01:00
}
2021-04-21 18:14:56 +02:00
func (fsys fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
2021-02-17 07:38:18 +01:00
const indexPage = "/index.gmi"
2021-04-21 18:41:56 +02:00
url := path.Clean(r.URL.Path)
2021-02-17 07:38:18 +01:00
// Redirect .../index.gmi to .../
2021-04-21 18:41:56 +02:00
if strings.HasSuffix(url, indexPage) {
w.WriteHeader(StatusPermanentRedirect, strings.TrimSuffix(url, "index.gmi"))
2021-02-17 07:38:18 +01:00
return
}
2021-04-21 18:41:56 +02:00
name := url
if name == "/" {
name = "."
} else {
2021-04-21 18:41:56 +02:00
name = strings.TrimPrefix(name, "/")
}
2021-02-15 01:50:38 +01:00
f, err := fsys.Open(name)
2020-10-12 00:57:04 +02:00
if err != nil {
2021-02-21 15:56:59 +01:00
w.WriteHeader(toGeminiError(err))
return
2020-10-12 00:57:04 +02:00
}
defer f.Close()
2020-10-12 00:57:04 +02:00
2021-02-15 01:50:38 +01:00
stat, err := f.Stat()
if err != nil {
2021-02-21 15:56:59 +01:00
w.WriteHeader(toGeminiError(err))
return
2021-02-15 01:50:38 +01:00
}
2021-02-17 07:38:18 +01:00
// Redirect to canonical path
2021-04-21 18:41:56 +02:00
if len(r.URL.Path) != 0 {
2021-02-17 07:38:18 +01:00
if stat.IsDir() {
2021-04-21 18:41:56 +02:00
target := url
if target != "/" {
target += "/"
}
if len(r.URL.Path) != len(target) || r.URL.Path != target {
2021-04-21 18:41:56 +02:00
w.WriteHeader(StatusPermanentRedirect, target)
2021-02-17 07:38:18 +01:00
return
}
2021-04-21 18:41:56 +02:00
} else if r.URL.Path[len(r.URL.Path)-1] == '/' {
2021-02-17 07:38:18 +01:00
// Remove trailing slash
2021-04-21 18:41:56 +02:00
w.WriteHeader(StatusPermanentRedirect, url)
2021-04-21 18:14:56 +02:00
return
2021-02-17 07:38:18 +01:00
}
}
2021-02-15 01:50:38 +01:00
if stat.IsDir() {
2021-02-17 07:38:18 +01:00
// Use contents of index.gmi if present
name = path.Join(name, indexPage)
index, err := fsys.Open(name)
if err == nil {
defer index.Close()
2021-04-21 18:14:56 +02:00
f = index
} else {
// Failed to find index file
dirList(w, f)
return
2021-02-15 01:50:38 +01:00
}
}
2021-04-21 18:14:56 +02:00
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(mimetype)
io.Copy(w, f)
}
// ServeFile responds to the request with the contents of the named file
2021-04-21 18:41:56 +02:00
// or directory. If the provided name is constructed from user input, it
// should be sanitized before calling ServeFile.
2021-04-21 18:14:56 +02:00
func ServeFile(w ResponseWriter, fsys fs.FS, name string) {
const indexPage = "/index.gmi"
// Ensure name is relative
if name == "/" {
name = "."
} else {
name = strings.TrimLeft(name, "/")
}
f, err := fsys.Open(name)
if err != nil {
w.WriteHeader(toGeminiError(err))
return
}
2021-04-21 18:14:56 +02:00
defer f.Close()
stat, err := f.Stat()
if err != nil {
w.WriteHeader(toGeminiError(err))
return
}
if stat.IsDir() {
// Use contents of index file if present
name = path.Join(name, indexPage)
index, err := fsys.Open(name)
if err == nil {
defer index.Close()
f = index
} else {
// Failed to find index file
dirList(w, f)
return
}
}
2021-04-21 17:41:40 +02:00
// Detect mimetype from file extension
ext := path.Ext(name)
mimetype := mime.TypeByExtension(ext)
w.SetMediaType(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 {
2021-02-17 19:36:16 +01:00
w.WriteHeader(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-15 01:50:38 +01:00
}
link := LineLink{
Name: name,
URL: "./" + url.PathEscape(name),
2020-10-12 00:57:04 +02:00
}
fmt.Fprintln(w, link.String())
2020-10-12 00:57:04 +02:00
}
}
2021-02-21 15:56:59 +01:00
func toGeminiError(err error) (status Status, meta string) {
if errors.Is(err, fs.ErrNotExist) {
return StatusNotFound, "Not found"
}
if errors.Is(err, fs.ErrPermission) {
return StatusNotFound, "Forbidden"
}
return StatusTemporaryFailure, "Internal server error"
}