git » gofer » next » tree

[next] / server / dir.go

package server

import (
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"blitiri.com.ar/go/gofer/config"
)

type FileSystem struct {
	fs http.FileSystem

	// On-disk root of the served directory. The PUT/DELETE handlers open it
	// on each request with os.OpenRoot, which confines them to it. Opening it
	// each time means they see the same directory as GET if the path is
	// created or replaced after startup.
	root string

	opts config.DirOpts
}

func NewFS(root string, fs http.FileSystem, opts config.DirOpts) *FileSystem {
	return &FileSystem{
		fs:   fs,
		root: root,
		opts: opts,
	}
}

// prefixMatch returns the value associated with the longest prefix of name in
// m, or false if no prefix matches. Prefixes match on path segment boundaries:
// "/pub/" matches "/pub" and "/pub/x", but not "/public".
func prefixMatch(m map[string]bool, name string) bool {
	if name == "" {
		name = "/"
	}
	name = filepath.Clean(name)

	longestP := ""
	value := false
	for p, val := range m {
		p = filepath.Clean(p)
		if hasPathPrefix(name, p) && len(p) > len(longestP) {
			longestP = p
			value = val
		}
	}

	return value
}

// hasPathPrefix returns true if name is p or is under it. Both must be clean.
func hasPathPrefix(name, p string) bool {
	if p == "/" {
		return strings.HasPrefix(name, "/")
	}
	return name == p || strings.HasPrefix(name, p+"/")
}

func ListingEnabled(opts *config.DirOpts, name string) bool {
	return prefixMatch(opts.Listing, name)
}

func PutEnabled(opts *config.DirOpts, name string) bool {
	return prefixMatch(opts.Put, name)
}

func DeleteEnabled(opts *config.DirOpts, name string) bool {
	return prefixMatch(opts.Delete, name)
}

// relPath converts a clean URL path into a path relative to the root, as
// needed by os.Root.
func relPath(name string) string {
	if name == "/" {
		return "."
	}
	return strings.TrimPrefix(name, "/")
}

func (fs *FileSystem) excluded(name string) bool {
	for _, re := range fs.opts.Exclude {
		if re.MatchString(name) {
			return true
		}
	}
	return false
}

func (fs *FileSystem) Open(name string) (http.File, error) {
	if fs.excluded(name) {
		return nil, os.ErrNotExist
	}

	f, err := fs.fs.Open(name)
	if err != nil {
		return nil, err
	}

	// If it's not a directory, let it be.
	if s, _ := f.Stat(); s == nil || !s.IsDir() {
		return f, nil
	}

	f = wrappedFile{File: f, name: name, opts: &fs.opts}

	if ListingEnabled(&fs.opts, name) {
		return f, nil
	}

	// It's a directory, and listing not allowed.
	// However, if there is an index.html, we let it be served.
	index := filepath.Join(name, "index.html")
	if idxf, err := fs.fs.Open(index); err == nil {
		idxf.Close()
		return f, err
	}

	f.Close()
	return nil, os.ErrNotExist
}

type wrappedFile struct {
	http.File
	name string
	opts *config.DirOpts
}

func (f wrappedFile) Readdir(count int) ([]os.FileInfo, error) {
	if !ListingEnabled(f.opts, f.name) {
		return nil, os.ErrNotExist
	}

	// Exclude files from listings too.
	all, err := f.File.Readdir(count)
	var fis []os.FileInfo
outer:
	for _, fi := range all {
		for _, re := range f.opts.Exclude {
			name := filepath.Join(f.name, fi.Name())
			if re.MatchString(name) {
				continue outer
			}
		}
		fis = append(fis, fi)
	}
	return fis, err
}