diff options
Diffstat (limited to 'caddyconfig/httpcaddyfile/builtins.go')
-rw-r--r-- | caddyconfig/httpcaddyfile/builtins.go | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/caddyconfig/httpcaddyfile/builtins.go b/caddyconfig/httpcaddyfile/builtins.go index 2de808b..aa68adb 100644 --- a/caddyconfig/httpcaddyfile/builtins.go +++ b/caddyconfig/httpcaddyfile/builtins.go @@ -22,6 +22,7 @@ import ( "io/ioutil" "net/http" "reflect" + "strconv" "strings" "github.com/caddyserver/caddy/v2" @@ -462,14 +463,14 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) { if h.NextArg() { code = h.Val() } - if code == "permanent" { + + var body string + switch code { + case "permanent": code = "301" - } - if code == "temporary" || code == "" { + case "temporary", "": code = "302" - } - var body string - if code == "html" { + case "html": // Script tag comes first since that will better imitate a redirect in the browser's // history, but the meta tag is a fallback for most non-JS clients. const metaRedir = `<!DOCTYPE html> @@ -484,6 +485,15 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) { ` safeTo := html.EscapeString(to) body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo) + code = "302" + default: + codeInt, err := strconv.Atoi(code) + if err != nil { + return nil, h.Errf("Not a supported redir code type or not valid integer: '%s'", code) + } + if codeInt < 300 || codeInt > 399 { + return nil, h.Errf("Redir code not in the 3xx range: '%v'", codeInt) + } } return caddyhttp.StaticResponse{ |