Skip to content

Commit

Permalink
Add file_limit option for file_server browse
Browse files Browse the repository at this point in the history
  • Loading branch information
atakanyenel committed Oct 21, 2024
1 parent 9753c44 commit 05261ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
13 changes: 12 additions & 1 deletion modules/caddyhttp/fileserver/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ type Browse struct {
// - `sort size` will sort by size in ascending order
// The first option must be `sort_by` and the second option must be `order` (if exists).
SortOptions []string `json:"sort,omitempty"`

// FileLimit limits the number of up to n DirEntry values in directory order.
FileLimit int `json:"file_limit,omitempty"`
}

const (
defaultMaxDirLimit = 10000
)

func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if c := fsrv.logger.Check(zapcore.DebugLevel, "browse enabled; listing directory contents"); c != nil {
c.Write(zap.String("path", dirPath), zap.String("root", root))
Expand Down Expand Up @@ -206,7 +213,11 @@ func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w ht
}

func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs.FS, dir fs.ReadDirFile, root, urlPath string, repl *caddy.Replacer) (*browseTemplateContext, error) {
files, err := dir.ReadDir(10000) // TODO: this limit should probably be configurable
dirLimit := defaultMaxDirLimit
if fsrv.Browse.FileLimit != 0 {
dirLimit = fsrv.Browse.FileLimit
}
files, err := dir.ReadDir(dirLimit)
if err != nil && err != io.EOF {
return nil, err
}
Expand Down
13 changes: 13 additions & 0 deletions modules/caddyhttp/fileserver/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package fileserver

import (
"path/filepath"
"strconv"
"strings"

"github.com/caddyserver/caddy/v2"
Expand Down Expand Up @@ -58,6 +59,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// browse [<template_file>]
// precompressed <formats...>
// status <status>
// file_limit <limit>
// disable_canonical_uris
// }
//
Expand Down Expand Up @@ -168,6 +170,17 @@ func (fsrv *FileServer) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
falseBool := false
fsrv.CanonicalURIs = &falseBool

case "file_limit":
fileLimit := d.RemainingArgs()
if len(fileLimit) != 1 {
return d.Err("file_limit should have an integer value")
}
val, _ := strconv.Atoi(fileLimit[0])
if fsrv.Browse.FileLimit != 0 {
return d.Err("file_limit is already enabled")
}
fsrv.Browse.FileLimit = val

case "pass_thru":
if d.NextArg() {
return d.ArgErr()
Expand Down
4 changes: 3 additions & 1 deletion modules/caddyhttp/fileserver/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ respond with a file listing.`,
cmd.Flags().BoolP("templates", "t", false, "Enable template rendering")
cmd.Flags().BoolP("access-log", "a", false, "Enable the access log")
cmd.Flags().BoolP("debug", "v", false, "Enable verbose debug logs")
cmd.Flags().IntP("file-limit", "f", defaultMaxDirLimit, "Max directories to read")
cmd.Flags().BoolP("no-compress", "", false, "Disable Zstandard and Gzip compression")
cmd.Flags().StringSliceP("precompressed", "p", []string{}, "Specify precompression file extensions. Compression preference implied from flag order.")
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdFileServer)
Expand All @@ -91,6 +92,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
browse := fs.Bool("browse")
templates := fs.Bool("templates")
accessLog := fs.Bool("access-log")
fileLimit := fs.Int("file-limit")
debug := fs.Bool("debug")
revealSymlinks := fs.Bool("reveal-symlinks")
compress := !fs.Bool("no-compress")
Expand Down Expand Up @@ -151,7 +153,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
}

if browse {
handler.Browse = &Browse{RevealSymlinks: revealSymlinks}
handler.Browse = &Browse{RevealSymlinks: revealSymlinks, FileLimit: fileLimit}
}

handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "file_server", nil))
Expand Down

0 comments on commit 05261ec

Please sign in to comment.