2019-07-01 01:07:58 +03:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-21 06:21:33 +03:00
|
|
|
package fileserver
|
2019-05-20 19:59:20 +03:00
|
|
|
|
|
|
|
import (
|
2019-05-21 00:46:34 +03:00
|
|
|
"bytes"
|
2022-10-08 21:56:35 +03:00
|
|
|
"context"
|
2021-11-15 21:53:54 +03:00
|
|
|
_ "embed"
|
2019-05-21 00:46:34 +03:00
|
|
|
"encoding/json"
|
2024-01-02 08:48:55 +03:00
|
|
|
"errors"
|
2021-05-01 05:17:23 +03:00
|
|
|
"fmt"
|
2022-09-08 06:14:11 +03:00
|
|
|
"io"
|
2022-07-30 22:07:44 +03:00
|
|
|
"io/fs"
|
2019-05-20 19:59:20 +03:00
|
|
|
"net/http"
|
2019-05-21 00:46:34 +03:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2021-06-16 23:28:34 +03:00
|
|
|
"sync"
|
2024-04-01 21:12:40 +03:00
|
|
|
"text/tabwriter"
|
2021-05-01 05:17:23 +03:00
|
|
|
"text/template"
|
2024-04-18 04:12:03 +03:00
|
|
|
"time"
|
2019-05-21 00:46:34 +03:00
|
|
|
|
2023-08-14 18:41:15 +03:00
|
|
|
"go.uber.org/zap"
|
2024-09-13 20:16:37 +03:00
|
|
|
"go.uber.org/zap/zapcore"
|
2023-08-14 18:41:15 +03:00
|
|
|
|
2019-07-02 21:37:06 +03:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2021-05-01 05:17:23 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/templates"
|
2019-05-20 19:59:20 +03:00
|
|
|
)
|
|
|
|
|
2023-08-29 18:34:20 +03:00
|
|
|
// BrowseTemplate is the default template document to use for
|
|
|
|
// file listings. By default, its default value is an embedded
|
|
|
|
// document. You can override this value at program start, or
|
|
|
|
// if you are running Caddy via config, you can specify a
|
|
|
|
// custom template_file in the browse configuration.
|
|
|
|
//
|
2021-11-15 21:53:54 +03:00
|
|
|
//go:embed browse.html
|
2023-08-29 18:34:20 +03:00
|
|
|
var BrowseTemplate string
|
2021-11-15 21:53:54 +03:00
|
|
|
|
2019-05-20 19:59:20 +03:00
|
|
|
// Browse configures directory browsing.
|
|
|
|
type Browse struct {
|
2023-08-29 18:34:20 +03:00
|
|
|
// Filename of the template to use instead of the embedded browse template.
|
2019-05-22 21:32:36 +03:00
|
|
|
TemplateFile string `json:"template_file,omitempty"`
|
2024-08-30 18:01:37 +03:00
|
|
|
|
2024-02-06 07:31:26 +03:00
|
|
|
// Determines whether or not targets of symlinks should be revealed.
|
|
|
|
RevealSymlinks bool `json:"reveal_symlinks,omitempty"`
|
2024-08-30 18:01:37 +03:00
|
|
|
|
|
|
|
// Override the default sort.
|
|
|
|
// It includes the following options:
|
|
|
|
// - sort_by: name(default), namedirfirst, size, time
|
|
|
|
// - order: asc(default), desc
|
|
|
|
// eg.:
|
|
|
|
// - `sort time desc` will sort by time in descending order
|
|
|
|
// - `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"`
|
2024-11-05 19:35:32 +03:00
|
|
|
|
|
|
|
// FileLimit limits the number of up to n DirEntry values in directory order.
|
|
|
|
FileLimit int `json:"file_limit,omitempty"`
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
2019-05-20 19:59:20 +03:00
|
|
|
|
2024-11-05 19:35:32 +03:00
|
|
|
const (
|
|
|
|
defaultDirEntryLimit = 10000
|
|
|
|
)
|
|
|
|
|
2024-01-13 23:12:43 +03:00
|
|
|
func (fsrv *FileServer) serveBrowse(fileSystem fs.FS, root, dirPath string, w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
|
2024-09-13 20:16:37 +03:00
|
|
|
if c := fsrv.logger.Check(zapcore.DebugLevel, "browse enabled; listing directory contents"); c != nil {
|
|
|
|
c.Write(zap.String("path", dirPath), zap.String("root", root))
|
|
|
|
}
|
2020-11-26 19:37:42 +03:00
|
|
|
|
fileserver: Only redirect if filename not rewritten (fix #4205)
This is the more correct implementation of 23dadc0d86dd75dad7559c25f20c9641bc7bc30f (#4179)... I think. This commit effectively undoes the revert in 8848df9c5d372a559d01512b7a4ef00e38867b55, but with corrections to the logic.
We *do* need to use the original request path (the path the browser knows) for redirects, since they are external, and rewrites are only internal.
However, if the path was rewritten to a non-canonical path, we should not redirect to canonicalize that, since rewrites are intentional by the site owner. Canonicalizing the path involves modifying only the suffix (base element, or filename) of the path. Thus, if a rewrite involves only the prefix (like how handle_path strips a path prefix), then we can (hopefully!) safely redirect using the original URI since the filename was not rewritten.
So basically, if rewrites modify the filename, we should not canonicalize those requests. If rewrites only modify another part of the path (commonly a prefix), we should be OK to redirect.
2021-06-17 18:55:49 +03:00
|
|
|
// Navigation on the client-side gets messed up if the
|
|
|
|
// URL doesn't end in a trailing slash because hrefs to
|
|
|
|
// "b/c" at path "/a" end up going to "/b/c" instead
|
2019-05-21 22:03:52 +03:00
|
|
|
// of "/a/b/c" - so we have to redirect in this case
|
fileserver: Only redirect if filename not rewritten (fix #4205)
This is the more correct implementation of 23dadc0d86dd75dad7559c25f20c9641bc7bc30f (#4179)... I think. This commit effectively undoes the revert in 8848df9c5d372a559d01512b7a4ef00e38867b55, but with corrections to the logic.
We *do* need to use the original request path (the path the browser knows) for redirects, since they are external, and rewrites are only internal.
However, if the path was rewritten to a non-canonical path, we should not redirect to canonicalize that, since rewrites are intentional by the site owner. Canonicalizing the path involves modifying only the suffix (base element, or filename) of the path. Thus, if a rewrite involves only the prefix (like how handle_path strips a path prefix), then we can (hopefully!) safely redirect using the original URI since the filename was not rewritten.
So basically, if rewrites modify the filename, we should not canonicalize those requests. If rewrites only modify another part of the path (commonly a prefix), we should be OK to redirect.
2021-06-17 18:55:49 +03:00
|
|
|
// so that the path is "/a/" and the client constructs
|
|
|
|
// relative hrefs "b/c" to be "/a/b/c".
|
|
|
|
//
|
|
|
|
// Only redirect if the last element of the path (the filename) was not
|
|
|
|
// rewritten; if the admin wanted to rewrite to the canonical path, they
|
|
|
|
// would have, and we have to be very careful not to introduce unwanted
|
|
|
|
// redirects and especially redirect loops! (Redirecting using the
|
|
|
|
// original URI is necessary because that's the URI the browser knows,
|
|
|
|
// we don't want to redirect from internally-rewritten URIs.)
|
|
|
|
// See https://github.com/caddyserver/caddy/issues/4205.
|
2022-03-02 01:32:39 +03:00
|
|
|
// We also redirect if the path is empty, because this implies the path
|
|
|
|
// prefix was fully stripped away by a `handle_path` handler for example.
|
|
|
|
// See https://github.com/caddyserver/caddy/issues/4466.
|
fileserver: Only redirect if filename not rewritten (fix #4205)
This is the more correct implementation of 23dadc0d86dd75dad7559c25f20c9641bc7bc30f (#4179)... I think. This commit effectively undoes the revert in 8848df9c5d372a559d01512b7a4ef00e38867b55, but with corrections to the logic.
We *do* need to use the original request path (the path the browser knows) for redirects, since they are external, and rewrites are only internal.
However, if the path was rewritten to a non-canonical path, we should not redirect to canonicalize that, since rewrites are intentional by the site owner. Canonicalizing the path involves modifying only the suffix (base element, or filename) of the path. Thus, if a rewrite involves only the prefix (like how handle_path strips a path prefix), then we can (hopefully!) safely redirect using the original URI since the filename was not rewritten.
So basically, if rewrites modify the filename, we should not canonicalize those requests. If rewrites only modify another part of the path (commonly a prefix), we should be OK to redirect.
2021-06-17 18:55:49 +03:00
|
|
|
origReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
|
2022-03-02 01:32:39 +03:00
|
|
|
if r.URL.Path == "" || path.Base(origReq.URL.Path) == path.Base(r.URL.Path) {
|
fileserver: Only redirect if filename not rewritten (fix #4205)
This is the more correct implementation of 23dadc0d86dd75dad7559c25f20c9641bc7bc30f (#4179)... I think. This commit effectively undoes the revert in 8848df9c5d372a559d01512b7a4ef00e38867b55, but with corrections to the logic.
We *do* need to use the original request path (the path the browser knows) for redirects, since they are external, and rewrites are only internal.
However, if the path was rewritten to a non-canonical path, we should not redirect to canonicalize that, since rewrites are intentional by the site owner. Canonicalizing the path involves modifying only the suffix (base element, or filename) of the path. Thus, if a rewrite involves only the prefix (like how handle_path strips a path prefix), then we can (hopefully!) safely redirect using the original URI since the filename was not rewritten.
So basically, if rewrites modify the filename, we should not canonicalize those requests. If rewrites only modify another part of the path (commonly a prefix), we should be OK to redirect.
2021-06-17 18:55:49 +03:00
|
|
|
if !strings.HasSuffix(origReq.URL.Path, "/") {
|
2024-09-13 20:16:37 +03:00
|
|
|
if c := fsrv.logger.Check(zapcore.DebugLevel, "redirecting to trailing slash to preserve hrefs"); c != nil {
|
|
|
|
c.Write(zap.String("request_path", r.URL.Path))
|
|
|
|
}
|
2022-07-07 23:10:19 +03:00
|
|
|
return redirect(w, r, origReq.URL.Path+"/")
|
fileserver: Only redirect if filename not rewritten (fix #4205)
This is the more correct implementation of 23dadc0d86dd75dad7559c25f20c9641bc7bc30f (#4179)... I think. This commit effectively undoes the revert in 8848df9c5d372a559d01512b7a4ef00e38867b55, but with corrections to the logic.
We *do* need to use the original request path (the path the browser knows) for redirects, since they are external, and rewrites are only internal.
However, if the path was rewritten to a non-canonical path, we should not redirect to canonicalize that, since rewrites are intentional by the site owner. Canonicalizing the path involves modifying only the suffix (base element, or filename) of the path. Thus, if a rewrite involves only the prefix (like how handle_path strips a path prefix), then we can (hopefully!) safely redirect using the original URI since the filename was not rewritten.
So basically, if rewrites modify the filename, we should not canonicalize those requests. If rewrites only modify another part of the path (commonly a prefix), we should be OK to redirect.
2021-06-17 18:55:49 +03:00
|
|
|
}
|
2019-05-21 22:03:52 +03:00
|
|
|
}
|
|
|
|
|
2024-01-13 23:12:43 +03:00
|
|
|
dir, err := fsrv.openFile(fileSystem, dirPath, w)
|
2019-05-21 00:46:34 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
2019-12-29 23:12:52 +03:00
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-05-21 00:46:34 +03:00
|
|
|
|
2023-05-15 19:48:05 +03:00
|
|
|
// TODO: not entirely sure if path.Clean() is necessary here but seems like a safe plan (i.e. /%2e%2e%2f) - someone could verify this
|
2024-01-13 23:12:43 +03:00
|
|
|
listing, err := fsrv.loadDirectoryContents(r.Context(), fileSystem, dir.(fs.ReadDirFile), root, path.Clean(r.URL.EscapedPath()), repl)
|
2019-05-21 00:46:34 +03:00
|
|
|
switch {
|
2024-01-02 08:48:55 +03:00
|
|
|
case errors.Is(err, fs.ErrPermission):
|
2019-05-21 00:46:34 +03:00
|
|
|
return caddyhttp.Error(http.StatusForbidden, err)
|
2024-01-02 08:48:55 +03:00
|
|
|
case errors.Is(err, fs.ErrNotExist):
|
2019-11-16 03:32:13 +03:00
|
|
|
return fsrv.notFound(w, r, next)
|
2019-05-21 00:46:34 +03:00
|
|
|
case err != nil:
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
2024-04-19 22:43:13 +03:00
|
|
|
w.Header().Add("Vary", "Accept, Accept-Encoding")
|
2024-04-18 04:12:03 +03:00
|
|
|
|
|
|
|
// speed up browser/client experience and caching by supporting If-Modified-Since
|
|
|
|
if ifModSinceStr := r.Header.Get("If-Modified-Since"); ifModSinceStr != "" {
|
|
|
|
ifModSince, err := time.ParseInLocation(http.TimeFormat, ifModSinceStr, time.Local)
|
|
|
|
lastModTrunc := listing.lastModified.Truncate(time.Second)
|
|
|
|
if err == nil && (lastModTrunc.Equal(ifModSince) || lastModTrunc.Before(ifModSince)) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-19 18:59:40 +03:00
|
|
|
fsrv.browseApplyQueryParams(w, r, listing)
|
2019-05-21 00:46:34 +03:00
|
|
|
|
2021-06-16 23:28:34 +03:00
|
|
|
buf := bufPool.Get().(*bytes.Buffer)
|
2022-08-16 07:31:45 +03:00
|
|
|
buf.Reset()
|
2021-06-16 23:28:34 +03:00
|
|
|
defer bufPool.Put(buf)
|
|
|
|
|
2019-05-21 00:46:34 +03:00
|
|
|
acceptHeader := strings.ToLower(strings.Join(r.Header["Accept"], ","))
|
2024-04-18 04:12:03 +03:00
|
|
|
w.Header().Set("Last-Modified", listing.lastModified.Format(http.TimeFormat))
|
2021-06-16 23:28:34 +03:00
|
|
|
|
2024-04-01 21:12:40 +03:00
|
|
|
switch {
|
|
|
|
case strings.Contains(acceptHeader, "application/json"):
|
2021-06-16 23:28:34 +03:00
|
|
|
if err := json.NewEncoder(buf).Encode(listing.Items); err != nil {
|
2019-05-21 00:46:34 +03:00
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2024-04-01 21:12:40 +03:00
|
|
|
|
|
|
|
case strings.Contains(acceptHeader, "text/plain"):
|
|
|
|
writer := tabwriter.NewWriter(buf, 0, 8, 1, '\t', tabwriter.AlignRight)
|
|
|
|
|
|
|
|
// Header on top
|
|
|
|
if _, err := fmt.Fprintln(writer, "Name\tSize\tModified"); err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lines to separate the header
|
|
|
|
if _, err := fmt.Fprintln(writer, "----\t----\t--------"); err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Actual files
|
|
|
|
for _, item := range listing.Items {
|
|
|
|
if _, err := fmt.Fprintf(writer, "%s\t%s\t%s\n",
|
|
|
|
item.Name, item.HumanSize(), item.HumanModTime("January 2, 2006 at 15:04:05"),
|
|
|
|
); err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
|
|
|
default:
|
2021-05-01 05:17:23 +03:00
|
|
|
var fs http.FileSystem
|
|
|
|
if fsrv.Root != "" {
|
|
|
|
fs = http.Dir(repl.ReplaceAll(fsrv.Root, "."))
|
|
|
|
}
|
|
|
|
|
2023-08-07 22:40:31 +03:00
|
|
|
tplCtx := &templateContext{
|
2021-05-01 05:17:23 +03:00
|
|
|
TemplateContext: templates.TemplateContext{
|
|
|
|
Root: fs,
|
|
|
|
Req: r,
|
|
|
|
RespHeader: templates.WrappedHeader{Header: w.Header()},
|
|
|
|
},
|
|
|
|
browseTemplateContext: listing,
|
|
|
|
}
|
|
|
|
|
2021-06-16 23:28:34 +03:00
|
|
|
tpl, err := fsrv.makeBrowseTemplate(tplCtx)
|
2021-05-01 05:17:23 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing browse template: %v", err)
|
|
|
|
}
|
2021-06-16 23:28:34 +03:00
|
|
|
if err := tpl.Execute(buf, tplCtx); err != nil {
|
2019-05-21 00:46:34 +03:00
|
|
|
return caddyhttp.Error(http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
}
|
2019-06-21 23:36:26 +03:00
|
|
|
|
2020-11-23 00:50:29 +03:00
|
|
|
_, _ = buf.WriteTo(w)
|
2019-05-20 19:59:20 +03:00
|
|
|
|
2019-05-21 00:46:34 +03:00
|
|
|
return nil
|
2019-05-20 19:59:20 +03:00
|
|
|
}
|
|
|
|
|
2024-01-13 23:12:43 +03:00
|
|
|
func (fsrv *FileServer) loadDirectoryContents(ctx context.Context, fileSystem fs.FS, dir fs.ReadDirFile, root, urlPath string, repl *caddy.Replacer) (*browseTemplateContext, error) {
|
2024-11-05 19:35:32 +03:00
|
|
|
dirLimit := defaultDirEntryLimit
|
|
|
|
if fsrv.Browse.FileLimit != 0 {
|
|
|
|
dirLimit = fsrv.Browse.FileLimit
|
|
|
|
}
|
|
|
|
files, err := dir.ReadDir(dirLimit)
|
2022-09-08 06:14:11 +03:00
|
|
|
if err != nil && err != io.EOF {
|
2023-05-19 18:59:40 +03:00
|
|
|
return nil, err
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 18:03:33 +03:00
|
|
|
// user can presumably browse "up" to parent folder if path is longer than "/"
|
|
|
|
canGoUp := len(urlPath) > 1
|
2019-05-21 00:46:34 +03:00
|
|
|
|
2024-01-13 23:12:43 +03:00
|
|
|
return fsrv.directoryListing(ctx, fileSystem, files, canGoUp, root, urlPath, repl), nil
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// browseApplyQueryParams applies query parameters to the listing.
|
|
|
|
// It mutates the listing and may set cookies.
|
2021-05-01 05:17:23 +03:00
|
|
|
func (fsrv *FileServer) browseApplyQueryParams(w http.ResponseWriter, r *http.Request, listing *browseTemplateContext) {
|
2024-08-05 17:27:45 +03:00
|
|
|
var orderParam, sortParam string
|
|
|
|
|
|
|
|
// The configs in Caddyfile have lower priority than Query params,
|
|
|
|
// so put it at first.
|
2024-08-30 18:01:37 +03:00
|
|
|
for idx, item := range fsrv.Browse.SortOptions {
|
2024-08-05 17:27:45 +03:00
|
|
|
// Only `sort` & `order`, 2 params are allowed
|
|
|
|
if idx >= 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
switch item {
|
|
|
|
case sortByName, sortByNameDirFirst, sortBySize, sortByTime:
|
|
|
|
sortParam = item
|
|
|
|
case sortOrderAsc, sortOrderDesc:
|
|
|
|
orderParam = item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 21:19:31 +03:00
|
|
|
layoutParam := r.URL.Query().Get("layout")
|
2019-05-21 00:46:34 +03:00
|
|
|
limitParam := r.URL.Query().Get("limit")
|
2020-07-09 08:56:15 +03:00
|
|
|
offsetParam := r.URL.Query().Get("offset")
|
2024-08-05 17:27:45 +03:00
|
|
|
sortParamTmp := r.URL.Query().Get("sort")
|
|
|
|
if sortParamTmp != "" {
|
|
|
|
sortParam = sortParamTmp
|
|
|
|
}
|
|
|
|
orderParamTmp := r.URL.Query().Get("order")
|
|
|
|
if orderParamTmp != "" {
|
|
|
|
orderParam = orderParamTmp
|
|
|
|
}
|
2019-05-21 00:46:34 +03:00
|
|
|
|
2023-03-10 21:19:31 +03:00
|
|
|
switch layoutParam {
|
|
|
|
case "list", "grid", "":
|
|
|
|
listing.Layout = layoutParam
|
|
|
|
default:
|
|
|
|
listing.Layout = "list"
|
|
|
|
}
|
|
|
|
|
|
|
|
// figure out what to sort by
|
2019-05-21 00:46:34 +03:00
|
|
|
switch sortParam {
|
|
|
|
case "":
|
|
|
|
sortParam = sortByNameDirFirst
|
|
|
|
if sortCookie, sortErr := r.Cookie("sort"); sortErr == nil {
|
|
|
|
sortParam = sortCookie.Value
|
|
|
|
}
|
|
|
|
case sortByName, sortByNameDirFirst, sortBySize, sortByTime:
|
|
|
|
http.SetCookie(w, &http.Cookie{Name: "sort", Value: sortParam, Secure: r.TLS != nil})
|
|
|
|
}
|
|
|
|
|
|
|
|
// then figure out the order
|
|
|
|
switch orderParam {
|
|
|
|
case "":
|
2024-08-05 17:27:45 +03:00
|
|
|
orderParam = sortOrderAsc
|
2019-05-21 00:46:34 +03:00
|
|
|
if orderCookie, orderErr := r.Cookie("order"); orderErr == nil {
|
|
|
|
orderParam = orderCookie.Value
|
|
|
|
}
|
2024-08-05 17:27:45 +03:00
|
|
|
case sortOrderAsc, sortOrderDesc:
|
2019-05-21 00:46:34 +03:00
|
|
|
http.SetCookie(w, &http.Cookie{Name: "order", Value: orderParam, Secure: r.TLS != nil})
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally, apply the sorting and limiting
|
2020-07-09 08:56:15 +03:00
|
|
|
listing.applySortAndLimit(sortParam, orderParam, limitParam, offsetParam)
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
|
|
|
|
2021-05-01 05:17:23 +03:00
|
|
|
// makeBrowseTemplate creates the template to be used for directory listings.
|
2021-06-16 23:28:34 +03:00
|
|
|
func (fsrv *FileServer) makeBrowseTemplate(tplCtx *templateContext) (*template.Template, error) {
|
2021-05-01 05:17:23 +03:00
|
|
|
var tpl *template.Template
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if fsrv.Browse.TemplateFile != "" {
|
|
|
|
tpl = tplCtx.NewTemplate(path.Base(fsrv.Browse.TemplateFile))
|
|
|
|
tpl, err = tpl.ParseFiles(fsrv.Browse.TemplateFile)
|
|
|
|
if err != nil {
|
2021-06-16 23:28:34 +03:00
|
|
|
return nil, fmt.Errorf("parsing browse template file: %v", err)
|
2021-05-01 05:17:23 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tpl = tplCtx.NewTemplate("default_listing")
|
2023-08-29 18:34:20 +03:00
|
|
|
tpl, err = tpl.Parse(BrowseTemplate)
|
2021-05-01 05:17:23 +03:00
|
|
|
if err != nil {
|
2021-06-16 23:28:34 +03:00
|
|
|
return nil, fmt.Errorf("parsing default browse template: %v", err)
|
2021-05-01 05:17:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 23:28:34 +03:00
|
|
|
return tpl, nil
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
|
|
|
|
2019-05-21 22:03:52 +03:00
|
|
|
// isSymlinkTargetDir returns true if f's symbolic link target
|
|
|
|
// is a directory.
|
2024-01-13 23:12:43 +03:00
|
|
|
func (fsrv *FileServer) isSymlinkTargetDir(fileSystem fs.FS, f fs.FileInfo, root, urlPath string) bool {
|
2019-05-21 22:03:52 +03:00
|
|
|
if !isSymlink(f) {
|
|
|
|
return false
|
|
|
|
}
|
2021-06-17 18:59:08 +03:00
|
|
|
target := caddyhttp.SanitizedPathJoin(root, path.Join(urlPath, f.Name()))
|
2024-01-13 23:12:43 +03:00
|
|
|
targetInfo, err := fs.Stat(fileSystem, target)
|
2019-05-21 22:03:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return targetInfo.IsDir()
|
2019-05-21 00:46:34 +03:00
|
|
|
}
|
2021-05-01 05:17:23 +03:00
|
|
|
|
2022-07-30 22:07:44 +03:00
|
|
|
// isSymlink return true if f is a symbolic link.
|
|
|
|
func isSymlink(f fs.FileInfo) bool {
|
|
|
|
return f.Mode()&os.ModeSymlink != 0
|
|
|
|
}
|
|
|
|
|
2021-05-01 05:17:23 +03:00
|
|
|
// templateContext powers the context used when evaluating the browse template.
|
|
|
|
// It combines browse-specific features with the standard templates handler
|
|
|
|
// features.
|
|
|
|
type templateContext struct {
|
|
|
|
templates.TemplateContext
|
2023-05-19 18:59:40 +03:00
|
|
|
*browseTemplateContext
|
2021-05-01 05:17:23 +03:00
|
|
|
}
|
2021-06-16 23:28:34 +03:00
|
|
|
|
|
|
|
// bufPool is used to increase the efficiency of file listings.
|
|
|
|
var bufPool = sync.Pool{
|
2022-08-02 23:39:09 +03:00
|
|
|
New: func() any {
|
2021-06-16 23:28:34 +03:00
|
|
|
return new(bytes.Buffer)
|
|
|
|
},
|
|
|
|
}
|