Skip to content

Commit 1163e22

Browse files
committed
fix the behaviour of trailing slashes for paths
1 parent 078ae11 commit 1163e22

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/dpouris/goster
22

3-
go 1.18
3+
go 1.21

meta.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,26 @@ func (p *Path) Get(id string) (value string, exists bool) {
3636
}
3737

3838
// Pass in a `url` and see if there're parameters in it
39-
//
39+
//
4040
// If there're, ParseUrl will construct a Params struct and populate Meta.Query.Params
41-
//
41+
//
4242
// If there aren't any, ParseUrl will return the error that occurred
43-
//
43+
//
4444
// The `url` string reference that is passed in will have the parameters stripped in either case
4545
func (m *Meta) ParseUrl(url *string) (err error) {
4646
paramValues := make(map[string]string, 0)
4747
paramPattern := regexp.MustCompile(`\?.+(\/)?`)
48-
pathPattern := regexp.MustCompile(`^(\/\w+)+(\?)?`)
48+
pathPattern := regexp.MustCompile(`^(\/\w+)+(\/)*(\?)?`)
4949
defer func() {
5050
m.Query = Params{
5151
values: paramValues,
5252
}
53-
}()
54-
55-
defer func() {
56-
*url = strings.Trim(pathPattern.FindString(*url), "?")
53+
matchedStr := pathPattern.FindString(*url)
54+
if len(matchedStr) != 0 {
55+
*url = matchedStr
56+
}
57+
*url = strings.Trim(*url, "?")
58+
cleanPath(url)
5759
}()
5860

5961
params := paramPattern.FindString(*url)

utils.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@ func DefaultHeader(c *Ctx) {
1212
}
1313

1414
func cleanPath(path *string) {
15-
if *path == "/" {
16-
*path = ""
15+
if len(*path) == 0 {
1716
return
1817
}
1918

2019
if (*path)[0] != '/' {
2120
*path = "/" + *path
2221
}
2322

24-
*path = strings.TrimRightFunc(*path, func(r rune) bool {
25-
return r == '/'
26-
})
23+
*path = strings.TrimSuffix(*path, "/")
2724
}
2825

2926
func cleanEmptyBytes(b *[]byte) {

0 commit comments

Comments
 (0)