-
Notifications
You must be signed in to change notification settings - Fork 50
/
utils.go
80 lines (67 loc) · 1.41 KB
/
utils.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
package goseaweedfs
import (
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func parseURI(uri string) (u *url.URL, err error) {
u, err = url.Parse(uri)
if err == nil && u.Scheme == "" {
u.Scheme = "http"
}
return
}
func encodeURI(base url.URL, path string, args url.Values) string {
base.Path = path
query := base.Query()
args = normalize(args, "", "")
for k, vs := range args {
for _, v := range vs {
query.Add(k, v)
}
}
base.RawQuery = query.Encode()
return base.String()
}
func valid(c rune) bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || '.' == c || '-' == c || '_' == c
}
func normalizeName(st string) string {
for _, _c := range st {
if !valid(_c) {
var sb strings.Builder
sb.Grow(len(st))
for _, c := range st {
if valid(c) {
_, _ = sb.WriteRune(c)
}
}
return sb.String()
}
}
return st
}
func drainAndClose(body io.ReadCloser) {
_, _ = io.Copy(ioutil.Discard, body)
_ = body.Close()
}
func normalize(values url.Values, collection, ttl string) url.Values {
if values == nil {
values = make(url.Values)
}
if len(collection) > 0 {
values.Set(ParamCollection, collection)
}
if len(ttl) > 0 {
values.Set(ParamTTL, ttl)
}
return values
}
func readAll(r *http.Response) (body []byte, statusCode int, err error) {
statusCode = r.StatusCode
body, err = ioutil.ReadAll(r.Body)
r.Body.Close()
return
}