forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
283 lines (234 loc) Β· 5.81 KB
/
http.go
File metadata and controls
283 lines (234 loc) Β· 5.81 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
package plugin
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/evcc-io/evcc/plugin/pipeline"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/sandrolain/httpcache"
)
// HTTP implements HTTP request provider
type HTTP struct {
*getter
*request.Helper
url, method string
headers map[string]string
body string
pipeline *pipeline.Pipeline
mu *sync.Mutex
}
func init() {
registry.AddCtx("http", NewHTTPPluginFromConfig)
}
var mc = httpcache.NewMemoryCache()
// NewHTTPPluginFromConfig creates a HTTP provider
func NewHTTPPluginFromConfig(ctx context.Context, other map[string]any) (Plugin, error) {
cc := struct {
URI, Method string
Headers map[string]string
Body string
pipeline.Settings `mapstructure:",squash"`
Scale float64
Offset float64
Insecure bool
Auth Auth
Timeout time.Duration
Cache time.Duration
}{
Headers: make(map[string]string),
Method: http.MethodGet,
Scale: 1,
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.URI == "" {
return nil, errors.New("missing uri")
}
log := util.ContextLoggerWithDefault(ctx, util.NewLogger("http"))
p := NewHTTP(
log,
strings.ToUpper(cc.Method),
cc.URI,
cc.Insecure,
cc.Cache,
).
WithHeaders(cc.Headers).
WithBody(cc.Body)
p.Client.Timeout = cc.Timeout
p.getter = defaultGetters(p, cc.Scale)
p.getter.offset = cc.Offset
if cc.Auth.Type != "" || cc.Auth.Source != "" {
transport, err := cc.Auth.Transport(ctx, log, p.Client.Transport)
if err != nil {
return nil, err
}
p.Client.Transport = transport
}
pipe, err := pipeline.New(log, cc.Settings)
if err != nil {
return nil, err
}
p.pipeline = pipe
return p, nil
}
// NewHTTP create HTTP provider
func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Duration) *HTTP {
p := &HTTP{
Helper: request.NewHelper(log),
url: uri,
method: method,
}
// override the transport to accept self-signed certificates
if insecure {
p.Client.Transport = request.NewTripper(log, transport.Insecure())
}
if cache > 0 {
// remove no-cache response headers
p.Client.Transport = &transport.Modifier{
Modifier: func(resp *http.Response) error {
dropNoCache(resp, "Cache-Control")
dropNoCache(resp, "Pragma")
return nil
},
Base: p.Client.Transport,
}
}
// http cache
p.Client.Transport = &httpcache.Transport{
Cache: mc,
MarkCachedResponses: true,
Transport: p.Client.Transport,
}
if cache > 0 {
cacheHeader := fmt.Sprintf("max-age=%d, must-revalidate", int(cache.Seconds()))
p.Client.Transport = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"Cache-Control": cacheHeader,
}),
Base: p.Client.Transport,
}
// for cached requests enforce single inflight GET
if method == http.MethodGet {
p.mu = muForKey(p.url)
}
}
return p
}
func dropNoCache(resp *http.Response, header string) {
if h := resp.Header.Get(header); h != "" {
var hh []string
for h := range strings.SplitSeq(h, ",") {
if s := strings.TrimSpace(h); strings.ToLower(s) != "no-cache" {
hh = append(hh, s)
}
}
if len(hh) == 0 {
resp.Header.Del(header)
} else {
resp.Header.Set(header, strings.Join(hh, ", "))
}
}
}
// WithBody adds request body
func (p *HTTP) WithBody(body string) *HTTP {
if body != "" {
p.body = body
if p.method == http.MethodGet {
p.method = http.MethodPost
}
}
return p
}
// WithHeaders adds request headers
func (p *HTTP) WithHeaders(headers map[string]string) *HTTP {
p.headers = headers
return p
}
// request executes the configured request or returns the cached value
func (p *HTTP) request(url string, body string) ([]byte, error) {
var b io.Reader
if p.method != http.MethodGet {
b = strings.NewReader(body)
}
url = util.DefaultScheme(url, "http")
// empty method becomes GET
req, err := request.New(p.method, url, b, p.headers)
if err != nil {
return []byte{}, err
}
val, err := p.DoBody(req)
if err != nil {
if err2 := knownErrors(val); err2 != nil {
err = err2
}
}
return val, err
}
var _ Getters = (*HTTP)(nil)
// StringGetter sends string request
func (p *HTTP) StringGetter() (func() (string, error), error) {
return func() (string, error) {
if p.mu != nil {
p.mu.Lock()
defer p.mu.Unlock()
}
url, err := setFormattedValue(p.url, "", "")
if err != nil {
return "", err
}
b, err := p.request(url, p.body)
if err == nil && p.pipeline != nil {
b, err = p.pipeline.Process(b)
}
return string(b), err
}, nil
}
func (p *HTTP) set(param string, val any) error {
url, err := setFormattedValue(p.url, param, val)
if err != nil {
return err
}
body, err := setFormattedValue(p.body, param, val)
if err != nil {
return err
}
_, err = p.request(url, body)
return err
}
var _ IntSetter = (*HTTP)(nil)
// IntSetter sends int request
func (p *HTTP) IntSetter(param string) (func(int64) error, error) {
return func(val int64) error {
return p.set(param, val)
}, nil
}
var _ FloatSetter = (*HTTP)(nil)
// FloatSetter sends int request
func (p *HTTP) FloatSetter(param string) (func(float64) error, error) {
return func(val float64) error {
return p.set(param, val)
}, nil
}
var _ StringSetter = (*HTTP)(nil)
// StringSetter sends string request
func (p *HTTP) StringSetter(param string) (func(string) error, error) {
return func(val string) error {
return p.set(param, val)
}, nil
}
var _ BoolSetter = (*HTTP)(nil)
// BoolSetter sends bool request
func (p *HTTP) BoolSetter(param string) (func(bool) error, error) {
return func(val bool) error {
return p.set(param, val)
}, nil
}