@marcel91200 / caddy Mirror · read-only / commits / bef80cd8
Fork 0

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

caddyhttp: Fix redir html status code, improve flow (#3987)

* Fix html redir code, improve flow * Fix integer check error and add tests

Tyler Kropp committed Jan 28, 2021 at 14:59 UTC bef80cd806eadb3555487b624849149b46199c3f
2 files changed +138 -6
caddyconfig/httpcaddyfile/builtins.go
+16 -6
@@ -22,6 +22,7 @@ import (
22 "io/ioutil"
23 "net/http"
24 "reflect"
25 + "strconv"
26 "strings"
27
28 "github.com/caddyserver/caddy/v2"
@@ -462,14 +463,14 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
463 if h.NextArg() {
464 code = h.Val()
465 }
465 - if code == "permanent" {
466 +
467 + var body string
468 + switch code {
469 + case "permanent":
470 code = "301"
467 - }
468 - if code == "temporary" || code == "" {
471 + case "temporary", "":
472 code = "302"
470 - }
471 - var body string
472 - if code == "html" {
473 + case "html":
474 // Script tag comes first since that will better imitate a redirect in the browser's
475 // history, but the meta tag is a fallback for most non-JS clients.
476 const metaRedir = `<!DOCTYPE html>
@@ -484,6 +485,15 @@ func parseRedir(h Helper) (caddyhttp.MiddlewareHandler, error) {
485 `
486 safeTo := html.EscapeString(to)
487 body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
488 + code = "302"
489 + default:
490 + codeInt, err := strconv.Atoi(code)
491 + if err != nil {
492 + return nil, h.Errf("Not a supported redir code type or not valid integer: '%s'", code)
493 + }
494 + if codeInt < 300 || codeInt > 399 {
495 + return nil, h.Errf("Redir code not in the 3xx range: '%v'", codeInt)
496 + }
497 }
498
499 return caddyhttp.StaticResponse{
caddyconfig/httpcaddyfile/builtins_test.go
+122
@@ -51,3 +51,125 @@ func TestLogDirectiveSyntax(t *testing.T) {
51 }
52 }
53 }
54 +
55 +func TestRedirDirectiveSyntax(t *testing.T) {
56 + for i, tc := range []struct {
57 + input string
58 + expectError bool
59 + }{
60 + {
61 + input: `:8080 {
62 + redir :8081
63 + }`,
64 + expectError: false,
65 + },
66 + {
67 + input: `:8080 {
68 + redir * :8081
69 + }`,
70 + expectError: false,
71 + },
72 + {
73 + input: `:8080 {
74 + redir /api/* :8081 300
75 + }`,
76 + expectError: false,
77 + },
78 + {
79 + input: `:8080 {
80 + redir :8081 300
81 + }`,
82 + expectError: false,
83 + },
84 + {
85 + input: `:8080 {
86 + redir /api/* :8081 399
87 + }`,
88 + expectError: false,
89 + },
90 + {
91 + input: `:8080 {
92 + redir :8081 399
93 + }`,
94 + expectError: false,
95 + },
96 + {
97 + input: `:8080 {
98 + redir /old.html /new.html
99 + }`,
100 + expectError: false,
101 + },
102 + {
103 + input: `:8080 {
104 + redir /old.html /new.html temporary
105 + }`,
106 + expectError: false,
107 + },
108 + {
109 + input: `:8080 {
110 + redir https://example.com{uri} permanent
111 + }`,
112 + expectError: false,
113 + },
114 + {
115 + input: `:8080 {
116 + redir /old.html /new.html permanent
117 + }`,
118 + expectError: false,
119 + },
120 + {
121 + input: `:8080 {
122 + redir /old.html /new.html html
123 + }`,
124 + expectError: false,
125 + },
126 + {
127 + input: `:8080 {
128 + redir /old.html /new.html htlm
129 + }`,
130 + expectError: true,
131 + },
132 + {
133 + input: `:8080 {
134 + redir * :8081 200
135 + }`,
136 + expectError: true,
137 + },
138 + {
139 + input: `:8080 {
140 + redir * :8081 400
141 + }`,
142 + expectError: true,
143 + },
144 + {
145 + input: `:8080 {
146 + redir * :8081 temp
147 + }`,
148 + expectError: true,
149 + },
150 + {
151 + input: `:8080 {
152 + redir * :8081 perm
153 + }`,
154 + expectError: true,
155 + },
156 + {
157 + input: `:8080 {
158 + redir * :8081 php
159 + }`,
160 + expectError: true,
161 + },
162 + } {
163 +
164 + adapter := caddyfile.Adapter{
165 + ServerType: ServerType{},
166 + }
167 +
168 + _, _, err := adapter.Adapt([]byte(tc.input), nil)
169 +
170 + if err != nil != tc.expectError {
171 + t.Errorf("Test %d error expectation failed Expected: %v, got %s", i, tc.expectError, err)
172 + continue
173 + }
174 + }
175 +}