Skip to content

Commit

Permalink
chore: editoral tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Sep 22, 2022
1 parent 3c1a133 commit c717225
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ from to [status]
# Redirect with a 302
/my-redirect / 302

# Rewrite a path
/pass-through /index.html 200
# Redirect with wildcard (splat placeholder)
/splat/* /redirected-splat/:splat 301

# Redirect with multiple named placeholder
/posts/:year/:month/:day/:title /articles/:year/:month/:day/:title 301

# Show a custom 404 for everything under this path
/ecommerce/* /store-closed 404
/ecommerce/* /store-closed.html 404

# Single page app rewrite
# Single page app rewrite (SPA, PWA)
/* /index.html 200

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

## Notes for contributors

- `make all` builds and runs tests
- `FUZZTIME=1m make fuzz` runs fuzzing for specified amount of time

---

## Credit
Expand Down
13 changes: 5 additions & 8 deletions redirects.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
// 64 KiB
const maxFileSizeInBytes = 65536

// 64 KiB
const maxFileSizeInBytes = 65536

// A Rule represents a single redirection or rewrite rule.
type Rule struct {
// From is the path which is matched to perform the rule.
Expand Down Expand Up @@ -129,11 +126,11 @@ func Parse(r io.Reader) (rules []Rule, err error) {

// missing dst
if len(fields) <= 1 {
return nil, fmt.Errorf("missing destination path: %q", line)
return nil, fmt.Errorf("missing 'to' path: %q", line)
}

if len(fields) > 3 {
return nil, fmt.Errorf("must match format `from to [status][!]`")
return nil, fmt.Errorf("must match format 'from to [status]'")
}

// src and dst
Expand All @@ -145,18 +142,18 @@ func Parse(r io.Reader) (rules []Rule, err error) {

// from
if !strings.HasPrefix(rule.From, "/") {
return nil, fmt.Errorf("from path must begin with '/'")
return nil, fmt.Errorf("'from' path must begin with '/'")
}

if strings.Contains(rule.From, "*") && !strings.HasSuffix(rule.From, "*") {
return nil, fmt.Errorf("from path can only end with splat")
return nil, fmt.Errorf("'from' path can only end with splat")
}

// to
if !strings.HasPrefix(rule.To, "/") {
_, err := url.Parse(rule.To)
if err != nil {
return nil, errors.Wrapf(err, "invalid to path")
return nil, errors.Wrapf(err, "invalid 'to' path")
}
}

Expand Down

0 comments on commit c717225

Please sign in to comment.