forked from folbricht/desync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotehttp.go
245 lines (223 loc) · 5.63 KB
/
remotehttp.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package desync
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"time"
"crypto/x509"
"github.com/pkg/errors"
)
// TrustInsecure determines if invalid certs presented by HTTP stores should
// be accepted.
var TrustInsecure bool
// RemoteHTTPBase is the base object for a remote, HTTP-based chunk or index stores.
type RemoteHTTPBase struct {
location *url.URL
client *http.Client
opt StoreOptions
}
// RemoteHTTP is a remote casync store accessed via HTTP.
type RemoteHTTP struct {
*RemoteHTTPBase
}
// NewRemoteHTTPStoreBase initializes a base object for HTTP index or chunk stores.
func NewRemoteHTTPStoreBase(location *url.URL, opt StoreOptions) (*RemoteHTTPBase, error) {
if location.Scheme != "http" && location.Scheme != "https" {
return nil, fmt.Errorf("unsupported scheme %s, expected http or https", location.Scheme)
}
// Make sure we have a trailing / on the path
u := *location
if !strings.HasSuffix(u.Path, "/") {
u.Path = u.Path + "/"
}
var tr *http.Transport
if opt.ClientCert != "" && opt.ClientKey != "" {
// Load client cert
certificate, err := tls.LoadX509KeyPair(opt.ClientCert, opt.ClientKey)
if err != nil {
return nil, fmt.Errorf("failed to load client certificate from %s", opt.ClientCert)
}
caCertPool, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to create CaCertPool")
}
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableCompression: true,
MaxIdleConnsPerHost: opt.N,
IdleConnTimeout: 60 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: TrustInsecure,
Certificates: []tls.Certificate{certificate},
RootCAs: caCertPool,
},
}
} else {
// Build a client with the right size connection pool and optionally disable
// certificate verification.
tr = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableCompression: true,
MaxIdleConnsPerHost: opt.N,
IdleConnTimeout: 60 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: TrustInsecure},
}
}
timeout := opt.Timeout
if timeout == 0 {
timeout = time.Minute
}
client := &http.Client{Transport: tr, Timeout: timeout}
return &RemoteHTTPBase{location: location, client: client, opt: opt}, nil
}
func (r *RemoteHTTPBase) String() string {
return r.location.String()
}
// Close the HTTP store. NOP operation but needed to implement the interface.
func (r *RemoteHTTPBase) Close() error { return nil }
// GetObject reads and returns an object in the form of []byte from the store
func (r *RemoteHTTPBase) GetObject(name string) ([]byte, error) {
u, _ := r.location.Parse(name)
var (
resp *http.Response
err error
attempt int
b []byte
)
for {
attempt++
resp, err = r.client.Get(u.String())
if err != nil {
if attempt >= r.opt.ErrorRetry {
return nil, errors.Wrap(err, u.String())
}
continue
}
defer resp.Body.Close()
switch resp.StatusCode {
case 200: // expected
case 404:
return nil, NoSuchObject{name}
default:
return nil, fmt.Errorf("unexpected status code %d from %s", resp.StatusCode, name)
}
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
if attempt >= r.opt.ErrorRetry {
return nil, errors.Wrap(err, u.String())
}
continue
}
break
}
return b, err
}
// StoreObject stores an object to the store.
func (r *RemoteHTTPBase) StoreObject(name string, rdr io.Reader) error {
u, _ := r.location.Parse(name)
var (
resp *http.Response
err error
attempt int
)
retry:
attempt++
req, err := http.NewRequest("PUT", u.String(), rdr)
if err != nil {
return err
}
resp, err = r.client.Do(req)
if err != nil {
if attempt >= r.opt.ErrorRetry {
return err
}
goto retry
}
defer resp.Body.Close()
msg, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return errors.New(string(msg))
}
return nil
}
// NewRemoteHTTPStore initializes a new store that pulls chunks via HTTP(S) from
// a remote web server. n defines the size of idle connections allowed.
func NewRemoteHTTPStore(location *url.URL, opt StoreOptions) (*RemoteHTTP, error) {
b, err := NewRemoteHTTPStoreBase(location, opt)
if err != nil {
return nil, err
}
return &RemoteHTTP{b}, nil
}
// GetChunk reads and returns one chunk from the store
func (r *RemoteHTTP) GetChunk(id ChunkID) (*Chunk, error) {
p := r.nameFromID(id)
b, err := r.GetObject(p)
if err != nil {
return nil, err
}
if r.opt.Uncompressed {
return NewChunkWithID(id, b, nil, r.opt.SkipVerify)
}
return NewChunkWithID(id, nil, b, r.opt.SkipVerify)
}
// HasChunk returns true if the chunk is in the store
func (r *RemoteHTTP) HasChunk(id ChunkID) bool {
p := r.nameFromID(id)
u, _ := r.location.Parse(p)
var (
resp *http.Response
err error
attempt int
)
retry:
attempt++
resp, err = r.client.Head(u.String())
if err != nil {
if attempt >= r.opt.ErrorRetry {
return false
}
goto retry
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
switch resp.StatusCode {
case 200:
return true
default:
return false
}
}
// StoreChunk adds a new chunk to the store
func (r *RemoteHTTP) StoreChunk(chunk *Chunk) error {
p := r.nameFromID(chunk.ID())
var (
b []byte
err error
)
if r.opt.Uncompressed {
b, err = chunk.Uncompressed()
} else {
b, err = chunk.Compressed()
}
if err != nil {
return err
}
return r.StoreObject(p, bytes.NewReader(b))
}
func (r *RemoteHTTP) nameFromID(id ChunkID) string {
sID := id.String()
name := path.Join(sID[0:4], sID)
if r.opt.Uncompressed {
name += UncompressedChunkExt
} else {
name += CompressedChunkExt
}
return name
}