This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
61 lines (54 loc) · 1.77 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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package downloader
import (
"context"
"io"
"net/http"
"net/url"
"os"
"github.com/juju/errors"
jujuhttp "github.com/juju/http/v2"
"github.com/juju/utils/v2"
)
// NewHTTPBlobOpener returns a blob opener func suitable for use with
// Download. The opener func uses an HTTP client that enforces the
// provided SSL hostname verification policy.
func NewHTTPBlobOpener(hostnameVerification utils.SSLHostnameVerification) func(*url.URL) (io.ReadCloser, error) {
return func(url *url.URL) (io.ReadCloser, error) {
// TODO(rog) make the download operation interruptible.
client := jujuhttp.NewClient(
jujuhttp.WithSkipHostnameVerification(!bool(hostnameVerification)),
)
resp, err := client.Get(context.TODO(), url.String())
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
// resp.Body is always non-nil. (see https://golang.org/pkg/net/http/#Response)
_ = resp.Body.Close()
// Blob is pending to be downloaded
if resp.StatusCode == http.StatusConflict {
return nil, errors.NotYetAvailablef("blob contents")
}
return nil, errors.Errorf("bad http response: %v", resp.Status)
}
return resp.Body, nil
}
}
// NewSha256Verifier returns a verifier suitable for Request. The
// verifier checks the SHA-256 checksum of the file to ensure that it
// matches the one returned by the provided func.
func NewSha256Verifier(expected string) func(*os.File) error {
return func(file *os.File) error {
actual, _, err := utils.ReadSHA256(file)
if err != nil {
return errors.Trace(err)
}
if actual != expected {
err := errors.Errorf("expected sha256 %q, got %q", expected, actual)
return errors.NewNotValid(err, "")
}
return nil
}
}