-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
115 lines (95 loc) · 2.2 KB
/
options.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package server
import (
"context"
"net/http"
"net/url"
"strconv"
"github.com/gin-gonic/gin"
)
type Options struct {
URL string
Includes []Matcher
Excludes []Matcher
Merge bool
Error string
}
type calenderOptions struct {
url string
includes, excludes matchGroup
merge bool
}
func getCalendarOptions(ctx context.Context, getArray func(string) []string) (calenderOptions, error) {
var (
opts calenderOptions
err error
)
opts.merge, err = getBool(ctx, getArray, "mrg")
if err != nil {
return calenderOptions{}, err
}
opts.includes, err = parseMatchers(getArray("inc"))
if err != nil {
return calenderOptions{}, newErrorWithMessage(
http.StatusBadRequest,
"Bad inc argument: %s", err.Error(),
)
}
opts.excludes, err = parseMatchers(getArray("exc"))
if err != nil {
return calenderOptions{}, newErrorWithMessage(
http.StatusBadRequest,
"Bad exc argument: %s", err.Error(),
)
}
opts.url = getString(getArray, "cal")
return opts, nil
}
func getBool(ctx context.Context, getArray func(string) []string, key string) (bool, error) {
bs := getArray(key)
if len(bs) < 1 {
return false, nil
}
b, err := strconv.ParseBool(bs[0])
if err != nil {
log(ctx).Warnf("error getting %q parameter: %w", key, err)
return false, newErrorWithMessage(
http.StatusBadRequest,
"Bad argument %q for %q, should be boolean.", bs[0], key,
)
}
return b, nil
}
func getString(getArray func(string) []string, key string) string {
ss := getArray(key)
if len(ss) < 1 {
return ""
}
return ss[0]
}
func (c calenderOptions) Options() Options {
o := Options{
URL: c.url,
Merge: c.merge,
}
for _, i := range c.includes {
o.Includes = append(o.Includes, i.Matcher())
}
for _, e := range c.excludes {
o.Excludes = append(o.Excludes, e.Matcher())
}
return o
}
func clientURL(c *gin.Context) *url.URL {
u := c.Request.URL
u.Scheme = "webcal"
u.Host = c.GetHeader("X-HX-Host") // Host header is banned in XHR
u.Path = c.GetHeader("X-Forwarded-URI") + "/"
q := url.Values{
"cal": c.PostFormArray("cal"),
"inc": c.PostFormArray("inc"),
"exc": c.PostFormArray("exc"),
"mrg": c.PostFormArray("mrg"),
}
u.RawQuery = q.Encode()
return u
}