Skip to content

Commit

Permalink
- Remove forced redirects support
Browse files Browse the repository at this point in the history
- Return a human-readable error is someone attempts to use forced redirects
  • Loading branch information
Justin Johnson committed Aug 11, 2022
1 parent 8b4073e commit ac36c51
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Currently this is a subset of Netlify's [redirects format](https://www.netlify.c
The details of the supported functionality are still in flux and will eventually be included in a [spec](https://github.com/ipfs/specs).

## Format
Currently only supports `from`, `to`, `status` and `force`.
Currently only supports `from`, `to` and `status`.

```
from to [status][!]
from to [status]
```

## Example
Expand Down Expand Up @@ -39,9 +39,6 @@ from to [status][!]

# Proxying
/api/* https://api.example.com/:splat 200

# Forced redirect, even if the from path exists
/app/* /app/index.html 200!
```

---
Expand Down
18 changes: 7 additions & 11 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redirects

import (
"bufio"
"fmt"
"io"
"net/url"
"strconv"
Expand All @@ -29,10 +30,6 @@ type Rule struct {
// When proxying this field is ignored.
//
Status int

// Force is used to force a rewrite or redirect even
// when a response (or static file) is present.
Force bool
}

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

// status
if len(fields) > 2 {
code, force, err := parseStatus(fields[2])
code, err := parseStatus(fields[2])
if err != nil {
return nil, errors.Wrapf(err, "parsing status %q", fields[2])
}

rule.Status = code
rule.Force = force
}

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

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

code, err = strconv.Atoi(s)
return
return code, err
}
39 changes: 10 additions & 29 deletions redirects_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ func Example() {
# Proxying
/api/* https://api.example.com/:splat 200
# Forcing
/app/* /app/index.html 200!
`))

enc := json.NewEncoder(os.Stdout)
Expand All @@ -43,68 +40,52 @@ func Example() {
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false
// "Status": 301
// },
// {
// "From": "/blog/my-post.php",
// "To": "/blog/my-post",
// "Status": 301,
// "Force": false
// "Status": 301
// },
// {
// "From": "/news",
// "To": "/blog",
// "Status": 301,
// "Force": false
// "Status": 301
// },
// {
// "From": "/google",
// "To": "https://www.google.com",
// "Status": 301,
// "Force": false
// "Status": 301
// },
// {
// "From": "/home",
// "To": "/",
// "Status": 301,
// "Force": false
// "Status": 301
// },
// {
// "From": "/my-redirect",
// "To": "/",
// "Status": 302,
// "Force": false
// "Status": 302
// },
// {
// "From": "/pass-through",
// "To": "/index.html",
// "Status": 200,
// "Force": false
// "Status": 200
// },
// {
// "From": "/ecommerce",
// "To": "/store-closed",
// "Status": 404,
// "Force": false
// "Status": 404
// },
// {
// "From": "/*",
// "To": "/index.html",
// "Status": 200,
// "Force": false
// "Status": 200
// },
// {
// "From": "/api/*",
// "To": "https://api.example.com/:splat",
// "Status": 200,
// "Force": false
// },
// {
// "From": "/app/*",
// "To": "/app/index.html",
// "Status": 200,
// "Force": true
// "Status": 200
// }
// ]
}
11 changes: 11 additions & 0 deletions redirects_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redirects

import (
"strings"
"testing"

"github.com/tj/assert"
Expand Down Expand Up @@ -56,3 +57,13 @@ func TestRule_IsRewrite(t *testing.T) {
assert.False(t, r.IsRewrite())
})
}

func TestParse(t *testing.T) {
t.Run("with illegal force", func(t *testing.T) {
_, err := Parse(strings.NewReader(`
/home / 301!
`))

assert.Error(t, err, "forced redirects should return an error")
})
}

0 comments on commit ac36c51

Please sign in to comment.