/
restful.go
267 lines (244 loc) · 6.38 KB
/
restful.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
/*
* JuiceFS, Copyright 2018 Juicedata, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package object
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/viki-org/dnscache"
)
var resolver = dnscache.New(time.Minute)
var httpClient *http.Client
func init() {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: time.Second * 20,
ResponseHeaderTimeout: time.Second * 30,
IdleConnTimeout: time.Second * 300,
MaxIdleConnsPerHost: 500,
ReadBufferSize: 32 << 10,
WriteBufferSize: 32 << 10,
Dial: func(network string, address string) (net.Conn, error) {
separator := strings.LastIndex(address, ":")
host := address[:separator]
port := address[separator:]
ips, err := resolver.Fetch(host)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("No such host: %s", host)
}
var conn net.Conn
n := len(ips)
first := rand.Intn(n)
dialer := &net.Dialer{Timeout: time.Second * 10}
for i := 0; i < n; i++ {
ip := ips[(first+i)%n]
address = ip.String()
if port != "" {
address = net.JoinHostPort(address, port[1:])
}
conn, err = dialer.Dial(network, address)
if err == nil {
return conn, nil
}
}
return nil, err
},
DisableCompression: true,
TLSClientConfig: &tls.Config{},
},
Timeout: time.Hour,
}
}
func GetHttpClient() *http.Client {
return httpClient
}
func cleanup(response *http.Response) {
if response != nil && response.Body != nil {
_, _ = io.ReadAll(response.Body)
_ = response.Body.Close()
}
}
type RestfulStorage struct {
DefaultObjectStorage
endpoint string
accessKey string
secretKey string
signName string
signer func(*http.Request, string, string, string)
}
func (s *RestfulStorage) String() string {
return s.endpoint
}
var HEADER_NAMES = []string{"Content-MD5", "Content-Type", "Date"}
// RequestURL is fully url of api request
func sign(req *http.Request, accessKey, secretKey, signName string) {
if accessKey == "" {
return
}
toSign := req.Method + "\n"
for _, n := range HEADER_NAMES {
toSign += req.Header.Get(n) + "\n"
}
bucket := strings.Split(req.URL.Host, ".")[0]
toSign += "/" + bucket + req.URL.Path
h := hmac.New(sha1.New, []byte(secretKey))
_, _ = h.Write([]byte(toSign))
sig := base64.StdEncoding.EncodeToString(h.Sum(nil))
token := signName + " " + accessKey + ":" + sig
req.Header.Add("Authorization", token)
}
func (s *RestfulStorage) request(method, key string, body io.Reader, headers map[string]string) (*http.Response, error) {
uri := s.endpoint + "/" + key
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
if f, ok := body.(*os.File); ok {
st, err := f.Stat()
if err == nil {
req.ContentLength = st.Size()
}
}
now := time.Now().UTC().Format(http.TimeFormat)
req.Header.Add("Date", now)
for key := range headers {
req.Header.Add(key, headers[key])
}
s.signer(req, s.accessKey, s.secretKey, s.signName)
return httpClient.Do(req)
}
func parseError(resp *http.Response) error {
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("request failed: %s", err)
}
return fmt.Errorf("status: %v, message: %s", resp.StatusCode, string(data))
}
func (s *RestfulStorage) Head(key string) (Object, error) {
resp, err := s.request("HEAD", key, nil, nil)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusNotFound {
return nil, os.ErrNotExist
}
defer cleanup(resp)
if resp.StatusCode != 200 {
return nil, parseError(resp)
}
lastModified := resp.Header.Get("Last-Modified")
if lastModified == "" {
return nil, fmt.Errorf("cannot get last modified time")
}
mtime, _ := time.Parse(time.RFC1123, lastModified)
return &obj{
key,
resp.ContentLength,
mtime,
strings.HasSuffix(key, "/"),
"",
}, nil
}
func getRange(off, limit int64) string {
if off > 0 || limit > 0 {
if limit > 0 {
return fmt.Sprintf("bytes=%d-%d", off, off+limit-1)
} else {
return fmt.Sprintf("bytes=%d-", off)
}
}
return ""
}
func checkGetStatus(statusCode int, partial bool) error {
var expected = http.StatusOK
if partial {
expected = http.StatusPartialContent
}
if statusCode != expected {
return fmt.Errorf("expected status code %d, but got %d", expected, statusCode)
}
return nil
}
func (s *RestfulStorage) Get(key string, off, limit int64) (io.ReadCloser, error) {
headers := make(map[string]string)
if off > 0 || limit > 0 {
headers["Range"] = getRange(off, limit)
}
resp, err := s.request("GET", key, nil, headers)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 && resp.StatusCode != 206 {
return nil, parseError(resp)
}
if err = checkGetStatus(resp.StatusCode, len(headers) > 0); err != nil {
_ = resp.Body.Close()
return nil, err
}
return resp.Body, nil
}
func (u *RestfulStorage) Put(key string, body io.Reader) error {
resp, err := u.request("PUT", key, body, nil)
if err != nil {
return err
}
defer cleanup(resp)
if resp.StatusCode != 201 && resp.StatusCode != 200 {
return parseError(resp)
}
return nil
}
func (s *RestfulStorage) Copy(dst, src string) error {
in, err := s.Get(src, 0, -1)
if err != nil {
return err
}
defer in.Close()
d, err := io.ReadAll(in)
if err != nil {
return err
}
return s.Put(dst, bytes.NewReader(d))
}
func (s *RestfulStorage) Delete(key string) error {
resp, err := s.request("DELETE", key, nil, nil)
if err != nil {
return err
}
defer cleanup(resp)
if resp.StatusCode != 204 && resp.StatusCode != 404 {
return parseError(resp)
}
return nil
}
func (s *RestfulStorage) List(prefix, marker, delimiter string, limit int64, followLink bool) ([]Object, error) {
return nil, notSupported
}
var _ ObjectStorage = &RestfulStorage{}