summaryrefslogtreecommitdiff
path: root/caddyconfig
diff options
context:
space:
mode:
authorTyler Kropp <github@tylerkropp.xyz>2021-01-28 14:59:50 -0500
committerGitHub <noreply@github.com>2021-01-28 12:59:50 -0700
commitbef80cd806eadb3555487b624849149b46199c3f (patch)
treefb401251cc769a236a8eacf2192ebc77b4e90eb7 /caddyconfig
parente2c5c28597e6a8c5e7ef48fde88f0b7d740d9586 (diff)
caddyhttp: Fix redir html status code, improve flow (#3987)
* Fix html redir code, improve flow * Fix integer check error and add tests
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/httpcaddyfile/builtins.go22
-rw-r--r--caddyconfig/httpcaddyfile/builtins_test.go122
2 files changed, 138 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{
diff --git a/caddyconfig/httpcaddyfile/builtins_test.go b/caddyconfig/httpcaddyfile/builtins_test.go
index c06dd23..09bb4ed 100644
--- a/caddyconfig/httpcaddyfile/builtins_test.go
+++ b/caddyconfig/httpcaddyfile/builtins_test.go
@@ -51,3 +51,125 @@ func TestLogDirectiveSyntax(t *testing.T) {
}
}
}
+
+func TestRedirDirectiveSyntax(t *testing.T) {
+ for i, tc := range []struct {
+ input string
+ expectError bool
+ }{
+ {
+ input: `:8080 {
+ redir :8081
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir * :8081
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /api/* :8081 300
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir :8081 300
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /api/* :8081 399
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir :8081 399
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /old.html /new.html
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /old.html /new.html temporary
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir https://example.com{uri} permanent
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /old.html /new.html permanent
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /old.html /new.html html
+ }`,
+ expectError: false,
+ },
+ {
+ input: `:8080 {
+ redir /old.html /new.html htlm
+ }`,
+ expectError: true,
+ },
+ {
+ input: `:8080 {
+ redir * :8081 200
+ }`,
+ expectError: true,
+ },
+ {
+ input: `:8080 {
+ redir * :8081 400
+ }`,
+ expectError: true,
+ },
+ {
+ input: `:8080 {
+ redir * :8081 temp
+ }`,
+ expectError: true,
+ },
+ {
+ input: `:8080 {
+ redir * :8081 perm
+ }`,
+ expectError: true,
+ },
+ {
+ input: `:8080 {
+ redir * :8081 php
+ }`,
+ expectError: true,
+ },
+ } {
+
+ adapter := caddyfile.Adapter{
+ ServerType: ServerType{},
+ }
+
+ _, _, err := adapter.Adapt([]byte(tc.input), nil)
+
+ if err != nil != tc.expectError {
+ t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
+ continue
+ }
+ }
+}