-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio.go
More file actions
560 lines (463 loc) · 14.1 KB
/
io.go
File metadata and controls
560 lines (463 loc) · 14.1 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package httpio
import (
"bytes"
"context"
"crypto/md5"
"crypto/sha256"
"errors"
"fmt"
"hash"
"io"
"io/ioutil"
"math"
"math/rand"
"net/http"
"net/url"
"strings"
"sync"
)
// Possible errors
var (
ErrInvalidURLHost = errors.New("invalid url host")
ErrInvalidURLScheme = errors.New("invalid url scheme")
ErrReadFailed = errors.New("read failed")
ErrReadFromSource = errors.New("read from source")
ErrRangeReadNotSupported = errors.New("range reads not supported")
ErrRangeReadNotSatisfied = errors.New("range not satisfied")
ErrHeaderEtag = errors.New("etag header differs")
ErrHeaderContentLength = errors.New("content length differs")
headerErrs = map[string]error{
"Etag": ErrHeaderEtag,
"Content-Length": ErrHeaderContentLength,
}
)
// MaxConcurrentReaders limits the number of concurrent reads.
const MaxConcurrentReaders = 5
// RequestError fulfills the error type for reporting more specific errors with http requests.
type RequestError struct {
StatusCode int
Url string
}
func getHeaderErr(h string) error {
e, ok := headerErrs[h]
if !ok {
return fmt.Errorf("retrieving header '%s'", h)
}
return e
}
// Error returns the string of a RequestError.
func (r RequestError) Error() string {
return fmt.Sprintf("Error requesting %s, received code: %d", r.Url, r.StatusCode)
}
// HTTPStatus returns the http code from the RequestError.
func (r RequestError) HTTPStatus() (int, bool) {
if r.StatusCode < 200 {
return 0, false
}
return r.StatusCode, true
}
// ReadSizeLimit is the maximum size buffer chunk to operate on.
const ReadSizeLimit = 32768
// Options contains the parts to create and use a ReadCloser or ReadAtCloser
type Options struct {
client *http.Client
ctx context.Context
hashChunkSize int64
expectHeaders map[string]string
maxConcurrentReaders int64
url string
}
// Option is a func type used to pass options to the New* funcs.
type Option func(*Options)
// ReadCloser contains the required parts to implement a io.ReadCloser on a URL.
type ReadCloser struct {
options *Options
cancel context.CancelFunc
}
type readClient interface {
do(req *http.Request) (*http.Response, error)
}
type readAtCloseRead struct {
client readClient
ctx context.Context
cancelCTX context.CancelFunc
id string
}
func (r *ReadAtCloser) newReader() *readAtCloseRead {
ctx, cancel := context.WithCancel(r.ctx)
reader := &readAtCloseRead{
client: r,
ctx: ctx,
cancelCTX: cancel,
id: randomString(),
}
r.mutex.Lock()
defer r.mutex.Unlock()
r.readers[reader.id] = reader
return reader
}
func (r *readAtCloseRead) cancel() {
r.cancelCTX()
}
// ReadAtCloser contains the required options and metadata to implement io.ReadAtCloser on a URL.
// Use the Options to configure the ReadAtCloser with an http.Client, URL, and any additional http.Header values that should be sent with the request.
type ReadAtCloser struct {
options *Options
contentLength int64
etag string
ctx context.Context
cancel context.CancelFunc
readerWG *sync.WaitGroup
concurrentReaders chan struct{}
mutex sync.Mutex
readers map[string]*readAtCloseRead
}
func (r *ReadAtCloser) do(req *http.Request) (*http.Response, error) {
r.concurrentReaders <- struct{}{}
res, err := r.options.client.Do(req)
<-r.concurrentReaders
return res, err
}
func (r *ReadAtCloser) finishReader(id string) {
r.mutex.Lock()
defer r.mutex.Unlock()
reader, ok := r.readers[id]
if !ok {
return
}
reader.cancel()
delete(r.readers, id)
}
// NewReadAtCloser validates the options provided and returns a new a *ReadAtCloser after validating the URL. The URL validation includes basic scheme and hostnane checks.
func NewReadAtCloser(opts ...Option) (r *ReadAtCloser, err error) {
o := &Options{expectHeaders: make(map[string]string)}
for _, opt := range opts {
opt(o)
}
o.ensureClient()
if err := o.validateUrl(); err != nil {
return nil, err
}
maxReaders := o.maxConcurrentReaders
if maxReaders == 0 {
maxReaders = MaxConcurrentReaders
}
contentLength, etag, err := o.headURL(o.expectHeaders)
if err != nil {
return nil, err
}
useCTX := context.Background()
if o.ctx != nil {
useCTX = o.ctx
}
ctx, cancel := context.WithCancel(useCTX)
return &ReadAtCloser{
contentLength: contentLength,
etag: etag,
options: o,
ctx: ctx,
cancel: cancel,
concurrentReaders: make(chan struct{}, maxReaders),
readerWG: &sync.WaitGroup{},
readers: make(map[string]*readAtCloseRead),
}, nil
}
// WithClient is an Option func which allows the user to supply their own instance of an http.Client. If not supplied a new generic http.Client is created.
func WithClient(c *http.Client) Option {
return func(o *Options) {
o.client = c
}
}
// WithContext allows supplying a context for the ReadAtCloser to use.
func WithContext(ctx context.Context) Option {
return func(o *Options) {
o.ctx = ctx
}
}
// WithURL (REQUIRED) is an Option func used to supply the full url string; scheme, host, and path, to be read.
func WithURL(url string) Option {
return func(o *Options) {
o.url = url
}
}
// WithMaxConcurrentReaders is an Option to set the maximum number of concurrent go funcs performing Reads in any Reader. If not supplied the default MaxConcurrentReaders is used.
func WithMaxConcurrentReaders(r int64) Option {
return func(o *Options) {
o.maxConcurrentReaders = r
}
}
// WithExpectHeaders is an Option func used to specify any expected response headers from the server.
func WithExpectHeaders(e map[string]string) Option {
return func(o *Options) {
o.expectHeaders = e
}
}
// WithHashChunkSize is an Option func to specify the size to chunk content at when hashing the content.
func WithHashChunkSize(s int64) Option {
return func(o *Options) {
o.hashChunkSize = s
}
}
func (o *Options) ensureClient() {
if o.client == nil {
o.client = new(http.Client)
}
}
func (o *Options) validateUrl() error {
u, err := url.Parse(o.url)
if err != nil {
return err
}
if u.Scheme == "" {
return ErrInvalidURLScheme
}
if u.Hostname() == "" {
return ErrInvalidURLHost
}
return nil
}
func (o *Options) headURL(expectHeaders map[string]string) (int64, string, error) {
head, err := o.client.Head(o.url)
if err != nil {
return 0, "", err
}
if head.StatusCode != http.StatusOK {
return 0, "", RequestError{StatusCode: head.StatusCode, Url: o.url}
}
if head.Header.Get("accept-ranges") != "bytes" {
return 0, "", ErrRangeReadNotSupported
}
for k, v := range expectHeaders {
if sent := head.Header.Get(k); sent != v {
return 0, "", getHeaderErr(k)
}
}
// This is really annoying, but we have to strip the quotes around the string.
etag := strings.Trim(head.Header.Get("Etag"), "\"")
return head.ContentLength, etag, nil
}
func (o *Options) hashURL(hashSize uint) (hash.Hash, error) {
res, err := o.client.Get(o.url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode < 200 || res.StatusCode > 399 {
return nil, RequestError{StatusCode: res.StatusCode, Url: o.url}
}
switch hashSize {
case sha256.Size:
return sha256SumReader(res.Body)
default:
return md5SumReader(res.Body)
}
}
// HashURL takes the hash scheme as a uint (either sha256.Size or md5.Size) and the chunk size, and returns the hashed URL body in the supplied scheme as a slice of hash.Hash.
// When the chunk size is less than the length of the content, the URL will be read with multiple, concurrent range reads to create the slice of hash.Hash.
// Specifying a chunkSize <= 0 is translated to "do not chunk" and the entire content will be hashed as one chunk.
// The size and capacity of the returned slice of hash.Hash is equal to the number of chunks calculated based on the content length divided by the chunkSize, or 1 if chunkSize is equal to, or less than 0.
func (r *ReadAtCloser) HashURL(scheme uint) ([]hash.Hash, error) {
// Quickly copy these out and release the lock.
r.mutex.Lock()
cl := r.contentLength
chunkSize := r.options.hashChunkSize
r.mutex.Unlock()
if chunkSize <= 0 {
chunkSize = cl
}
var chunks int64
// If chunkSize is greater than the content length reset it to the available length and set number of chunks to 1.
// Otherwise, we need to divide the length by the number of chunks and round up. The final chunkSize will be the sum of the remainder.
if chunkSize > cl {
chunkSize = cl
chunks = 1
} else {
chunks = int64(math.Ceil(float64(cl) / float64(chunkSize)))
}
var hasher func(reader io.Reader) (hash.Hash, error)
switch scheme {
case sha256.Size:
hasher = sha256SumReader
default:
hasher = md5SumReader
}
hashes := make([]hash.Hash, chunks, chunks)
hashErrs := make([]error, chunks)
wg := sync.WaitGroup{}
for i := int64(0); i < chunks; i++ {
// The remaining size is the smaller of the chunkSize or the chunkSize times the number of chunks already read.
remaining := chunkSize
if remaining > cl-(i*chunkSize) {
remaining = cl - (i * chunkSize)
}
// If remaining ends up less than 0 then the math above to determine the number of chunks is incorrect or something bad happened.
if remaining < 0 {
return nil, errors.New("failed to properly calculate the hash chunk count")
}
start := chunkSize * i
wg.Add(1)
go func(w *sync.WaitGroup, idx, start, size int64, rac *ReadAtCloser) {
defer w.Done()
b := make([]byte, size)
if _, err := rac.ReadAt(b, start); err != nil {
hashErrs[idx] = err
if err != io.ErrUnexpectedEOF {
return
}
}
reader := bytes.NewReader(b[:])
h, err := hasher(reader)
if err != nil {
hashErrs[idx] = err
return
}
hashes[idx] = h
}(&wg, i, start, remaining, r)
}
wg.Wait()
if err := checkErrSlice(hashErrs); err != nil {
return hashes, err
}
return hashes, nil
}
func checkErrSlice(es []error) (err error) {
for _, e := range es {
if e != nil {
if err != nil {
err = fmt.Errorf("%s: %s", err, e)
continue
}
err = e
}
}
return
}
// Length returns the reported ContentLength of the URL body.
func (r *ReadAtCloser) Length() int64 {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.contentLength
}
// Etag returns the last read Etag from the URL being operated on by the configured ReadAtCloser.
func (r *ReadAtCloser) Etag() string {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.etag
}
func (r *ReadAtCloser) ChunkSize() int64 {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.options.hashChunkSize
}
func (r *ReadAtCloser) URL() string {
r.mutex.Lock()
defer r.mutex.Unlock()
return r.options.url
}
// ReadAt satisfies the io.ReaderAt interface. It requires the ReadAtCloser be previously configured.
func (r *ReadAtCloser) ReadAt(b []byte, start int64) (n int, err error) {
end := start + int64(len(b))
r.readerWG.Add(1)
defer r.readerWG.Done()
reader := r.newReader()
defer r.finishReader(reader.id)
req, err := http.NewRequestWithContext(reader.ctx, http.MethodGet, r.options.url, nil)
if err != nil {
return 0, err
}
requestRange := fmt.Sprintf("bytes=%d-%d", start, end)
req.Header.Add("Range", requestRange)
res, err := reader.client.do(req)
if err != nil {
return 0, err
}
if res.StatusCode != http.StatusPartialContent && res.StatusCode != http.StatusOK {
return 0, ErrRangeReadNotSatisfied
}
bt := make([]byte, len(b))
bt, err = ioutil.ReadAll(res.Body)
copy(b, bt)
r.mutex.Lock()
defer r.mutex.Unlock()
if r.contentLength < end {
return int(res.ContentLength - start), io.ErrUnexpectedEOF
}
l := int64(len(b))
if l > res.ContentLength {
l = res.ContentLength
}
return int(l), nil
}
// Close cancels the client context and closes any idle connections.
func (r *ReadAtCloser) Close() error {
// Ensure a cancellable context has been created else r.cancel will be nil.
if r.cancel != nil {
r.cancel()
}
r.options.client.CloseIdleConnections()
r.readerWG.Wait()
return nil
}
// HashURL takes the hash scheme size (sha256.Size or md5.Size) and returns the hashed URL body in the supplied scheme as a hash.Hash interface.
func (r *ReadCloser) HashURL(size uint) (hash.Hash, error) {
return r.options.hashURL(size)
}
// Read fulfills the io.Reader interface. The ReadCloser must be previously configured. The body of the configured URL is read into p, up to len(p). If the length of p is greater than the ContentLength of the body the length returned will be ContentLength.
func (r *ReadCloser) Read(p []byte) (n int, err error) {
ctx, cancel := context.WithCancel(context.Background())
r.cancel = cancel
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.options.url, nil)
if err != nil {
return 0, err
}
res, err := r.options.client.Do(req)
if err != nil {
return 0, err
}
if res.StatusCode != http.StatusOK {
return 0, ErrReadFailed
}
bt := make([]byte, len(p))
bt, err = ioutil.ReadAll(res.Body)
copy(p, bt)
l := int64(len(p))
if l > res.ContentLength {
l = res.ContentLength
}
return int(l), nil
}
// Close cancels the client context and closes any idle connections.
func (r *ReadCloser) Close() error {
// Ensure a cancellable context has been created else r.cancel will be nil.
if r.cancel != nil {
r.cancel()
}
r.options.client.CloseIdleConnections()
return nil
}
// sha256SumReader reads from r until and calculates the sha256 sum, until r is exhausted.
func sha256SumReader(r io.Reader) (hash.Hash, error) {
shaSum := sha256.New()
buf := make([]byte, ReadSizeLimit)
if _, err := io.CopyBuffer(shaSum, r, buf); err != nil {
return nil, err
}
return shaSum, nil
}
// md5SumReader reads from r until and calculates the md5 sum, until r is exhausted.
func md5SumReader(r io.Reader) (hash.Hash, error) {
md5sum := md5.New()
buf := make([]byte, ReadSizeLimit)
if _, err := io.CopyBuffer(md5sum, r, buf); err != nil {
return nil, err
}
return md5sum, nil
}
func randomString() string {
letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, rand.Intn(25)+25)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}