-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_formatter.go
52 lines (42 loc) · 1.01 KB
/
path_formatter.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
package surl
import (
"net/url"
"strings"
)
type pathFormatter struct{}
func (f *pathFormatter) addExpiry(unsigned *url.URL, expiry string) {
unsigned.Path = expiry + unsigned.Path
}
func (f *pathFormatter) buildPayload(u url.URL, opts payloadOptions) string {
if opts.skipQuery {
u.RawQuery = ""
}
if opts.skipScheme {
u.Scheme = ""
}
return u.String()
}
func (f *pathFormatter) addSignature(payload *url.URL, sig string) {
payload.Path = "/" + sig + "." + payload.Path
}
func (f *pathFormatter) extractSignature(u *url.URL) (string, error) {
// prise apart sig and payload
sig, payload, found := strings.Cut(u.Path, ".")
if !found {
return "", ErrInvalidFormat
}
// remove leading /
sig = sig[1:]
u.Path = payload
return sig, nil
}
func (*pathFormatter) extractExpiry(u *url.URL) (string, error) {
// prise apart expiry and data
expiry, path, found := strings.Cut(u.Path, "/")
if !found {
return "", ErrInvalidFormat
}
// add leading slash back to path
u.Path = "/" + path
return expiry, nil
}