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-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.
|
|
|
|
type MatchFile struct {
|
|
|
|
// The root directory, used for creating absolute
|
|
|
|
// file paths, and required when working with
|
|
|
|
// relative paths; if not specified, the current
|
|
|
|
// directory is assumed. Accepts placeholders.
|
|
|
|
Root string `json:"root,omitempty"`
|
|
|
|
|
|
|
|
// The list of files to try. Each path here is
|
|
|
|
// considered relatice to Root. If nil, the
|
|
|
|
// request URL's path will be assumed. Accepts
|
|
|
|
// placeholders.
|
|
|
|
TryFiles []string `json:"try_files,omitempty"`
|
|
|
|
|
|
|
|
// How to choose a file in TryFiles.
|
|
|
|
// Default is first_exist.
|
|
|
|
TryPolicy string `json:"try_policy,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (MatchFile) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
Name: "http.matchers.file",
|
|
|
|
New: func() caddy.Module { return new(MatchFile) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:05:47 -05:00
|
|
|
// UnmarshalCaddyfile sets up the matcher from Caddyfile tokens. Syntax:
|
|
|
|
//
|
|
|
|
// file {
|
|
|
|
// root <path>
|
|
|
|
// try_files <files...>
|
2019-08-21 11:46:35 -05:00
|
|
|
// try_policy first_exist|smallest_size|largest_size|most_recent_modified
|
2019-08-09 13:05:47 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
|
|
|
for d.NextBlock() {
|
|
|
|
switch d.Val() {
|
|
|
|
case "root":
|
|
|
|
if !d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
m.Root = d.Val()
|
|
|
|
case "try_files":
|
|
|
|
m.TryFiles = d.RemainingArgs()
|
|
|
|
if len(m.TryFiles) == 0 {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
case "try_policy":
|
|
|
|
if !d.NextArg() {
|
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
m.TryPolicy = d.Val()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
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,
|
|
|
|
tryPolicyMostRecentMod:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown try policy %s", m.TryPolicy)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match returns true if r matches m. Returns true
|
|
|
|
// if a file was matched. If so, two placeholders
|
|
|
|
// will be available:
|
|
|
|
// - http.matchers.file.relative
|
|
|
|
// - http.matchers.file.absolute
|
|
|
|
func (m MatchFile) Match(r *http.Request) bool {
|
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
|
|
|
|
rel, abs, matched := m.selectFile(r)
|
|
|
|
if matched {
|
|
|
|
repl.Set("http.matchers.file.relative", rel)
|
|
|
|
repl.Set("http.matchers.file.absolute", abs)
|
|
|
|
}
|
2019-07-15 18:33:47 -05:00
|
|
|
return matched
|
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.
|
|
|
|
// It returns the root-relative path to the matched file, the full
|
|
|
|
// or absolute path, and whether a match was made.
|
|
|
|
func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
root := repl.ReplaceAll(m.Root, ".")
|
2019-07-11 18:02:57 -05:00
|
|
|
|
|
|
|
// if list of files to try was omitted entirely,
|
|
|
|
// assume URL path
|
|
|
|
if m.TryFiles == nil {
|
|
|
|
// m is not a pointer, so this is safe
|
|
|
|
m.TryFiles = []string{r.URL.Path}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m.TryPolicy {
|
|
|
|
case "", tryPolicyFirstExist:
|
|
|
|
for _, f := range m.TryFiles {
|
2019-09-06 13:36:45 -05:00
|
|
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
2019-07-11 18:02:57 -05:00
|
|
|
fullpath := sanitizedPathJoin(root, suffix)
|
|
|
|
if fileExists(fullpath) {
|
|
|
|
return suffix, fullpath, 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
|
|
|
|
for _, f := range m.TryFiles {
|
2019-09-06 13:36:45 -05:00
|
|
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
2019-07-11 18:02:57 -05:00
|
|
|
fullpath := sanitizedPathJoin(root, suffix)
|
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil && info.Size() > largestSize {
|
|
|
|
largestSize = info.Size()
|
|
|
|
largestFilename = fullpath
|
|
|
|
largestSuffix = suffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return largestSuffix, largestFilename, true
|
|
|
|
|
|
|
|
case tryPolicySmallestSize:
|
|
|
|
var smallestSize int64
|
|
|
|
var smallestFilename string
|
|
|
|
var smallestSuffix string
|
|
|
|
for _, f := range m.TryFiles {
|
2019-09-06 13:36:45 -05:00
|
|
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
2019-07-11 18:02:57 -05:00
|
|
|
fullpath := sanitizedPathJoin(root, suffix)
|
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
|
|
|
smallestSize = info.Size()
|
|
|
|
smallestFilename = fullpath
|
|
|
|
smallestSuffix = suffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return smallestSuffix, smallestFilename, true
|
|
|
|
|
|
|
|
case tryPolicyMostRecentMod:
|
|
|
|
var recentDate time.Time
|
|
|
|
var recentFilename string
|
|
|
|
var recentSuffix string
|
|
|
|
for _, f := range m.TryFiles {
|
2019-09-06 13:36:45 -05:00
|
|
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
2019-07-11 18:02:57 -05:00
|
|
|
fullpath := sanitizedPathJoin(root, suffix)
|
|
|
|
info, err := os.Stat(fullpath)
|
|
|
|
if err == nil &&
|
|
|
|
(recentDate.IsZero() || info.ModTime().After(recentDate)) {
|
|
|
|
recentDate = info.ModTime()
|
|
|
|
recentFilename = fullpath
|
|
|
|
recentSuffix = suffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return recentSuffix, recentFilename, true
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileExists returns true if file exists.
|
|
|
|
func fileExists(file string) bool {
|
|
|
|
_, err := os.Stat(file)
|
|
|
|
return !os.IsNotExist(err)
|
2019-05-20 11:59:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
const (
|
|
|
|
tryPolicyFirstExist = "first_exist"
|
|
|
|
tryPolicyLargestSize = "largest_size"
|
|
|
|
tryPolicySmallestSize = "smallest_size"
|
|
|
|
tryPolicyMostRecentMod = "most_recent_modified"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.Validator = (*MatchFile)(nil)
|
|
|
|
_ caddyhttp.RequestMatcher = (*MatchFile)(nil)
|
|
|
|
)
|