-
Notifications
You must be signed in to change notification settings - Fork 13
/
client.go
200 lines (181 loc) · 6.06 KB
/
client.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package metha
import (
"bytes"
"compress/gzip"
"crypto/tls"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/sethgrid/pester"
log "github.com/sirupsen/logrus"
"golang.org/x/net/html/charset"
)
const (
// DefaultTimeout on requests.
DefaultTimeout = 10 * time.Minute
// DefaultMaxRetries is the default number of retries on a single request.
DefaultMaxRetries = 8
)
var (
// StdClient is the standard lib http client.
StdClient = &Client{Doer: http.DefaultClient}
// DefaultClient is the more resilient client, that will retry and timeout.
DefaultClient = &Client{Doer: CreateDoer(DefaultTimeout, DefaultMaxRetries)}
// DefaultUserAgent to identify crawler, some endpoints do not like the Go
// default (https://golang.org/src/net/http/request.go#L462), e.g.
// https://calhoun.nps.edu/oai/request.
DefaultUserAgent = fmt.Sprintf("metha/%s", Version)
// ControlCharReplacer helps to deal with broken XML: http://eprints.vu.edu.au/perl/oai2. Add more
// weird things to be cleaned before XML parsing here. Another faulty:
// http://digitalcommons.gardner-webb.edu/do/oai/?from=2016-02-29&metadataPr
// efix=oai_dc&until=2016-03-31&verb=ListRecords. Replace control chars
// outside XML char range.
ControlCharReplacer = strings.NewReplacer(
"\u0000", "", "\u0001", "", "\u0002", "", "\u0003", "", "\u0004", "",
"\u0005", "", "\u0006", "", "\u0007", "", "\u0008", "", "\u0009", "",
"\u000B", "", "\u000C", "", "\u000E", "", "\u000F", "", "\u0010", "",
"\u0011", "", "\u0012", "", "\u0013", "", "\u0014", "", "\u0015", "",
"\u0016", "", "\u0017", "", "\u0018", "", "\u0019", "", "\u001A", "",
"\u001B", "", "\u001C", "", "\u001D", "", "\u001E", "", "\u001F", "",
"\uFFFD", "", "\uFFFE", "",
)
)
// HTTPError saves details of an HTTP error.
type HTTPError struct {
URL *url.URL
StatusCode int
RequestError error
}
// Error prints the error message.
func (e HTTPError) Error() string {
return fmt.Sprintf("failed with %s on %s: %v", http.StatusText(e.StatusCode), e.URL, e.RequestError)
}
// CreateDoer will return http request clients with specific timeout and retry
// properties.
func CreateDoer(timeout time.Duration, retries int) Doer {
tr := http.DefaultTransport
tr.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client := http.DefaultClient
client.Transport = tr
if timeout == 0 && retries == 0 {
return client
}
c := pester.New()
c.EmbedHTTPClient(client)
c.Timeout = timeout
c.MaxRetries = retries
c.Backoff = pester.ExponentialBackoff
c.Transport = tr
return c
}
// CreateClient creates a client with timeout and retry properties.
func CreateClient(timeout time.Duration, retries int) *Client {
return &Client{Doer: CreateDoer(timeout, retries)}
}
// Doer is a minimal HTTP interface.
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
// Client can execute requests.
type Client struct {
Doer Doer
}
// Do is a shortcut for DefaultClient.Do.
func Do(r *Request) (*Response, error) {
return DefaultClient.Do(r)
}
// maybeCompressed detects compressed content and decompresses it on the fly.
func maybeCompressed(r io.Reader) (io.ReadCloser, error) {
buf, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
if gr, err := gzip.NewReader(bytes.NewReader(buf)); err == nil {
log.Println("decompress-on-the-fly")
return gr, nil
}
return ioutil.NopCloser(bytes.NewReader(buf)), nil
}
// Do executes a single OAIRequest. ResumptionToken handling must happen in the
// caller. Only Identify and GetRecord requests will return a complete response.
func (c *Client) Do(r *Request) (*Response, error) {
link, err := r.URL()
if err != nil {
return nil, err
}
log.Println(link)
req, err := http.NewRequest("GET", link.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", DefaultUserAgent) // Maybe https://codereview.appspot.com/7532043.
for name, values := range r.ExtraHeaders {
for _, value := range values {
req.Header.Add(name, value)
}
}
resp, err := c.Doer.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, HTTPError{URL: link, RequestError: err, StatusCode: resp.StatusCode}
}
defer resp.Body.Close()
var reader = resp.Body
// Detect compressed response.
reader, err = maybeCompressed(reader)
if err != nil {
return nil, err
}
defer reader.Close()
if r.CleanBeforeDecode {
// Remove some chars, that the XML decoder will complain about.
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
reader = ioutil.NopCloser(strings.NewReader(ControlCharReplacer.Replace(string(b))))
}
// Drain response XML, iterate over various XML encoding declarations.
// Limit the amount we can read.
respBody, err := ioutil.ReadAll(io.LimitReader(reader, 2<<24))
if err != nil {
return nil, err
}
// refs #21812, hack around misleading XML declarations; we only cover
// declared "UTF-8", but actually ... A rare issue nonetheless; add more
// cases here if necessary; observed in the wild in 05/2022 at
// http://digi.ub.uni-heidelberg.de/cgi-bin/digioai.cgi?from=2021-07-01T00:00:00Z&metadataPrefix=oai_dc&until=2021-07-31T23:59:59Z&verb=ListRecords.
decls := [][]byte{
[]byte(`<?xml version="1.0" encoding="UTF-8"?>`),
[]byte(`<?xml version="1.0" encoding="ISO-8859-1"?>`),
[]byte(`<?xml version="1.0" encoding="WINDOWS-1252"?>`),
[]byte(`<?xml version="1.0" encoding="UTF-16"?>`),
[]byte(`<?xml version="1.0" encoding="US-ASCII"?>`),
}
for i, decl := range decls {
body := bytes.Replace(respBody, []byte(`<?xml version="1.0" encoding="UTF-8"?>`), decl, 1)
dec := xml.NewDecoder(bytes.NewReader(body))
dec.CharsetReader = charset.NewReaderLabel
dec.Strict = false
var response Response
if err := dec.Decode(&response); err != nil {
if !bytes.HasPrefix(body, []byte(`<?xml version="1.0"`)) {
return nil, err
}
log.Printf("decode failed with: %v", string(decl))
continue
}
if i > 0 {
log.Printf("decode worked with adjusted declaration: %v", string(decl))
}
return &response, nil
}
return nil, fmt.Errorf("failed to parse response")
}