-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathclient.go
More file actions
285 lines (228 loc) · 6.35 KB
/
Copy pathclient.go
File metadata and controls
285 lines (228 loc) · 6.35 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
284
285
package http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"howett.net/plist"
)
const (
appStoreAuthURL = "https://buy.itunes.apple.com/WebObjects/MZFinance.woa/wa/authenticate"
)
var (
documentXMLPattern = regexp.MustCompile(`(?is)<Document\b[^>]*>(.*)</Document>`)
plistXMLPattern = regexp.MustCompile(`(?is)<plist\b[^>]*>.*?</plist>`)
dictXMLPattern = regexp.MustCompile(`(?is)<dict\b[^>]*>.*</dict>`)
urlPattern = regexp.MustCompile(`https?://[^\s"'<>]+`)
)
type ResponseDecodeError struct {
Cause error
StatusCode int
ContentType string
Body string
URLs []string
}
func (e *ResponseDecodeError) Error() string {
return fmt.Sprintf("failed to unmarshal xml: %v", e.Cause)
}
func (e *ResponseDecodeError) Unwrap() error {
return e.Cause
}
//go:generate go run go.uber.org/mock/mockgen -source=client.go -destination=client_mock.go -package=http
type Client[R interface{}] interface {
Send(request Request) (Result[R], error)
Do(req *http.Request) (*http.Response, error)
NewRequest(method, url string, body io.Reader) (*http.Request, error)
}
type client[R interface{}] struct {
internalClient http.Client
cookieJar CookieJar
}
type Args struct {
CookieJar CookieJar
}
type AddHeaderTransport struct {
T http.RoundTripper
}
func (t *AddHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", DefaultUserAgent)
}
res, err := t.T.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("failed to make round trip: %w", err)
}
return res, nil
}
func NewClient[R interface{}](args Args) Client[R] {
return &client[R]{
internalClient: http.Client{
Timeout: 0,
Jar: args.CookieJar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if req.Referer() == appStoreAuthURL {
return http.ErrUseLastResponse
}
return nil
},
Transport: &AddHeaderTransport{http.DefaultTransport},
},
cookieJar: args.CookieJar,
}
}
func (c *client[R]) Send(req Request) (Result[R], error) {
var (
data []byte
err error
)
if req.Payload != nil {
data, err = req.Payload.data()
if err != nil {
return Result[R]{}, fmt.Errorf("failed to get payload data: %w", err)
}
}
request, err := http.NewRequest(req.Method, req.URL, bytes.NewReader(data))
if err != nil {
return Result[R]{}, fmt.Errorf("failed to create request: %w", err)
}
for key, val := range req.Headers {
request.Header.Set(key, val)
}
res, err := c.internalClient.Do(request)
if err != nil {
return Result[R]{}, fmt.Errorf("request failed: %w", err)
}
defer res.Body.Close()
err = c.cookieJar.Save()
if err != nil {
return Result[R]{}, fmt.Errorf("failed to save cookies: %w", err)
}
if req.ResponseFormat == ResponseFormatJSON {
return c.handleJSONResponse(res)
}
if req.ResponseFormat == ResponseFormatXML {
return c.handleXMLResponse(res)
}
return Result[R]{}, fmt.Errorf("content type is not supported (%s)", req.ResponseFormat)
}
func (c *client[R]) Do(req *http.Request) (*http.Response, error) {
res, err := c.internalClient.Do(req)
if err != nil {
return nil, fmt.Errorf("received error: %w", err)
}
return res, nil
}
func (*client[R]) NewRequest(method, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
return req, nil
}
func (c *client[R]) handleJSONResponse(res *http.Response) (Result[R], error) {
body, err := io.ReadAll(res.Body)
if err != nil {
return Result[R]{}, fmt.Errorf("failed to read response body: %w", err)
}
var data R
err = json.Unmarshal(body, &data)
if err != nil {
return Result[R]{}, fmt.Errorf("failed to unmarshal json: %w", err)
}
return Result[R]{
StatusCode: res.StatusCode,
Data: data,
}, nil
}
func (c *client[R]) handleXMLResponse(res *http.Response) (Result[R], error) {
body, err := io.ReadAll(res.Body)
if err != nil {
return Result[R]{}, fmt.Errorf("failed to read response body: %w", err)
}
if res.StatusCode == http.StatusTooManyRequests {
return Result[R]{}, fmt.Errorf("rate limited by Apple (HTTP %d): %s", res.StatusCode, strings.TrimSpace(string(body)))
}
var data R
normalizedBody := normalizeXMLPlistBody(body)
_, err = plist.Unmarshal(normalizedBody, &data)
if err != nil {
return Result[R]{}, &ResponseDecodeError{
Cause: err,
StatusCode: res.StatusCode,
ContentType: res.Header.Get("Content-Type"),
Body: truncateBody(body, 500),
URLs: extractURLs(body),
}
}
headers := map[string]string{}
for key, val := range res.Header {
headers[key] = strings.Join(val, "; ")
}
return Result[R]{
StatusCode: res.StatusCode,
Headers: headers,
Data: data,
}, nil
}
func extractURLs(body []byte) []string {
matches := urlPattern.FindAll(body, -1)
urls := make([]string, 0, len(matches))
for _, match := range matches {
urls = append(urls, string(match))
}
return urls
}
func truncateBody(body []byte, max int) string {
trimmed := strings.TrimSpace(string(body))
if len(trimmed) <= max {
return trimmed
}
return trimmed[:max] + "..."
}
func normalizeXMLPlistBody(body []byte) []byte {
normalized := bytes.TrimSpace(body)
if len(normalized) == 0 {
return normalized
}
if documentBody := extractDocumentInnerBody(normalized); len(documentBody) > 0 {
normalized = documentBody
}
if embeddedPlist := extractEmbeddedPlist(normalized); len(embeddedPlist) > 0 {
normalized = embeddedPlist
}
if dictBody := extractEmbeddedDict(normalized); len(dictBody) > 0 {
return dictBody
}
if bytes.Contains(normalized, []byte("<key>")) {
return []byte("<dict>" + string(normalized) + "</dict>")
}
return normalized
}
func extractEmbeddedPlist(body []byte) []byte {
plistMatch := plistXMLPattern.Find(body)
if len(plistMatch) == 0 {
return nil
}
return bytes.TrimSpace(plistMatch)
}
func extractEmbeddedDict(body []byte) []byte {
dictMatch := dictXMLPattern.Find(body)
if len(dictMatch) == 0 {
return nil
}
return bytes.TrimSpace(dictMatch)
}
func extractDocumentInnerBody(body []byte) []byte {
documentMatch := documentXMLPattern.FindSubmatch(body)
if len(documentMatch) < 2 {
return nil
}
documentBody := bytes.TrimSpace(documentMatch[1])
if len(documentBody) == 0 {
return nil
}
return documentBody
}