Commit 74b75803 authored by Matthew Holt's avatar Matthew Holt

redir: Allows replacements; defaults to exact match redirects

This is a breaking change for those who expect catch-all redirects to preserve path; use {uri} variable explicitly now
parent 04571ff3
......@@ -6,9 +6,6 @@ import (
"fmt"
"html"
"net/http"
"net/url"
"path"
"strings"
"github.com/mholt/caddy/middleware"
)
......@@ -22,36 +19,13 @@ type Redirect struct {
// ServeHTTP implements the middleware.Handler interface.
func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for _, rule := range rd.Rules {
if rule.From == "/" {
// Catchall redirect preserves path and query string
toURL, err := url.Parse(rule.To)
if err != nil {
return http.StatusInternalServerError, err
}
newPath := path.Join(toURL.Host, toURL.Path, r.URL.Path)
if strings.HasSuffix(r.URL.Path, "/") {
newPath = newPath + "/"
}
newPath = toURL.Scheme + "://" + newPath
parameters := toURL.Query()
for k, v := range r.URL.Query() {
parameters.Set(k, v[0])
}
if len(parameters) > 0 {
newPath = newPath + "?" + parameters.Encode()
}
if rule.Meta {
fmt.Fprintf(w, metaRedir, html.EscapeString(newPath))
} else {
http.Redirect(w, r, newPath, rule.Code)
}
return 0, nil
}
if r.URL.Path == rule.From {
if rule.From == "/" || r.URL.Path == rule.From {
to := middleware.NewReplacer(r, nil, "").Replace(rule.To)
if rule.Meta {
fmt.Fprintf(w, metaRedir, html.EscapeString(rule.To))
safeTo := html.EscapeString(to)
fmt.Fprintf(w, metaRedir, safeTo, safeTo)
} else {
http.Redirect(w, r, rule.To, rule.Code)
http.Redirect(w, r, to, rule.Code)
}
return 0, nil
}
......@@ -66,9 +40,13 @@ type Rule struct {
Meta bool
}
var metaRedir = `<html>
<head>
<meta http-equiv="refresh" content="0;URL='%s'">
</head>
<body>redirecting...</body>
// 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>
<html>
<head>
<script>window.location.replace("%s");</script>
<meta http-equiv="refresh" content="0; URL='%s'">
</head>
<body>Redirecting...</body>
</html>`
......@@ -10,16 +10,34 @@ import (
"github.com/mholt/caddy/middleware"
)
func TestMetaRedirect(t *testing.T) {
re := Redirect{
Rules: []Rule{
{From: "/", Meta: true, To: "https://example.com/"},
{From: "/whatever", Meta: true, To: "https://example.com/whatever"},
},
}
func TestRedirect(t *testing.T) {
for i, test := range []struct {
from string
expectedLocation string
}{
{"/from", "/to"},
{"/a", "/b"},
{"/aa", ""},
{"/", ""},
{"/a?foo=bar", "/b"},
{"/asdf?foo=bar", ""},
{"/foo#bar", ""},
{"/a#foo", "/b"},
} {
var nextCalled bool
for i, test := range re.Rules {
req, err := http.NewRequest("GET", test.From, nil)
re := Redirect{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
nextCalled = true
return 0, nil
}),
Rules: []Rule{
{From: "/from", To: "/to"},
{From: "/a", To: "/b"},
},
}
req, err := http.NewRequest("GET", test.from, nil)
if err != nil {
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
}
......@@ -27,14 +45,13 @@ func TestMetaRedirect(t *testing.T) {
rec := httptest.NewRecorder()
re.ServeHTTP(rec, req)
body, err := ioutil.ReadAll(rec.Body)
if err != nil {
t.Fatalf("Test %d: Could not read HTTP response body: %v", i, err)
if rec.Header().Get("Location") != test.expectedLocation {
t.Errorf("Test %d: Expected Location header to be %q but was %q",
i, test.expectedLocation, rec.Header().Get("Location"))
}
expectedSnippet := `<meta http-equiv="refresh" content="0;URL='` + test.To + `'">`
if !bytes.Contains(body, []byte(expectedSnippet)) {
t.Errorf("Test %d: Expected Response Body to contain %q but was %q",
i, expectedSnippet, body)
if nextCalled && test.expectedLocation != "" {
t.Errorf("Test %d: Next handler was unexpectedly called", i)
}
}
}
......@@ -42,7 +59,7 @@ func TestMetaRedirect(t *testing.T) {
func TestParametersRedirect(t *testing.T) {
re := Redirect{
Rules: []Rule{
{From: "/", Meta: false, To: "http://example.com/"},
{From: "/", Meta: false, To: "http://example.com{uri}"},
},
}
......@@ -54,13 +71,13 @@ func TestParametersRedirect(t *testing.T) {
rec := httptest.NewRecorder()
re.ServeHTTP(rec, req)
if "http://example.com/a?b=c" != rec.Header().Get("Location") {
if rec.Header().Get("Location") != "http://example.com/a?b=c" {
t.Fatalf("Test: expected location header %q but was %q", "http://example.com/a?b=c", rec.Header().Get("Location"))
}
re = Redirect{
Rules: []Rule{
{From: "/", Meta: false, To: "http://example.com/a?b=c"},
{From: "/", Meta: false, To: "http://example.com/a{path}?b=c&{query}"},
},
}
......@@ -76,34 +93,16 @@ func TestParametersRedirect(t *testing.T) {
}
}
func TestRedirect(t *testing.T) {
for i, test := range []struct {
from string
expectedLocation string
}{
{"/from", "/to"},
{"/a", "/b"},
{"/aa", ""},
{"/", ""},
{"/a?foo=bar", "/b"},
{"/asdf?foo=bar", ""},
{"/foo#bar", ""},
{"/a#foo", "/b"},
} {
var nextCalled bool
re := Redirect{
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
nextCalled = true
return 0, nil
}),
Rules: []Rule{
{From: "/from", To: "/to"},
{From: "/a", To: "/b"},
},
}
func TestMetaRedirect(t *testing.T) {
re := Redirect{
Rules: []Rule{
{From: "/whatever", Meta: true, To: "/something"},
{From: "/", Meta: true, To: "https://example.com/"},
},
}
req, err := http.NewRequest("GET", test.from, nil)
for i, test := range re.Rules {
req, err := http.NewRequest("GET", test.From, nil)
if err != nil {
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
}
......@@ -111,13 +110,14 @@ func TestRedirect(t *testing.T) {
rec := httptest.NewRecorder()
re.ServeHTTP(rec, req)
if rec.Header().Get("Location") != test.expectedLocation {
t.Errorf("Test %d: Expected Location header to be %q but was %q",
i, test.expectedLocation, rec.Header().Get("Location"))
body, err := ioutil.ReadAll(rec.Body)
if err != nil {
t.Fatalf("Test %d: Could not read HTTP response body: %v", i, err)
}
if nextCalled && test.expectedLocation != "" {
t.Errorf("Test %d: Next handler was unexpectedly called", i)
expectedSnippet := `<meta http-equiv="refresh" content="0; URL='` + test.To + `'">`
if !bytes.Contains(body, []byte(expectedSnippet)) {
t.Errorf("Test %d: Expected Response Body to contain %q but was %q",
i, expectedSnippet, body)
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment