2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-11 16:57:04 -06:00
|
|
|
|
|
|
|
import (
|
2021-02-20 13:49:07 -07:00
|
|
|
"context"
|
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
|
|
|
}
|
|
|
|
|
2021-02-20 13:49:07 -07:00
|
|
|
func (fs fileServer) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
|
|
|
serveFile(ctx, w, r, fs, path.Clean(r.URL.Path), true)
|
2020-10-27 11:32:48 -06:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:15:30 -07:00
|
|
|
// ServeContent replies to the request using the content in the
|
|
|
|
// provided Reader. The main benefit of ServeContent over io.Copy
|
|
|
|
// is that it sets the MIME type of the response.
|
|
|
|
//
|
|
|
|
// ServeContent tries to deduce the type from name's file extension.
|
|
|
|
// The name is otherwise unused; it is never sent in the response.
|
2021-02-20 13:49:07 -07:00
|
|
|
func ServeContent(ctx context.Context, w ResponseWriter, r *Request, name string, content io.Reader) {
|
|
|
|
serveContent(ctx, w, name, content)
|
2021-02-17 09:15:30 -07:00
|
|
|
}
|
|
|
|
|
2021-02-20 13:49:07 -07:00
|
|
|
func serveContent(ctx context.Context, w ResponseWriter, name string, content io.Reader) {
|
2021-02-17 09:15:30 -07:00
|
|
|
// Detect mimetype from file extension
|
|
|
|
ext := path.Ext(name)
|
|
|
|
mimetype := mime.TypeByExtension(ext)
|
2021-02-17 11:36:16 -07:00
|
|
|
w.MediaType(mimetype)
|
2021-02-17 09:15:30 -07:00
|
|
|
io.Copy(w, content)
|
|
|
|
}
|
|
|
|
|
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-17 09:17:13 -07:00
|
|
|
//
|
|
|
|
// As a precaution, ServeFile will reject requests where r.URL.Path contains a
|
|
|
|
// ".." path element; this protects against callers who might unsafely use
|
|
|
|
// filepath.Join on r.URL.Path without sanitizing it and then use that
|
|
|
|
// filepath.Join result as the name argument.
|
|
|
|
//
|
|
|
|
// As another special case, ServeFile redirects any request where r.URL.Path
|
|
|
|
// ends in "/index.gmi" to the same path, without the final "index.gmi". To
|
|
|
|
// avoid such redirects either modify the path or use ServeContent.
|
|
|
|
//
|
|
|
|
// Outside of those two special cases, ServeFile does not use r.URL.Path for
|
|
|
|
// selecting the file or directory to serve; only the file or directory
|
|
|
|
// provided in the name argument is used.
|
2021-02-20 13:49:07 -07:00
|
|
|
func ServeFile(ctx context.Context, w ResponseWriter, r *Request, fsys fs.FS, name string) {
|
2021-02-17 09:17:13 -07:00
|
|
|
if containsDotDot(r.URL.Path) {
|
|
|
|
// Too many programs use r.URL.Path to construct the argument to
|
|
|
|
// serveFile. Reject the request under the assumption that happened
|
|
|
|
// here and ".." may not be wanted.
|
|
|
|
// Note that name might not contain "..", for example if code (still
|
|
|
|
// incorrectly) used filepath.Join(myDir, r.URL.Path).
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusBadRequest, "invalid URL path")
|
2021-02-17 09:17:13 -07:00
|
|
|
return
|
|
|
|
}
|
2021-02-20 13:49:07 -07:00
|
|
|
serveFile(ctx, w, r, fsys, name, false)
|
2021-02-16 23:38:18 -07:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:17:13 -07:00
|
|
|
func containsDotDot(v string) bool {
|
|
|
|
if !strings.Contains(v, "..") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, ent := range strings.FieldsFunc(v, isSlashRune) {
|
|
|
|
if ent == ".." {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSlashRune(r rune) bool { return r == '/' || r == '\\' }
|
|
|
|
|
2021-02-20 13:49:07 -07:00
|
|
|
func serveFile(ctx context.Context, w ResponseWriter, r *Request, fsys fs.FS, name string, redirect bool) {
|
2021-02-16 23:38:18 -07:00
|
|
|
const indexPage = "/index.gmi"
|
|
|
|
|
|
|
|
// Redirect .../index.gmi to .../
|
|
|
|
if strings.HasSuffix(r.URL.Path, indexPage) {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusPermanentRedirect, "./")
|
2021-02-16 23:38:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusNotFound, "Not found")
|
2021-02-16 22:03:31 -07:00
|
|
|
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-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
|
2021-02-16 22:03:31 -07:00
|
|
|
return
|
2021-02-14 17:50:38 -07:00
|
|
|
}
|
|
|
|
|
2021-02-16 23:38:18 -07:00
|
|
|
// Redirect to canonical path
|
|
|
|
if redirect {
|
|
|
|
url := r.URL.Path
|
|
|
|
if stat.IsDir() {
|
|
|
|
// Add trailing slash
|
|
|
|
if url[len(url)-1] != '/' {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusPermanentRedirect, path.Base(url)+"/")
|
2021-02-16 23:38:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Remove trailing slash
|
2021-02-17 07:23:58 -07:00
|
|
|
if url[len(url)-1] == '/' {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusPermanentRedirect, "../"+path.Base(url))
|
2021-02-16 23:38:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:50:38 -07:00
|
|
|
if stat.IsDir() {
|
2021-02-16 23:38:18 -07:00
|
|
|
// Redirect if the directory name doesn't end in a slash
|
|
|
|
url := r.URL.Path
|
|
|
|
if url[len(url)-1] != '/' {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusRedirect, path.Base(url)+"/")
|
2021-02-16 23:38:18 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use contents of index.gmi if present
|
|
|
|
index, err := fsys.Open(path.Join(name, indexPage))
|
2021-02-16 22:03:31 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-20 13:49:07 -07:00
|
|
|
serveContent(ctx, w, name, f)
|
2021-02-16 22:03:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusTemporaryFailure, "Error reading directory")
|
2021-02-16 22:03:31 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|