forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
113 lines (93 loc) · 2.58 KB
/
client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
package client
import (
"crypto/sha1"
"fmt"
"io"
"net/http"
"net/url"
"path"
"strings"
davconf "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-davcli/config"
bosherr "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/errors"
boshhttp "github.com/cloudfoundry/bosh-init/internal/github.com/cloudfoundry/bosh-utils/http"
)
type Client interface {
Get(path string) (content io.ReadCloser, err error)
Put(path string, content io.ReadCloser, contentLength int64) (err error)
}
func NewClient(config davconf.Config, httpClient boshhttp.Client) (c Client) {
return client{
config: config,
httpClient: httpClient,
}
}
type client struct {
config davconf.Config
httpClient boshhttp.Client
}
func (c client) Get(path string) (content io.ReadCloser, err error) {
req, err := c.createReq("GET", path, nil)
if err != nil {
return
}
resp, err := c.httpClient.Do(req)
if err != nil {
err = bosherr.WrapErrorf(err, "Getting dav blob %s", path)
return
}
if resp.StatusCode != 200 {
err = fmt.Errorf("Getting dav blob %s: Wrong response code: %d; body: %s", path, resp.StatusCode, c.readAndTruncateBody(resp))
return
}
content = resp.Body
return
}
func (c client) Put(path string, content io.ReadCloser, contentLength int64) (err error) {
req, err := c.createReq("PUT", path, content)
if err != nil {
return
}
defer content.Close()
req.ContentLength = contentLength
resp, err := c.httpClient.Do(req)
if err != nil {
err = bosherr.WrapErrorf(err, "Putting dav blob %s", path)
return
}
if resp.StatusCode != 201 && resp.StatusCode != 204 {
err = fmt.Errorf("Putting dav blob %s: Wrong response code: %d; body: %s", path, resp.StatusCode, c.readAndTruncateBody(resp))
return
}
return
}
func (c client) createReq(method, blobID string, body io.Reader) (req *http.Request, err error) {
blobURL, err := url.Parse(c.config.Endpoint)
if err != nil {
return
}
digester := sha1.New()
digester.Write([]byte(blobID))
blobPrefix := fmt.Sprintf("%02x", digester.Sum(nil)[0])
newPath := path.Join(blobURL.Path, blobPrefix, blobID)
if !strings.HasPrefix(newPath, "/") {
newPath = "/" + newPath
}
blobURL.Path = newPath
req, err = http.NewRequest(method, blobURL.String(), body)
if err != nil {
return
}
req.SetBasicAuth(c.config.User, c.config.Password)
return
}
func (c client) readAndTruncateBody(resp *http.Response) string {
body := ""
if resp.Body != nil {
buf := make([]byte, 1024)
n, err := resp.Body.Read(buf)
if err == io.EOF || err == nil {
body = string(buf[0:n])
}
}
return body
}