~emersion/kimchi

354e8fae908846eb55c9615c1e378266e6d3fbec — Simon Ser 11 months ago e1a2275
Disable directory listings by default

The file_server.browse directive allows to explicitly enable them.

Closes: https://todo.sr.ht/~emersion/kimchi/3
2 files changed, 32 insertions(+), 2 deletions(-)

M directives.go
M kimchi.1.scd
M directives.go => directives.go +26 -1
@@ 7,6 7,7 @@ import (
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"
	"path/filepath"
	"strings"



@@ 126,7 127,11 @@ var backends = map[string]parseBackendFunc{
		if err := dir.ParseParams(&dirname); err != nil {
			return nil, err
		}
		return http.FileServer(http.Dir(dirname)), nil
		var fs http.FileSystem = http.Dir(dirname)
		if dir.Children.Get("browse") == nil {
			fs = noBrowseFileSystem{fs}
		}
		return http.FileServer(fs), nil
	},
	"reverse_proxy": func(dir *scfg.Directive) (http.Handler, error) {
		var urlStr string


@@ 250,3 255,23 @@ func resolveImports(input scfg.Block, filename string) (scfg.Block, error) {

	return output, nil
}

type noBrowseFileSystem struct {
	http.FileSystem
}

func (fs noBrowseFileSystem) Open(name string) (http.File, error) {
	f, err := fs.FileSystem.Open(name)
	if err != nil {
		return nil, err
	}
	return noBrowseFile{f}, nil
}

type noBrowseFile struct {
	http.File
}

func (f noBrowseFile) Readdir(count int) ([]os.FileInfo, error) {
	return nil, os.ErrPermission
}

M kimchi.1.scd => kimchi.1.scd +6 -1
@@ 67,9 67,14 @@ The following directives are supported:
	*reverse_proxy* <uri>
		Forward incoming requests to another HTTP server.

	*file_server* <path>
	*file_server* <path> { ++
	*browse* ++
}
		Serve static files at the specified path.

		_browse_ enables file listings for directories that do not have an
		index file.

	*header* <key> <value> ++
*header* { ++
	<key> <value> ++