-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
58 lines (48 loc) · 998 Bytes
/
transport.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
package oaichecker
import (
"bytes"
"io"
"io/ioutil"
"net/http"
)
type Transport struct {
Transport http.RoundTripper
analyzer *Analyzer
}
func NewTransport(specs *Specs) *Transport {
return &Transport{
Transport: http.DefaultTransport,
analyzer: NewAnalyzer(specs),
}
}
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
var err error
// GetBody is an optional func to return a new copy of Body
switch req.Body.(type) {
case nil:
req.GetBody = func() (io.ReadCloser, error) {
return http.NoBody, nil
}
default:
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(body)), nil
}
}
req.Body, err = req.GetBody()
if err != nil {
return nil, err
}
res, err := t.Transport.RoundTrip(req)
if err != nil {
return nil, err
}
err = t.analyzer.Analyze(req)
if err != nil {
return nil, err
}
return res, err
}