forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource_http.go
100 lines (93 loc) · 2.3 KB
/
datasource_http.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
package data
import (
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
)
func buildURL(base *url.URL, args ...string) (*url.URL, error) {
if len(args) == 0 {
return base, nil
}
p, err := url.Parse(args[0])
if err != nil {
return nil, errors.Wrapf(err, "bad sub-path %s", args[0])
}
return base.ResolveReference(p), nil
}
func readHTTP(source *Source, args ...string) ([]byte, error) {
if source.hc == nil {
source.hc = &http.Client{Timeout: time.Second * 5}
}
u, err := buildURL(source.URL, args...)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req.Header = source.header
res, err := source.hc.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
err := errors.Errorf("Unexpected HTTP status %d on GET from %s: %s", res.StatusCode, source.URL, string(body))
return nil, err
}
ctypeHdr := res.Header.Get("Content-Type")
if ctypeHdr != "" {
mediatype, _, e := mime.ParseMediaType(ctypeHdr)
if e != nil {
return nil, e
}
source.mediaType = mediatype
}
return body, nil
}
func parseHeaderArgs(headerArgs []string) (map[string]http.Header, error) {
headers := make(map[string]http.Header)
for _, v := range headerArgs {
ds, name, value, err := splitHeaderArg(v)
if err != nil {
return nil, err
}
if _, ok := headers[ds]; !ok {
headers[ds] = make(http.Header)
}
headers[ds][name] = append(headers[ds][name], strings.TrimSpace(value))
}
return headers, nil
}
func splitHeaderArg(arg string) (datasourceAlias, name, value string, err error) {
parts := strings.SplitN(arg, "=", 2)
if len(parts) != 2 {
err = errors.Errorf("Invalid datasource-header option '%s'", arg)
return "", "", "", err
}
datasourceAlias = parts[0]
name, value, err = splitHeader(parts[1])
return datasourceAlias, name, value, err
}
func splitHeader(header string) (name, value string, err error) {
parts := strings.SplitN(header, ":", 2)
if len(parts) != 2 {
err = errors.Errorf("Invalid HTTP Header format '%s'", header)
return "", "", err
}
name = http.CanonicalHeaderKey(parts[0])
value = parts[1]
return name, value, nil
}