Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
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
Matthew Holt committed
Sep 6, 2019 at 12:36 UTC
14f9662f9cc0f93e88d5efbbaf10de79070bea93
8 files changed
+25
-23
modules/caddyhttp/fileserver/caddyfile.go
+1
-7
@@ -17,9 +17,9 @@ package fileserver
17
import (
18
"encoding/json"
19
20
- "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
20
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
21
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
22
+ "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
23
)
24
25
func init() {
@@ -71,11 +71,6 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
71
}
72
}
73
74
- // if no root was configured explicitly, use site root
75
- if fsrv.Root == "" {
76
- fsrv.Root = "{http.var.root}"
77
- }
78
-
74
// hide the Caddyfile (and any imported Caddyfiles)
75
if configFiles := h.Caddyfiles(); len(configFiles) > 0 {
76
for _, file := range configFiles {
@@ -104,7 +99,6 @@ func parseTryFiles(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
99
100
matcherSet := map[string]json.RawMessage{
101
"file": h.JSON(MatchFile{
107
- Root: "{http.var.root}",
102
TryFiles: try,
103
}, nil),
104
}
modules/caddyhttp/fileserver/matcher.go
+11
-5
@@ -18,6 +18,7 @@ import (
18
"fmt"
19
"net/http"
20
"os"
21
+ "path"
22
"time"
23
24
"github.com/caddyserver/caddy/v2"
@@ -87,8 +88,13 @@ func (m *MatchFile) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
88
}
89
}
90
}
91
+ return nil
92
+}
93
+
94
+// Provision sets up m's defaults.
95
+func (m *MatchFile) Provision(_ caddy.Context) error {
96
if m.Root == "" {
91
- m.Root = "{http.var.root}"
97
+ m.Root = "{http.vars.root}"
98
}
99
return nil
100
}
@@ -141,7 +147,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
147
switch m.TryPolicy {
148
case "", tryPolicyFirstExist:
149
for _, f := range m.TryFiles {
144
- suffix := repl.ReplaceAll(f, "")
150
+ suffix := path.Clean(repl.ReplaceAll(f, ""))
151
fullpath := sanitizedPathJoin(root, suffix)
152
if fileExists(fullpath) {
153
return suffix, fullpath, true
@@ -153,7 +159,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
159
var largestFilename string
160
var largestSuffix string
161
for _, f := range m.TryFiles {
156
- suffix := repl.ReplaceAll(f, "")
162
+ suffix := path.Clean(repl.ReplaceAll(f, ""))
163
fullpath := sanitizedPathJoin(root, suffix)
164
info, err := os.Stat(fullpath)
165
if err == nil && info.Size() > largestSize {
@@ -169,7 +175,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
175
var smallestFilename string
176
var smallestSuffix string
177
for _, f := range m.TryFiles {
172
- suffix := repl.ReplaceAll(f, "")
178
+ suffix := path.Clean(repl.ReplaceAll(f, ""))
179
fullpath := sanitizedPathJoin(root, suffix)
180
info, err := os.Stat(fullpath)
181
if err == nil && (smallestSize == 0 || info.Size() < smallestSize) {
@@ -185,7 +191,7 @@ func (m MatchFile) selectFile(r *http.Request) (rel, abs string, matched bool) {
191
var recentFilename string
192
var recentSuffix string
193
for _, f := range m.TryFiles {
188
- suffix := repl.ReplaceAll(f, "")
194
+ suffix := path.Clean(repl.ReplaceAll(f, ""))
195
fullpath := sanitizedPathJoin(root, suffix)
196
info, err := os.Stat(fullpath)
197
if err == nil &&
modules/caddyhttp/fileserver/staticfiles.go
+4
@@ -57,6 +57,10 @@ func (FileServer) CaddyModule() caddy.ModuleInfo {
57
58
// Provision sets up the static files responder.
59
func (fsrv *FileServer) Provision(ctx caddy.Context) error {
60
+ if fsrv.Root == "" {
61
+ fsrv.Root = "{http.vars.root}"
62
+ }
63
+
64
if fsrv.IndexNames == nil {
65
fsrv.IndexNames = defaultIndexNames
66
}
modules/caddyhttp/matchers.go
+4
-4
@@ -22,7 +22,6 @@ import (
22
"net/http"
23
"net/textproto"
24
"net/url"
25
- "path"
25
"path/filepath"
26
"regexp"
27
"strings"
@@ -151,12 +150,13 @@ func (MatchPath) CaddyModule() caddy.ModuleInfo {
150
// Match returns true if r matches m.
151
func (m MatchPath) Match(r *http.Request) bool {
152
for _, matchPath := range m {
154
- compare := r.URL.Path
153
+ // as a special case, if the first character is a
154
+ // wildcard, treat it as a quick suffix match
155
if strings.HasPrefix(matchPath, "*") {
156
- compare = path.Base(compare)
156
+ return strings.HasSuffix(r.URL.Path, matchPath[1:])
157
}
158
// can ignore error here because we can't handle it anyway
159
- matches, _ := filepath.Match(matchPath, compare)
159
+ matches, _ := filepath.Match(matchPath, r.URL.Path)
160
if matches {
161
return true
162
}
modules/caddyhttp/replacer.go
+1
-1
@@ -173,6 +173,6 @@ const (
173
cookieReplPrefix = "http.request.cookie."
174
hostLabelReplPrefix = "http.request.host.labels."
175
pathPartsReplPrefix = "http.request.uri.path."
176
- varsReplPrefix = "http.var."
176
+ varsReplPrefix = "http.vars."
177
respHeaderReplPrefix = "http.response.header."
178
)
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+1
-1
@@ -79,7 +79,7 @@ func (Transport) CaddyModule() caddy.ModuleInfo {
79
// Provision sets up t.
80
func (t *Transport) Provision(_ caddy.Context) error {
81
if t.Root == "" {
82
- t.Root = "{http.var.root}"
82
+ t.Root = "{http.vars.root}"
83
}
84
return nil
85
}
modules/caddyhttp/templates/caddyfile.go
-5
@@ -53,10 +53,5 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
53
}
54
}
55
}
56
-
57
- if t.IncludeRoot == "" {
58
- t.IncludeRoot = "{http.var.root}"
59
- }
60
-
56
return t, nil
57
}
modules/caddyhttp/templates/templates.go
+3
@@ -50,6 +50,9 @@ func (t *Templates) Provision(ctx caddy.Context) error {
50
if t.MIMETypes == nil {
51
t.MIMETypes = defaultMIMETypes
52
}
53
+ if t.IncludeRoot == "" {
54
+ t.IncludeRoot = "{http.vars.root}"
55
+ }
56
return nil
57
}
58