summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/fileserver
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2021-09-17 02:52:32 -0400
committerGitHub <noreply@github.com>2021-09-17 00:52:32 -0600
commit907e2d8d3a8ebaf14aa99939c493e56645bc2089 (patch)
treef5aa4af53cdc9209075c6bc52e1aa0e12c7abc7b /modules/caddyhttp/fileserver
parent33c70f418f780f8e9524c73fbf4bbdbdbb9d7500 (diff)
caddyhttp: Add support for triggering errors from `try_files` (#4346)
* caddyhttp: Add support for triggering errors from `try_files` * caddyhttp: Use vars instead of placeholders/replacer for matcher errors * caddyhttp: Add comment for matcher error var key
Diffstat (limited to 'modules/caddyhttp/fileserver')
-rw-r--r--modules/caddyhttp/fileserver/matcher.go25
1 files changed, 24 insertions, 1 deletions
diff --git a/modules/caddyhttp/fileserver/matcher.go b/modules/caddyhttp/fileserver/matcher.go
index 739b9e0..f8e9ce0 100644
--- a/modules/caddyhttp/fileserver/matcher.go
+++ b/modules/caddyhttp/fileserver/matcher.go
@@ -19,6 +19,7 @@ import (
"net/http"
"os"
"path"
+ "strconv"
"strings"
"time"
@@ -60,7 +61,11 @@ type MatchFile struct {
// 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.
+ // be no trailing slash. Accepts placeholders. If
+ // the policy is "first_exist", then an error may
+ // be triggered as a fallback by configuring "="
+ // followed by a status code number,
+ // for example "=404".
TryFiles []string `json:"try_files,omitempty"`
// How to choose a file in TryFiles. Can be:
@@ -205,6 +210,10 @@ func (m MatchFile) selectFile(r *http.Request) (matched bool) {
switch m.TryPolicy {
case "", tryPolicyFirstExist:
for _, f := range m.TryFiles {
+ if err := parseErrorCode(f); err != nil {
+ caddyhttp.SetVar(r.Context(), caddyhttp.MatcherErrorVarKey, err)
+ return
+ }
suffix, fullpath, remainder := prepareFilePath(f)
if info, exists := strictFileExists(fullpath); exists {
setPlaceholders(info, suffix, fullpath, remainder)
@@ -274,6 +283,20 @@ func (m MatchFile) selectFile(r *http.Request) (matched bool) {
return
}
+// parseErrorCode checks if the input is a status
+// code number, prefixed by "=", and returns an
+// error if so.
+func parseErrorCode(input string) error {
+ if len(input) > 1 && input[0] == '=' {
+ code, err := strconv.Atoi(input[1:])
+ if err != nil || code < 100 || code > 999 {
+ return nil
+ }
+ return caddyhttp.Error(code, fmt.Errorf("%s", input[1:]))
+ }
+ return nil
+}
+
// strictFileExists returns true if file exists
// and matches the convention of the given file
// path. If the path ends in a forward slash,