-
Notifications
You must be signed in to change notification settings - Fork 40
/
request_url.go
67 lines (52 loc) · 1.35 KB
/
request_url.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
package pusher
import (
"net/url"
"strings"
)
const authVersion = "1.0"
func unsignedParams(key, timestamp string, body []byte, parameters map[string]string) url.Values {
params := url.Values{
"auth_key": {key},
"auth_timestamp": {timestamp},
"auth_version": {authVersion},
}
if body != nil {
params.Add("body_md5", md5Signature(body))
}
if parameters != nil {
for key, values := range parameters {
params.Add(key, values)
}
}
return params
}
func unescapeURL(_url url.Values) string {
unesc, _ := url.QueryUnescape(_url.Encode())
return unesc
}
func createRequestURL(method, host, path, key, secret, timestamp string, secure bool, body []byte, parameters map[string]string, cluster string) (string, error) {
params := unsignedParams(key, timestamp, body, parameters)
stringToSign := strings.Join([]string{method, path, unescapeURL(params)}, "\n")
authSignature := hmacSignature(stringToSign, secret)
params.Add("auth_signature", authSignature)
if host == "" {
if cluster != "" {
host = "api-" + cluster + ".pusher.com"
} else {
host = "api.pusherapp.com"
}
}
var base string
if secure {
base = "https://"
} else {
base = "http://"
}
base += host
endpoint, err := url.ParseRequestURI(base + path)
if err != nil {
return "", err
}
endpoint.RawQuery = unescapeURL(params)
return endpoint.String(), nil
}