mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
Various fixes/tweaks to HTTP placeholder variables and file matching
- Rename http.var.* -> http.vars.* to be more consistent - Prefixing a path matcher with * now invokes simple suffix matching - Handlers and matchers that need a root path default to {http.vars.root} - Clean replacer output on the file matcher's file selection suffix
This commit is contained in:
parent
21d7b662e7
commit
14f9662f9c
8 changed files with 25 additions and 23 deletions
|
@ -17,9 +17,9 @@ package fileserver
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
|
|
||||||
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
|
||||||
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
||||||
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -71,11 +71,6 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no root was configured explicitly, use site root
|
|
||||||
if fsrv.Root == "" {
|
|
||||||
fsrv.Root = "{http.var.root}"
|
|
||||||
}
|
|
||||||
|
|
||||||
// hide the Caddyfile (and any imported Caddyfiles)
|
// hide the Caddyfile (and any imported Caddyfiles)
|
||||||
if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
|
if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
|
||||||
for _, file := range configFiles {
|
for _, file := range configFiles {
|
||||||
|
@ -104,7 +99,6 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
|
||||||
|
|
||||||
matcherSet := map[string]json.RawMessage{
|
matcherSet := map[string]json.RawMessage{
|
||||||
"file": h.JSON(MatchFile{
|
"file": h.JSON(MatchFile{
|
||||||
Root: "{http.var.root}",
|
|
||||||
TryFiles: try,
|
TryFiles: try,
|
||||||
}, nil),
|
}, nil),
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/caddyserver/caddy/v2"
|
"github.com/caddyserver/caddy/v2"
|
||||||
|
@ -87,8 +88,13 @@ func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Provision sets up m's defaults.
|
||||||
|
func (m *MatchFile) Provision(_ caddy.Context) error {
|
||||||
if m.Root == "" {
|
if m.Root == "" {
|
||||||
m.Root = "{http.var.root}"
|
m.Root = "{http.vars.root}"
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -141,7 +147,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
||||||
switch m.TryPolicy {
|
switch m.TryPolicy {
|
||||||
case "", tryPolicyFirstExist:
|
case "", tryPolicyFirstExist:
|
||||||
for _, f := range m.TryFiles {
|
for _, f := range m.TryFiles {
|
||||||
suffix := repl.ReplaceAll(f, "")
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||||
fullpath := sanitizedPathJoin(root, suffix)
|
fullpath := sanitizedPathJoin(root, suffix)
|
||||||
if fileExists(fullpath) {
|
if fileExists(fullpath) {
|
||||||
return suffix, fullpath, true
|
return suffix, fullpath, true
|
||||||
|
@ -153,7 +159,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
||||||
var largestFilename string
|
var largestFilename string
|
||||||
var largestSuffix string
|
var largestSuffix string
|
||||||
for _, f := range m.TryFiles {
|
for _, f := range m.TryFiles {
|
||||||
suffix := repl.ReplaceAll(f, "")
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||||
fullpath := sanitizedPathJoin(root, suffix)
|
fullpath := sanitizedPathJoin(root, suffix)
|
||||||
info, err := os.Stat(fullpath)
|
info, err := os.Stat(fullpath)
|
||||||
if err == nil && info.Size() > largestSize {
|
if err == nil && info.Size() > largestSize {
|
||||||
|
@ -169,7 +175,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
||||||
var smallestFilename string
|
var smallestFilename string
|
||||||
var smallestSuffix string
|
var smallestSuffix string
|
||||||
for _, f := range m.TryFiles {
|
for _, f := range m.TryFiles {
|
||||||
suffix := repl.ReplaceAll(f, "")
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||||
fullpath := sanitizedPathJoin(root, suffix)
|
fullpath := sanitizedPathJoin(root, suffix)
|
||||||
info, err := os.Stat(fullpath)
|
info, err := os.Stat(fullpath)
|
||||||
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
|
||||||
|
@ -185,7 +191,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
|
||||||
var recentFilename string
|
var recentFilename string
|
||||||
var recentSuffix string
|
var recentSuffix string
|
||||||
for _, f := range m.TryFiles {
|
for _, f := range m.TryFiles {
|
||||||
suffix := repl.ReplaceAll(f, "")
|
suffix := path.Clean(repl.ReplaceAll(f, ""))
|
||||||
fullpath := sanitizedPathJoin(root, suffix)
|
fullpath := sanitizedPathJoin(root, suffix)
|
||||||
info, err := os.Stat(fullpath)
|
info, err := os.Stat(fullpath)
|
||||||
if err == nil &&
|
if err == nil &&
|
||||||
|
|
|
@ -57,6 +57,10 @@ func (FileServer) CaddyModule() caddy.ModuleInfo {
|
||||||
|
|
||||||
// Provision sets up the static files responder.
|
// Provision sets up the static files responder.
|
||||||
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
|
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
|
||||||
|
if fsrv.Root == "" {
|
||||||
|
fsrv.Root = "{http.vars.root}"
|
||||||
|
}
|
||||||
|
|
||||||
if fsrv.IndexNames == nil {
|
if fsrv.IndexNames == nil {
|
||||||
fsrv.IndexNames = defaultIndexNames
|
fsrv.IndexNames = defaultIndexNames
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -151,12 +150,13 @@ func (MatchPath) CaddyModule() caddy.ModuleInfo {
|
||||||
// Match returns true if r matches m.
|
// Match returns true if r matches m.
|
||||||
func (m MatchPath) Match(r *http.Request) bool {
|
func (m MatchPath) Match(r *http.Request) bool {
|
||||||
for _, matchPath := range m {
|
for _, matchPath := range m {
|
||||||
compare := r.URL.Path
|
// as a special case, if the first character is a
|
||||||
|
// wildcard, treat it as a quick suffix match
|
||||||
if strings.HasPrefix(matchPath, "*") {
|
if strings.HasPrefix(matchPath, "*") {
|
||||||
compare = path.Base(compare)
|
return strings.HasSuffix(r.URL.Path, matchPath[1:])
|
||||||
}
|
}
|
||||||
// can ignore error here because we can't handle it anyway
|
// can ignore error here because we can't handle it anyway
|
||||||
matches, _ := filepath.Match(matchPath, compare)
|
matches, _ := filepath.Match(matchPath, r.URL.Path)
|
||||||
if matches {
|
if matches {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -173,6 +173,6 @@ const (
|
||||||
cookieReplPrefix = "http.request.cookie."
|
cookieReplPrefix = "http.request.cookie."
|
||||||
hostLabelReplPrefix = "http.request.host.labels."
|
hostLabelReplPrefix = "http.request.host.labels."
|
||||||
pathPartsReplPrefix = "http.request.uri.path."
|
pathPartsReplPrefix = "http.request.uri.path."
|
||||||
varsReplPrefix = "http.var."
|
varsReplPrefix = "http.vars."
|
||||||
respHeaderReplPrefix = "http.response.header."
|
respHeaderReplPrefix = "http.response.header."
|
||||||
)
|
)
|
||||||
|
|
|
@ -79,7 +79,7 @@ func (Transport) CaddyModule() caddy.ModuleInfo {
|
||||||
// Provision sets up t.
|
// Provision sets up t.
|
||||||
func (t *Transport) Provision(_ caddy.Context) error {
|
func (t *Transport) Provision(_ caddy.Context) error {
|
||||||
if t.Root == "" {
|
if t.Root == "" {
|
||||||
t.Root = "{http.var.root}"
|
t.Root = "{http.vars.root}"
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,10 +53,5 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.IncludeRoot == "" {
|
|
||||||
t.IncludeRoot = "{http.var.root}"
|
|
||||||
}
|
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,9 @@ func (t *Templates) Provision(ctx caddy.Context) error {
|
||||||
if t.MIMETypes == nil {
|
if t.MIMETypes == nil {
|
||||||
t.MIMETypes = defaultMIMETypes
|
t.MIMETypes = defaultMIMETypes
|
||||||
}
|
}
|
||||||
|
if t.IncludeRoot == "" {
|
||||||
|
t.IncludeRoot = "{http.vars.root}"
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue