Skip to content

Commit ac36c51

Browse files
author
Justin Johnson
committed
- Remove forced redirects support
- Return a human-readable error is someone attempts to use forced redirects
1 parent 8b4073e commit ac36c51

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ Currently this is a subset of Netlify's [redirects format](https://www.netlify.c
77
The details of the supported functionality are still in flux and will eventually be included in a [spec](https://github.com/ipfs/specs).
88

99
## Format
10-
Currently only supports `from`, `to`, `status` and `force`.
10+
Currently only supports `from`, `to` and `status`.
1111

1212
```
13-
from to [status][!]
13+
from to [status]
1414
```
1515

1616
## Example
@@ -39,9 +39,6 @@ from to [status][!]
3939

4040
# Proxying
4141
/api/* https://api.example.com/:splat 200
42-
43-
# Forced redirect, even if the from path exists
44-
/app/* /app/index.html 200!
4542
```
4643

4744
---

redirects.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package redirects
33

44
import (
55
"bufio"
6+
"fmt"
67
"io"
78
"net/url"
89
"strconv"
@@ -29,10 +30,6 @@ type Rule struct {
2930
// When proxying this field is ignored.
3031
//
3132
Status int
32-
33-
// Force is used to force a rewrite or redirect even
34-
// when a response (or static file) is present.
35-
Force bool
3633
}
3734

3835
// IsRewrite returns true if the rule represents a rewrite (status 200).
@@ -97,13 +94,12 @@ func Parse(r io.Reader) (rules []Rule, err error) {
9794

9895
// status
9996
if len(fields) > 2 {
100-
code, force, err := parseStatus(fields[2])
97+
code, err := parseStatus(fields[2])
10198
if err != nil {
10299
return nil, errors.Wrapf(err, "parsing status %q", fields[2])
103100
}
104101

105102
rule.Status = code
106-
rule.Force = force
107103
}
108104

109105
rules = append(rules, rule)
@@ -118,13 +114,13 @@ func ParseString(s string) ([]Rule, error) {
118114
return Parse(strings.NewReader(s))
119115
}
120116

121-
// parseStatus returns the status code and force when "!" suffix is present.
122-
func parseStatus(s string) (code int, force bool, err error) {
117+
// parseStatus returns the status code.
118+
func parseStatus(s string) (code int, err error) {
123119
if strings.HasSuffix(s, "!") {
124-
force = true
125-
s = strings.Replace(s, "!", "", -1)
120+
// See https://docs.netlify.com/routing/redirects/rewrites-proxies/#shadowing
121+
return -1, fmt.Errorf("forced redirects (and \"shadowing\") are not supported by IPFS gateways")
126122
}
127123

128124
code, err = strconv.Atoi(s)
129-
return
125+
return code, err
130126
}

redirects_example_test.go

+10-29
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ func Example() {
3030
3131
# Proxying
3232
/api/* https://api.example.com/:splat 200
33-
34-
# Forcing
35-
/app/* /app/index.html 200!
3633
`))
3734

3835
enc := json.NewEncoder(os.Stdout)
@@ -43,68 +40,52 @@ func Example() {
4340
// {
4441
// "From": "/home",
4542
// "To": "/",
46-
// "Status": 301,
47-
// "Force": false
43+
// "Status": 301
4844
// },
4945
// {
5046
// "From": "/blog/my-post.php",
5147
// "To": "/blog/my-post",
52-
// "Status": 301,
53-
// "Force": false
48+
// "Status": 301
5449
// },
5550
// {
5651
// "From": "/news",
5752
// "To": "/blog",
58-
// "Status": 301,
59-
// "Force": false
53+
// "Status": 301
6054
// },
6155
// {
6256
// "From": "/google",
6357
// "To": "https://www.google.com",
64-
// "Status": 301,
65-
// "Force": false
58+
// "Status": 301
6659
// },
6760
// {
6861
// "From": "/home",
6962
// "To": "/",
70-
// "Status": 301,
71-
// "Force": false
63+
// "Status": 301
7264
// },
7365
// {
7466
// "From": "/my-redirect",
7567
// "To": "/",
76-
// "Status": 302,
77-
// "Force": false
68+
// "Status": 302
7869
// },
7970
// {
8071
// "From": "/pass-through",
8172
// "To": "/index.html",
82-
// "Status": 200,
83-
// "Force": false
73+
// "Status": 200
8474
// },
8575
// {
8676
// "From": "/ecommerce",
8777
// "To": "/store-closed",
88-
// "Status": 404,
89-
// "Force": false
78+
// "Status": 404
9079
// },
9180
// {
9281
// "From": "/*",
9382
// "To": "/index.html",
94-
// "Status": 200,
95-
// "Force": false
83+
// "Status": 200
9684
// },
9785
// {
9886
// "From": "/api/*",
9987
// "To": "https://api.example.com/:splat",
100-
// "Status": 200,
101-
// "Force": false
102-
// },
103-
// {
104-
// "From": "/app/*",
105-
// "To": "/app/index.html",
106-
// "Status": 200,
107-
// "Force": true
88+
// "Status": 200
10889
// }
10990
// ]
11091
}

redirects_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redirects
22

33
import (
4+
"strings"
45
"testing"
56

67
"github.com/tj/assert"
@@ -56,3 +57,13 @@ func TestRule_IsRewrite(t *testing.T) {
5657
assert.False(t, r.IsRewrite())
5758
})
5859
}
60+
61+
func TestParse(t *testing.T) {
62+
t.Run("with illegal force", func(t *testing.T) {
63+
_, err := Parse(strings.NewReader(`
64+
/home / 301!
65+
`))
66+
67+
assert.Error(t, err, "forced redirects should return an error")
68+
})
69+
}

0 commit comments

Comments
 (0)