-
Notifications
You must be signed in to change notification settings - Fork 2
/
pattern.go
78 lines (67 loc) · 1.89 KB
/
pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package http
import (
"errors"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"go.autokitteh.dev/autokitteh/internal/kittehs"
"go.autokitteh.dev/autokitteh/sdk/sdkerrors"
)
// Extract path keys from path. This follows Go's convention for paths
// as described in https://pkg.go.dev/net/http#ServeMux.
// Essentially, extract the content of any curly braces in the path.
// If a key has a "..." suffix, it is removed.
// If a key is "$", it is ignored.
func extractPathKeys(s string) (keys []string, err error) {
var (
curr strings.Builder
inside bool
)
for _, r := range s {
if inside {
if r != '}' {
curr.WriteRune(r)
} else {
key := strings.TrimSuffix(curr.String(), "...")
if key == "" {
return nil, sdkerrors.NewInvalidArgumentError("empty key")
}
if key != "$" {
keys = append(keys, key)
}
curr.Reset()
inside = false
}
} else if r == '{' {
inside = true
}
}
if inside {
err = sdkerrors.NewInvalidArgumentError("unmatched '{'")
}
return
}
// Match request path against pattern. This uses go's http.ServeMux as described
// in https://pkg.go.dev/net/http#ServeMux. It will return a map of path keys
// to their values if the request path matches the pattern.
func MatchPattern(pattern string, req string) (vs map[string]string, err error) {
err = sdkerrors.ErrNotFound
var mux http.ServeMux
mux.Handle(pattern, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
var keys []string
if keys, err = extractPathKeys(pattern); err == nil {
vs = kittehs.ListToMap(keys, func(k string) (string, string) {
return k, r.PathValue(k)
})
}
}))
w := httptest.NewRecorder()
mux.ServeHTTP(w, &http.Request{URL: &url.URL{Path: req}})
if code := w.Result().StatusCode; code == http.StatusNotFound {
err = sdkerrors.ErrNotFound
} else if code != http.StatusOK {
err = errors.New(w.Result().Status)
}
return
}