/
mds.go
335 lines (281 loc) · 7.82 KB
/
mds.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package mds
import (
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"golang.org/x/net/context"
"golang.org/x/net/context/ctxhttp"
)
// UploadInfo describes result of upload
type UploadInfo struct {
XMLName xml.Name `xml:"post"`
Obj string `xml:"obj,attr"`
ID string `xml:"id,attr"`
Key string `xml:"key,attr"`
Size uint64 `xml:"size,attr"`
Groups int `xml:"groups,attr"`
Complete []struct {
Addr string `xml:"addr,attr"`
Path string `xml:"path,attr"`
Group int `xml:"group,attr"`
Status int `xml:"status,attr"`
} `xml:"complete"`
Written int `xml:"written"`
}
func decodeXML(result interface{}, body io.Reader) error {
return xml.NewDecoder(body).Decode(result)
}
// DownloadInfo describes a direct link to a file
type DownloadInfo struct {
XMLName xml.Name `xml:"download-info"`
Host string `xml:"host"`
Path string `xml:"path"`
TS string `xml:"ts"`
Region int `xml:"region"`
Sign string `xml:"s"`
}
// URL constructs a direct link from DownloadInfo
func (d *DownloadInfo) URL() string {
return fmt.Sprintf("http://%s%s?ts=%ssign=%s", d.Host, d.Path, d.TS, d.Sign)
}
// Config represents configuration for the client
type Config struct {
Host string
UploadPort int
ReadPort int
AuthHeader string
}
// Client works with MDS
type Client struct {
Config
client *http.Client
}
// NewClient creates a client to MDS
func NewClient(config Config, client *http.Client) (*Client, error) {
if client == nil {
client = http.DefaultClient
}
if !(strings.HasPrefix(config.Host, "http://") || strings.HasPrefix(config.Host, "https://")) {
config.Host = "http://" + config.Host
}
return &Client{
Config: config,
client: client,
}, nil
}
func (m *Client) uploadURL(namespace, filename string) string {
return fmt.Sprintf("%s:%d/upload-%s/%s", m.Host, m.UploadPort, namespace, filename)
}
// ReadURL returns a URL which could be used to get data.
func (m *Client) ReadURL(ctx context.Context, namespace, filename string, resolveRedirect bool) (string, error) {
if !resolveRedirect {
return fmt.Sprintf("%s:%d/get-%s/%s", m.Host, m.ReadPort, namespace, filename), nil
}
rurl := fmt.Sprintf("%s:%d/get-%s/%s?redirect=yes", m.Host, m.ReadPort, namespace, filename)
var noRedirectClient = http.Client{
Transport: m.client.Transport,
Jar: m.client.Jar,
Timeout: m.client.Timeout,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := ctxhttp.Head(ctx, &noRedirectClient, rurl)
if err != nil {
return "", err
}
defer resp.Body.Close()
switch resp.StatusCode {
case 302, 307:
var durl *url.URL
durl, err = resp.Location()
if err != nil {
return "", err
}
return durl.String(), nil
default:
return "", fmt.Errorf("unexpected code for resolving redirect %d", resp.StatusCode)
}
}
func (m *Client) deleteURL(namespace, filename string) string {
return fmt.Sprintf("%s:%d/delete-%s/%s", m.Host, m.UploadPort, namespace, filename)
}
func (m *Client) pingURL() string {
return fmt.Sprintf("%s:%d/ping", m.Host, m.ReadPort)
}
func (m *Client) downloadinfoURL(namespace, filename string) string {
return fmt.Sprintf("%s:%d/downloadinfo-%s/%s", m.Host, m.ReadPort, namespace, filename)
}
func (m *Client) getRealURL() string {
return fmt.Sprintf("%s:%d/hostname", m.Host, m.UploadPort)
}
func (m *Client) GetReal(ctx context.Context) (string, error) {
urlStr := m.getRealURL()
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return "", err
}
req.Header.Add("Authorization", m.AuthHeader)
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
b, err := ioutil.ReadAll(resp.Body)
return string(b), err
}
scope := ErrorMethodScope{
Method: "getReal",
URL: urlStr,
}
return "", newMethodError(scope, resp)
}
// Upload stores provided data to a specified namespace. Returns information about upload.
func (m *Client) Upload(ctx context.Context, namespace string, filename string, size int64, body io.Reader) (*UploadInfo, error) {
urlStr := m.uploadURL(namespace, filename)
req, err := http.NewRequest("POST", urlStr, body)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", m.AuthHeader)
if req.ContentLength <= 0 {
req.ContentLength = size
}
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
scope := ErrorMethodScope{
Method: "upload",
URL: urlStr,
}
return nil, newMethodError(scope, resp)
}
var info UploadInfo
if err := decodeXML(&info, resp.Body); err != nil {
return nil, err
}
return &info, nil
}
// Get reads a given key from storage and return ReadCloser to body.
// User is responsible for closing returned ReadCloser.
func (m *Client) Get(ctx context.Context, namespace, key string, Range ...uint64) (io.ReadCloser, error) {
urlStr, err := m.ReadURL(ctx, namespace, key, false)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", m.AuthHeader)
switch len(Range) {
case 0:
case 1:
req.Header.Add("Range", fmt.Sprintf("bytes=%d-", Range[0]))
case 2:
req.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", Range[0], Range[1]))
default:
return nil, fmt.Errorf("Invalid range")
}
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusPartialContent {
return resp.Body, nil
}
defer resp.Body.Close()
scope := ErrorMethodScope{
Method: "get",
URL: urlStr,
}
return nil, newMethodError(scope, resp)
}
// GetFile is like Get but returns bytes.
func (m *Client) GetFile(ctx context.Context, namespace, key string, Range ...uint64) ([]byte, error) {
output, err := m.Get(ctx, namespace, key, Range...)
if err != nil {
return nil, err
}
defer output.Close()
return ioutil.ReadAll(output)
}
// Delete deletes key from namespace.
func (m *Client) Delete(ctx context.Context, namespace, key string) error {
urlStr := m.deleteURL(namespace, key)
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return err
}
req.Header.Add("Authorization", m.AuthHeader)
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
scope := ErrorMethodScope{
Method: "delete",
URL: urlStr,
}
return newMethodError(scope, resp)
}
return nil
}
// Ping checks availability of proxy
func (m *Client) Ping(ctx context.Context) error {
urlStr := m.pingURL()
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return err
}
req.Header.Add("Authorization", m.AuthHeader)
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
scope := ErrorMethodScope{
Method: "ping",
URL: urlStr,
}
return newMethodError(scope, resp)
}
return nil
}
// DownloadInfo retrieves an information about direct link to a file,
// if it's available.
func (m *Client) DownloadInfo(ctx context.Context, namespace, key string) (*DownloadInfo, error) {
urlStr := m.downloadinfoURL(namespace, key)
req, err := http.NewRequest("GET", urlStr, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", m.AuthHeader)
resp, err := ctxhttp.Do(ctx, m.client, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
scope := ErrorMethodScope{
Method: "downloadInfo",
URL: urlStr,
}
return nil, newMethodError(scope, resp)
}
var info DownloadInfo
if err := decodeXML(&info, resp.Body); err != nil {
return nil, err
}
return &info, nil
}