-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
fetcher.go
179 lines (158 loc) · 4.38 KB
/
fetcher.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fetcher
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
stderrors "errors"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/dgraph-io/ristretto"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/ory/x/httpx"
"github.com/ory/x/stringsx"
)
// Fetcher is able to load file contents from http, https, file, and base64 locations.
type Fetcher struct {
hc *retryablehttp.Client
limit int64
cache *ristretto.Cache
ttl time.Duration
}
type opts struct {
hc *retryablehttp.Client
limit int64
cache *ristretto.Cache
ttl time.Duration
}
var ErrUnknownScheme = stderrors.New("unknown scheme")
// WithClient sets the http.Client the fetcher uses.
func WithClient(hc *retryablehttp.Client) Modifier {
return func(o *opts) {
o.hc = hc
}
}
// WithMaxHTTPMaxBytes reads at most limit bytes from the HTTP response body,
// returning bytes.ErrToLarge if the limit would be exceeded.
func WithMaxHTTPMaxBytes(limit int64) Modifier {
return func(o *opts) {
o.limit = limit
}
}
func WithCache(cache *ristretto.Cache, ttl time.Duration) Modifier {
return func(o *opts) {
if ttl < 0 {
return
}
o.cache = cache
o.ttl = ttl
}
}
func newOpts() *opts {
return &opts{
hc: httpx.NewResilientClient(),
}
}
type Modifier func(*opts)
// NewFetcher creates a new fetcher instance.
func NewFetcher(opts ...Modifier) *Fetcher {
o := newOpts()
for _, f := range opts {
f(o)
}
return &Fetcher{hc: o.hc, limit: o.limit, cache: o.cache, ttl: o.ttl}
}
// Fetch fetches the file contents from the source.
func (f *Fetcher) Fetch(source string) (*bytes.Buffer, error) {
return f.FetchContext(context.Background(), source)
}
// FetchContext fetches the file contents from the source and allows to pass a
// context that is used for HTTP requests.
func (f *Fetcher) FetchContext(ctx context.Context, source string) (*bytes.Buffer, error) {
b, err := f.FetchBytes(ctx, source)
if err != nil {
return nil, err
}
return bytes.NewBuffer(b), nil
}
// FetchBytes fetches the file contents from the source and allows to pass a
// context that is used for HTTP requests.
func (f *Fetcher) FetchBytes(ctx context.Context, source string) ([]byte, error) {
switch s := stringsx.SwitchPrefix(source); {
case s.HasPrefix("http://"), s.HasPrefix("https://"):
return f.fetchRemote(ctx, source)
case s.HasPrefix("file://"):
return f.fetchFile(strings.TrimPrefix(source, "file://"))
case s.HasPrefix("base64://"):
src, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(source, "base64://"))
if err != nil {
return nil, errors.Wrapf(err, "base64decode: %s", source)
}
return src, nil
default:
return nil, errors.Wrap(ErrUnknownScheme, s.ToUnknownPrefixErr().Error())
}
}
func (f *Fetcher) fetchRemote(ctx context.Context, source string) (b []byte, err error) {
if f.cache != nil {
cacheKey := sha256.Sum256([]byte(source))
if v, ok := f.cache.Get(cacheKey[:]); ok {
cached := v.([]byte)
b = make([]byte, len(cached))
copy(b, cached)
return b, nil
}
defer func() {
if err == nil && len(b) > 0 {
toCache := make([]byte, len(b))
copy(toCache, b)
f.cache.SetWithTTL(cacheKey[:], toCache, int64(len(toCache)), f.ttl)
}
}()
}
req, err := retryablehttp.NewRequestWithContext(ctx, http.MethodGet, source, nil)
if err != nil {
return nil, errors.Wrapf(err, "new request: %s", source)
}
res, err := f.hc.Do(req)
if err != nil {
return nil, errors.Wrap(err, source)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, errors.Errorf("expected http response status code 200 but got %d when fetching: %s", res.StatusCode, source)
}
if f.limit > 0 {
var buf bytes.Buffer
n, err := io.Copy(&buf, io.LimitReader(res.Body, f.limit+1))
if n > f.limit {
return nil, bytes.ErrTooLarge
}
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
return io.ReadAll(res.Body)
}
func (f *Fetcher) fetchFile(source string) ([]byte, error) {
fp, err := os.Open(source) // #nosec:G304
if err != nil {
return nil, errors.Wrapf(err, "unable to open file: %s", source)
}
defer fp.Close()
b, err := io.ReadAll(fp)
if err != nil {
return nil, errors.Wrapf(err, "unable to read file: %s", source)
}
if err := fp.Close(); err != nil {
return nil, errors.Wrapf(err, "unable to close file: %s", source)
}
return b, nil
}