2019-06-30 17:07:58 -05: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-20 22:21:33 -05:00
|
|
|
package fileserver
|
2019-05-20 11:59:20 -05:00
|
|
|
|
|
|
|
import (
|
2019-07-11 18:02:57 -05:00
|
|
|
"fmt"
|
2019-05-20 11:59:20 -05:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2019-09-06 13:36:45 -05:00
|
|
|
"path"
|
2019-09-06 14:32:02 -05:00
|
|
|
"strings"
|
2019-07-11 18:02:57 -05:00
|
|
|
"time"
|
2019-05-20 11:59:20 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2019-08-21 11:46:35 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2019-07-12 11:15:27 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-05-20 11:59:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 11:46:35 -05:00
|
|
|
caddy.RegisterModule(MatchFile{})
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
// MatchFile is an HTTP request matcher that can match
|
|
|
|
// requests based upon file existence.
|
2019-12-29 15:16:34 -05:00
|
|
|
//
|
2020-09-16 19:09:28 -05:00
|
|
|
// Upon matching, three new placeholders will be made
|
2019-12-29 15:16:34 -05:00
|
|
|
// available:
|
|
|
|
//
|
|
|
|
// - `{http.matchers.file.relative}` The root-relative
|
|
|
|
// path of the file. This is often useful when rewriting
|
|
|
|
// requests.
|
|
|
|
// - `{http.matchers.file.absolute}` The absolute path
|
|
|
|
// of the matched file.
|
2020-09-16 19:09:28 -05:00
|
|
|
// - `{http.matchers.file.type}` Set to "directory" if
|
|
|
|
// the matched file is a directory, "file" otherwise.
|
2020-12-04 19:12:13 -05:00
|
|
|
// - `{http.matchers.file.remainder}` Set to the remainder
|
|
|
|
// of the path if the path was split by `split_path`.
|
2019-07-11 18:02:57 -05:00
|
|
|
type MatchFile struct {
|
|
|
|
// The root directory, used for creating absolute
|
|
|
|
// file paths, and required when working with
|
2019-12-29 15:16:34 -05:00
|
|
|
// relative paths; if not specified, `{http.vars.root}`
|
|
|
|
// will be used, if set; otherwise, the current
|
2019-07-11 18:02:57 -05:00
|
|
|
// directory is assumed. Accepts placeholders.
|
|
|
|
Root string `json:"root,omitempty"`
|
|
|
|
|
|
|
|
// The list of files to try. Each path here is
|
2020-02-27 21:30:48 -05:00
|
|
|
// considered related to Root. If nil, the request
|
2020-01-22 11:32:38 -05:00
|
|
|
// URL's path will be assumed. Files and
|
|
|
|
// directories are treated distinctly, so to match
|
|
|
|
// a directory, the filepath MUST end in a forward
|
|
|
|
// slash `/`. To match a regular file, there must
|
|
|
|
// be no trailing slash. Accepts placeholders.
|
2019-07-11 18:02:57 -05:00
|
|
|
TryFiles []string `json:"try_files,omitempty"`
|
|
|
|
|
2019-12-23 14:45:35 -05:00
|
|
|
// How to choose a file in TryFiles. Can be:
|
|
|
|
//
|
|
|
|
// - first_exist
|
|
|
|
// - smallest_size
|
|
|
|
// - largest_size
|
|
|
|
// - most_recently_modified
|
|
|
|
//
|
2019-07-11 18:02:57 -05:00
|
|
|
// Default is first_exist.
|
|
|
|
TryPolicy string `json:"try_policy,omitempty"`
|
2020-04-27 15:46:46 -05:00
|
|
|
|
|
|
|
// A list of delimiters to use to split the path in two
|
|
|
|
// when trying files. If empty, no splitting will
|
|
|
|
// occur, and the path will be tried as-is. For each
|
|
|
|
// split value, the left-hand side of the split,
|
|
|
|
// including the split value, will be the path tried.
|
|
|
|
// For example, the path `/remote.php/dav/` using the
|
|
|
|
// split value `.php` would try the file `/remote.php`.
|
|
|
|
// Each delimiter must appear at the end of a URI path
|
|
|
|
// component in order to be used as a split delimiter.
|
|
|
|
SplitPath []string `json:"split_path,omitempty"`
|
2019-07-11 18:02:57 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (MatchFile) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.matchers.file",
|
|
|
|
New: func() caddy.Module { return new(MatchFile) },
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:05:47 -05:00
|
|
|
// UnmarshalCaddyfile sets up the matcher from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2020-05-05 13:34:58 -05:00
|
|
|
// file <files...> {
|
2019-08-09 13:05:47 -05:00
|
|
|
// root <path>
|
|
|
|
// try_files <files...>
|
2019-12-23 14:45:35 -05:00
|
|
|
// try_policy first_exist|smallest_size|largest_size|most_recently_modified
|
2019-08-09 13:05:47 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
2020-05-05 13:34:58 -05:00
|
|
|
m.TryFiles = append(m.TryFiles, d.RemainingArgs()...)
|
2019-09-10 20:21:52 -05:00
|
|
|
for d.NextBlock(0) {
|
2019-08-09 13:05:47 -05:00
|
|
|
switch d.Val() {
|
|
|
|
case "root":
|
|
|
|
if !d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
m.Root = d.Val()
|
|
|
|
case "try_files":
|
2020-05-05 13:34:58 -05:00
|
|
|
m.TryFiles = append(m.TryFiles, d.RemainingArgs()...)
|
2019-08-09 13:05:47 -05:00
|
|
|
if len(m.TryFiles) == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
case "try_policy":
|
|
|
|
if !d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
m.TryPolicy = d.Val()
|
2020-07-31 14:55:01 -05:00
|
|
|
case "split_path":
|
2020-04-27 15:46:46 -05:00
|
|
|
m.SplitPath = d.RemainingArgs()
|
|
|
|
if len(m.SplitPath) == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
2020-07-31 14:55:01 -05:00
|
|
|
default:
|
|
|
|
return d.Errf("unrecognized subdirective: %s", d.Val())
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 13:36:45 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provision sets up m's defaults.
|
|
|
|
func (m *MatchFile) Provision(_ caddy.Context) error {
|
2019-08-21 11:46:35 -05:00
|
|
|
if m.Root == "" {
|
2019-09-06 13:36:45 -05:00
|
|
|
m.Root = "{http.vars.root}"
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
fileserver: Fix `file` matcher with empty `try_files` (#4147)
* fileserver: Fix `file` matcher with empty `try_files`
Fixes https://github.com/caddyserver/caddy/issues/4146
If `TryFiles` is empty, we fill it with `r.URL.Path`. In this case, this is `/`. Then later, in `prepareFilePath()`, we run the replacer (which turns `{path}` into `/` at that point) but `file` remains the original value (and the placeholder is still the placeholder there).
So then `strings.HasSuffix(file, "/")` will be `false` for the placeholder, but `true` for the empty `TryFiles` codepath, because `file` was `/` due to being set to the actual request value beforehand.
This means that `suffix` becomes `//` in that case, so after `sanitizedPathJoin`, it becomes `./`, so `strictFileExists`'s `strings.HasSuffix(file, separator)` codepath will return true.
I think we should change the `m.TryFiles == nil` codepath to `m.TryFiles = []string{"{http.request.uri.path}"}` for consistency. (And maybe consider hoisting this to `Provision` cause there's no point doing this on every request). I don't think this "optimization" of directly using `r.URL.Path` is so valuable, cause it causes this edgecase with directories.
* Update modules/caddyhttp/fileserver/matcher.go
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
2021-05-04 10:49:13 -05:00
|
|
|
// if list of files to try was omitted entirely, assume URL path
|
|
|
|
// (use placeholder instead of r.URL.Path; see issue #4146)
|
|
|
|
if m.TryFiles == nil {
|
|
|
|
m.TryFiles = []string{"{http.request.uri.path}"}
|
|
|
|
}
|
2019-08-09 13:05:47 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
// Validate ensures m has a valid configuration.
|
|
|
|
func (m MatchFile) Validate() error {
|
|
|
|
switch m.TryPolicy {
|
|
|
|
case "",
|
|
|
|
tryPolicyFirstExist,
|
|
|
|
tryPolicyLargestSize,
|
|
|
|
tryPolicySmallestSize,
|
2019-12-23 14:45:35 -05:00
|
|
|
tryPolicyMostRecentlyMod:
|
2019-07-11 18:02:57 -05:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown try policy %s", m.TryPolicy)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if r matches m. Returns true
|
2020-12-04 19:12:13 -05:00
|
|
|
// if a file was matched. If so, four placeholders
|
2019-07-11 18:02:57 -05:00
|
|
|
// will be available:
|
|
|
|
// - http.matchers.file.relative
|
|
|
|
// - http.matchers.file.absolute
|
2020-09-16 19:09:28 -05:00
|
|
|
// - http.matchers.file.type
|
2020-12-04 19:12:13 -05:00
|
|
|
// - http.matchers.file.remainder
|
2019-07-11 18:02:57 -05:00
|
|
|
func (m MatchFile) Match(r *http.Request) bool {
|
2020-09-16 19:09:28 -05:00
|
|
|
return m.selectFile(r)
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
// selectFile chooses a file according to m.TryPolicy by appending
|
|
|
|
// the paths in m.TryFiles to m.Root, with placeholder replacements.
|
2020-09-16 19:09:28 -05:00
|
|
|
func (m MatchFile) selectFile(r *http.Request) (matched bool) {
|
2019-12-29 15:12:52 -05:00
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-07-11 18:02:57 -05:00
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
root := repl.ReplaceAll(m.Root, ".")
|
2019-07-11 18:02:57 -05:00
|
|
|
|
2020-09-16 19:09:28 -05:00
|
|
|
// common preparation of the file into parts
|
2020-12-04 19:12:13 -05:00
|
|
|
prepareFilePath := func(file string) (suffix, fullpath, remainder string) {
|
|
|
|
suffix, remainder = m.firstSplit(path.Clean(repl.ReplaceAll(file, "")))
|
2020-09-16 19:09:28 -05:00
|
|
|
if strings.HasSuffix(file, "/") {
|
|
|
|
suffix += "/"
|
|
|
|
}
|
2021-06-17 10:59:08 -05:00
|
|
|
fullpath = caddyhttp.SanitizedPathJoin(root, suffix)
|
2020-12-04 19:12:13 -05:00
|
|
|
return
|
2020-09-16 19:09:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// sets up the placeholders for the matched file
|
2020-12-04 19:12:13 -05:00
|
|
|
setPlaceholders := func(info os.FileInfo, rel string, abs string, remainder string) {
|
2020-09-16 19:09:28 -05:00
|
|
|
repl.Set("http.matchers.file.relative", rel)
|
|
|
|
repl.Set("http.matchers.file.absolute", abs)
|
2020-12-04 19:12:13 -05:00
|
|
|
repl.Set("http.matchers.file.remainder", remainder)
|
2020-09-16 19:09:28 -05:00
|
|
|
|
|
|
|
fileType := "file"
|
|
|
|
if info.IsDir() {
|
|
|
|
fileType = "directory"
|
|
|
|
}
|
|
|
|
repl.Set("http.matchers.file.type", fileType)
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
switch m.TryPolicy {
|
|
|
|
case "", tryPolicyFirstExist:
|
|
|
|
for _, f := range m.TryFiles {
|
2020-12-04 19:12:13 -05:00
|
|
|
suffix, fullpath, remainder := prepareFilePath(f)
|
2020-09-16 19:09:28 -05:00
|
|
|
if info, exists := strictFileExists(fullpath); exists {
|
2020-12-04 19:12:13 -05:00
|
|
|
setPlaceholders(info, suffix, fullpath, remainder)
|
2020-09-16 19:09:28 -05:00
|
|
|
return true
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
|
|
|
|
case tryPolicyLargestSize:
|
|
|
|
var largestSize int64
|
|
|
|
var largestFilename string
|
|
|
|
var largestSuffix string
|
2020-12-04 19:12:13 -05:00
|
|
|
var remainder string
|
2020-09-16 19:09:28 -05:00
|
|
|
var info os.FileInfo
|
2019-07-11 18:02:57 -05:00
|
|
|
for _, f := range m.TryFiles {
|
2020-12-04 19:12:13 -05:00
|
|
|
suffix, fullpath, splitRemainder := prepareFilePath(f)
|
2019-07-11 18:02:57 -05:00
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil && info.Size() > largestSize {
|
|
|
|
largestSize = info.Size()
|
|
|
|
largestFilename = fullpath
|
|
|
|
largestSuffix = suffix
|
2020-12-04 19:12:13 -05:00
|
|
|
remainder = splitRemainder
|
2019-07-11 18:02:57 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 19:12:13 -05:00
|
|
|
setPlaceholders(info, largestSuffix, largestFilename, remainder)
|
2020-09-16 19:09:28 -05:00
|
|
|
return true
|
2019-07-11 18:02:57 -05:00
|
|
|
|
|
|
|
case tryPolicySmallestSize:
|
|
|
|
var smallestSize int64
|
|
|
|
var smallestFilename string
|
|
|
|
var smallestSuffix string
|
2020-12-04 19:12:13 -05:00
|
|
|
var remainder string
|
2020-09-16 19:09:28 -05:00
|
|
|
var info os.FileInfo
|
2019-07-11 18:02:57 -05:00
|
|
|
for _, f := range m.TryFiles {
|
2020-12-04 19:12:13 -05:00
|
|
|
suffix, fullpath, splitRemainder := prepareFilePath(f)
|
2019-07-11 18:02:57 -05:00
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
|
|
|
smallestSize = info.Size()
|
|
|
|
smallestFilename = fullpath
|
|
|
|
smallestSuffix = suffix
|
2020-12-04 19:12:13 -05:00
|
|
|
remainder = splitRemainder
|
2019-07-11 18:02:57 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 19:12:13 -05:00
|
|
|
setPlaceholders(info, smallestSuffix, smallestFilename, remainder)
|
2020-09-16 19:09:28 -05:00
|
|
|
return true
|
2019-07-11 18:02:57 -05:00
|
|
|
|
2019-12-23 14:45:35 -05:00
|
|
|
case tryPolicyMostRecentlyMod:
|
2019-07-11 18:02:57 -05:00
|
|
|
var recentDate time.Time
|
|
|
|
var recentFilename string
|
|
|
|
var recentSuffix string
|
2020-12-04 19:12:13 -05:00
|
|
|
var remainder string
|
2020-09-16 19:09:28 -05:00
|
|
|
var info os.FileInfo
|
2019-07-11 18:02:57 -05:00
|
|
|
for _, f := range m.TryFiles {
|
2020-12-04 19:12:13 -05:00
|
|
|
suffix, fullpath, splitRemainder := prepareFilePath(f)
|
2019-07-11 18:02:57 -05:00
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil &&
|
|
|
|
(recentDate.IsZero() || info.ModTime().After(recentDate)) {
|
|
|
|
recentDate = info.ModTime()
|
|
|
|
recentFilename = fullpath
|
|
|
|
recentSuffix = suffix
|
2020-12-04 19:12:13 -05:00
|
|
|
remainder = splitRemainder
|
2019-07-11 18:02:57 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 19:12:13 -05:00
|
|
|
setPlaceholders(info, recentSuffix, recentFilename, remainder)
|
2020-09-16 19:09:28 -05:00
|
|
|
return true
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-06 14:32:02 -05:00
|
|
|
// strictFileExists returns true if file exists
|
|
|
|
// and matches the convention of the given file
|
|
|
|
// path. If the path ends in a forward slash,
|
|
|
|
// the file must also be a directory; if it does
|
|
|
|
// NOT end in a forward slash, the file must NOT
|
|
|
|
// be a directory.
|
2020-09-16 19:09:28 -05:00
|
|
|
func strictFileExists(file string) (os.FileInfo, bool) {
|
2019-09-06 14:32:02 -05:00
|
|
|
stat, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
// in reality, this can be any error
|
|
|
|
// such as permission or even obscure
|
|
|
|
// ones like "is not a directory" (when
|
|
|
|
// trying to stat a file within a file);
|
|
|
|
// in those cases we can't be sure if
|
|
|
|
// the file exists, so we just treat any
|
|
|
|
// error as if it does not exist; see
|
|
|
|
// https://stackoverflow.com/a/12518877/1048862
|
2020-09-16 19:09:28 -05:00
|
|
|
return nil, false
|
2019-09-06 13:57:12 -05:00
|
|
|
}
|
2020-11-02 16:20:12 -05:00
|
|
|
if strings.HasSuffix(file, separator) {
|
2019-09-06 14:32:02 -05:00
|
|
|
// by convention, file paths ending
|
2020-09-16 19:09:28 -05:00
|
|
|
// in a path separator must be a directory
|
|
|
|
return stat, stat.IsDir()
|
2019-09-06 14:32:02 -05:00
|
|
|
}
|
|
|
|
// by convention, file paths NOT ending
|
2020-09-16 19:09:28 -05:00
|
|
|
// in a path separator must NOT be a directory
|
|
|
|
return stat, !stat.IsDir()
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2020-04-27 15:46:46 -05:00
|
|
|
// firstSplit returns the first result where the path
|
|
|
|
// can be split in two by a value in m.SplitPath. The
|
2020-12-04 19:12:13 -05:00
|
|
|
// return values are the first piece of the path that
|
|
|
|
// ends with the split substring and the remainder.
|
|
|
|
// If the path cannot be split, the path is returned
|
|
|
|
// as-is (with no remainder).
|
|
|
|
func (m MatchFile) firstSplit(path string) (splitPart, remainder string) {
|
2020-04-27 15:46:46 -05:00
|
|
|
for _, split := range m.SplitPath {
|
2020-07-31 14:55:01 -05:00
|
|
|
if idx := indexFold(path, split); idx > -1 {
|
2020-04-27 15:46:46 -05:00
|
|
|
pos := idx + len(split)
|
|
|
|
// skip the split if it's not the final part of the filename
|
|
|
|
if pos != len(path) && !strings.HasPrefix(path[pos:], "/") {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-04 19:12:13 -05:00
|
|
|
return path[:pos], path[pos:]
|
2020-04-27 15:46:46 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 19:12:13 -05:00
|
|
|
return path, ""
|
2020-04-27 15:46:46 -05:00
|
|
|
}
|
|
|
|
|
2020-07-31 14:55:01 -05:00
|
|
|
// There is no strings.IndexFold() function like there is strings.EqualFold(),
|
|
|
|
// but we can use strings.EqualFold() to build our own case-insensitive
|
|
|
|
// substring search (as of Go 1.14).
|
|
|
|
func indexFold(haystack, needle string) int {
|
|
|
|
nlen := len(needle)
|
|
|
|
for i := 0; i+nlen < len(haystack); i++ {
|
|
|
|
if strings.EqualFold(haystack[i:i+nlen], needle) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
const (
|
2019-12-23 14:45:35 -05:00
|
|
|
tryPolicyFirstExist = "first_exist"
|
|
|
|
tryPolicyLargestSize = "largest_size"
|
|
|
|
tryPolicySmallestSize = "smallest_size"
|
|
|
|
tryPolicyMostRecentlyMod = "most_recently_modified"
|
2019-07-11 18:02:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.Validator = (*MatchFile)(nil)
|
|
|
|
_ caddyhttp.RequestMatcher = (*MatchFile)(nil)
|
|
|
|
)
|