-
Notifications
You must be signed in to change notification settings - Fork 496
/
http.go
136 lines (120 loc) · 2.84 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package storage
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
"github.com/stellar/go/support/errors"
)
type HttpStorage struct {
ctx context.Context
client http.Client
base url.URL
userAgent string
}
func NewHttpStorage(ctx context.Context, base *url.URL, userAgent string) Storage {
return &HttpStorage{
ctx: ctx,
base: *base,
userAgent: userAgent,
}
}
func (b *HttpStorage) GetFile(pth string) (io.ReadCloser, error) {
derived := b.base
derived.Path = path.Join(derived.Path, pth)
resp, err := b.makeSendRequest("GET", derived.String())
if err != nil {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return nil, err
}
err = checkResp(resp)
if err != nil {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
return nil, err
}
return resp.Body, nil
}
func (b *HttpStorage) Head(pth string) (*http.Response, error) {
derived := b.base
derived.Path = path.Join(derived.Path, pth)
resp, err := b.makeSendRequest("HEAD", derived.String())
if err != nil {
return nil, err
}
if resp.Body != nil {
resp.Body.Close()
}
return resp, nil
}
func (b *HttpStorage) Exists(pth string) (bool, error) {
resp, err := b.Head(pth)
if err != nil {
return false, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return true, nil
} else if resp.StatusCode == http.StatusNotFound {
return false, nil
} else {
return false, errors.Errorf("Unknown status code=%d", resp.StatusCode)
}
}
func (b *HttpStorage) Size(pth string) (int64, error) {
resp, err := b.Head(pth)
if err != nil {
return 0, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return resp.ContentLength, nil
} else if resp.StatusCode == http.StatusNotFound {
return 0, nil
} else {
return 0, errors.Errorf("Unknown status code=%d", resp.StatusCode)
}
}
func (b *HttpStorage) PutFile(pth string, in io.ReadCloser) error {
in.Close()
return errors.New("PutFile not available over HTTP")
}
func (b *HttpStorage) ListFiles(pth string) (chan string, chan error) {
ch := make(chan string)
er := make(chan error)
close(ch)
er <- errors.New("ListFiles not available over HTTP")
close(er)
return ch, er
}
func (b *HttpStorage) CanListFiles() bool {
return false
}
func (b *HttpStorage) Close() error {
return nil
}
func (b *HttpStorage) makeSendRequest(method, url string) (*http.Response, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(b.ctx)
logReq(req)
if b.userAgent != "" {
req.Header.Set("User-Agent", b.userAgent)
}
resp, err := b.client.Do(req)
logResp(resp)
return resp, err
}
func checkResp(r *http.Response) error {
if r.StatusCode >= 200 && r.StatusCode < 400 {
return nil
} else {
return fmt.Errorf("bad HTTP response '%s' for %s '%s'",
r.Status, r.Request.Method, r.Request.URL.String())
}
}