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 48c5441
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
10 changes: 9 additions & 1 deletion modules/caddyhttp/fileserver/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type Browse struct {
SortOptions []string `json:"sort,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 +210,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.FileLimit != 0 {
dirLimit = fsrv.FileLimit
}
files, err := dir.ReadDir(dirLimit)
if err != nil && err != io.EOF {
return nil, err
}
Expand Down
10 changes: 10 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,14 @@ 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])
fsrv.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 @@ -125,7 +127,7 @@ func cmdFileServer(fs caddycmd.Flags) (int, error) {
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "templates", nil))
}

handler := FileServer{Root: root}
handler := FileServer{Root: root, FileLimit: fileLimit}

if len(precompressed) != 0 {
// logic mirrors modules/caddyhttp/fileserver/caddyfile.go case "precompressed"
Expand Down
3 changes: 3 additions & 0 deletions modules/caddyhttp/fileserver/staticfiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ type FileServer struct {
fsmap caddy.FileSystems

logger *zap.Logger

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

// CaddyModule returns the Caddy module information.
Expand Down

0 comments on commit 48c5441

Please sign in to comment.