forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.go
74 lines (63 loc) · 1.6 KB
/
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
68
69
70
71
72
73
74
package elasticsearch
import (
"fmt"
"net/url"
"strings"
)
func addToURL(url, path, pipeline string, params map[string]string) string {
if strings.HasSuffix(url, "/") && strings.HasPrefix(path, "/") {
url = strings.TrimSuffix(url, "/")
}
if len(params) == 0 && pipeline == "" {
return url + path
}
return strings.Join([]string{
url, path, "?", urlEncode(pipeline, params),
}, "")
}
// Encode parameters in url
func urlEncode(pipeline string, params map[string]string) string {
values := url.Values{}
for key, val := range params {
values.Add(key, string(val))
}
if pipeline != "" {
values.Add("pipeline", pipeline)
}
return values.Encode()
}
// Create path out of index, docType and id that is used for querying Elasticsearch
func makePath(index string, docType string, id string) (string, error) {
var path string
if len(docType) > 0 {
if len(id) > 0 {
path = fmt.Sprintf("/%s/%s/%s", index, docType, id)
} else {
path = fmt.Sprintf("/%s/%s", index, docType)
}
} else {
if len(id) > 0 {
if len(index) > 0 {
path = fmt.Sprintf("/%s/%s", index, id)
} else {
path = fmt.Sprintf("/%s", id)
}
} else {
path = fmt.Sprintf("/%s", index)
}
}
return path, nil
}
// TODO: make this reusable. Same definition in elasticsearch monitoring module
func parseProxyURL(raw string) (*url.URL, error) {
if raw == "" {
return nil, nil
}
url, err := url.Parse(raw)
if err == nil && strings.HasPrefix(url.Scheme, "http") {
return url, err
}
// Proxy was bogus. Try prepending "http://" to it and
// see if that parses correctly.
return url.Parse("http://" + raw)
}