|
| 1 | +package httpreader |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + logging "github.com/ipfs/go-log/v2" |
| 11 | + "go.uber.org/multierr" |
| 12 | +) |
| 13 | + |
| 14 | +var log = logging.Logger("httpreader") |
| 15 | + |
| 16 | +type ResumableReader struct { |
| 17 | + ctx context.Context |
| 18 | + initialURL string |
| 19 | + finalURL *string |
| 20 | + position int64 |
| 21 | + contentLength int64 |
| 22 | + client *http.Client |
| 23 | + reader io.ReadCloser |
| 24 | +} |
| 25 | + |
| 26 | +func NewResumableReader(ctx context.Context, url string) (*ResumableReader, error) { |
| 27 | + finalURL := "" |
| 28 | + |
| 29 | + client := &http.Client{ |
| 30 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 31 | + finalURL = req.URL.String() |
| 32 | + if len(via) >= 10 { |
| 33 | + return fmt.Errorf("stopped after 10 redirects") |
| 34 | + } |
| 35 | + return nil |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + r := &ResumableReader{ |
| 40 | + ctx: ctx, |
| 41 | + initialURL: url, |
| 42 | + finalURL: &finalURL, |
| 43 | + position: 0, |
| 44 | + client: client, |
| 45 | + } |
| 46 | + |
| 47 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + resp, err := r.client.Do(req) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + if resp.StatusCode != http.StatusOK { |
| 58 | + return nil, fmt.Errorf("failed to fetch resource, status code: %d", resp.StatusCode) |
| 59 | + } |
| 60 | + |
| 61 | + contentLength, err := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64) |
| 62 | + if err != nil { |
| 63 | + if err = resp.Body.Close(); err != nil { |
| 64 | + err = multierr.Append(err, err) |
| 65 | + } |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + r.contentLength = contentLength |
| 70 | + r.reader = resp.Body |
| 71 | + |
| 72 | + return r, nil |
| 73 | +} |
| 74 | + |
| 75 | +func (r *ResumableReader) ContentLength() int64 { |
| 76 | + return r.contentLength |
| 77 | +} |
| 78 | + |
| 79 | +func (r *ResumableReader) Read(p []byte) (n int, err error) { |
| 80 | + for { |
| 81 | + if r.reader == nil { |
| 82 | + reqURL := r.initialURL |
| 83 | + if *r.finalURL != "" { |
| 84 | + reqURL = *r.finalURL |
| 85 | + } |
| 86 | + |
| 87 | + req, err := http.NewRequestWithContext(r.ctx, "GET", reqURL, nil) |
| 88 | + if err != nil { |
| 89 | + return 0, err |
| 90 | + } |
| 91 | + req.Header.Set("Range", fmt.Sprintf("bytes=%d-", r.position)) |
| 92 | + resp, err := r.client.Do(req) |
| 93 | + if err != nil { |
| 94 | + return 0, err |
| 95 | + } |
| 96 | + |
| 97 | + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { |
| 98 | + return 0, fmt.Errorf("non-resumable status code: %d", resp.StatusCode) |
| 99 | + } |
| 100 | + r.reader = resp.Body |
| 101 | + } |
| 102 | + |
| 103 | + n, err = r.reader.Read(p) |
| 104 | + r.position += int64(n) |
| 105 | + |
| 106 | + if err == io.EOF || err == io.ErrUnexpectedEOF { |
| 107 | + if r.position == r.contentLength { |
| 108 | + if err := r.reader.Close(); err != nil { |
| 109 | + log.Warnf("error closing reader: %+v", err) |
| 110 | + } |
| 111 | + return n, io.EOF |
| 112 | + } |
| 113 | + if err := r.reader.Close(); err != nil { |
| 114 | + log.Warnf("error closing reader: %+v", err) |
| 115 | + } |
| 116 | + r.reader = nil |
| 117 | + } else { |
| 118 | + return n, err |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments