-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
48 lines (45 loc) · 1.04 KB
/
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
package starenv
import (
"errors"
"io"
"net/http"
"net/url"
)
// HTTP derefs a URL to its content. Default tag for this derefer is "https".
// Tag "http" exists for the insecure config.
type HTTP struct {
// By default, if no scheme is provided, https is assumed. Setting this to
// true will instead assume http.
DefaultInsecure bool
}
// Deref treats ref as a URL and returns the response body of a GET request.
// You can leave off the scheme and take advantange of the default tag for this derefer:
//
// *https://example.com
//
// instead of:
//
// *https:https://example.com
func (h *HTTP) Deref(ref string) (string, error) {
u, err := url.Parse(ref)
if err != nil {
return "", errors.New("cannot parse " + ref + " as url")
}
if u.Scheme == "" {
if h.DefaultInsecure {
u.Scheme = "http"
} else {
u.Scheme = "https"
}
}
resp, err := http.Get(u.String())
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close }()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}