-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
75 lines (70 loc) · 1.89 KB
/
http.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
package gofirefox
import (
"context"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
var headers = map[string]string{
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", // default Accept header sent by Firefox
"Accept-Language": "en-US,en;q=0.5",
"DNT": "1",
}
func downloadFile(ctx context.Context, client *http.Client, fileURL, filePath string) error {
resp, err := openURLHTTP(ctx, client, fileURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 399 {
return fmt.Errorf("status_code=%d", resp.StatusCode)
}
f, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("failed to create file: %s", err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
return fmt.Errorf("failed to write to file %s - error: %s", filePath, err)
}
return nil
}
const userAgent = "Mozilla/5.0 (Windows NT 10.0; rv:91.0) Gecko/20100101 Firefox/91.0"
func openURLHTTP(ctx context.Context, client *http.Client, pageURL string) (*http.Response, error) {
var resp *http.Response
var err error
for attempt := 0; attempt < 5; attempt++ {
select {
case <-ctx.Done():
return nil, fmt.Errorf("context canceled")
default:
}
if attempt > 0 {
dur := time.Duration(attempt*attempt) * time.Second
log.Printf("HTTP request failed: %s - retrying in %v", err, dur)
time.Sleep(dur)
}
var req *http.Request
req, err = http.NewRequestWithContext(ctx, "GET", pageURL, nil)
if err != nil {
err = fmt.Errorf("http.NewRequest failed: %s", err)
continue
}
req.Header.Set("User-Agent", userAgent)
for headerKey, headerValue := range headers {
req.Header.Set(headerKey, headerValue)
}
resp, err = client.Do(req)
if err != nil {
err = fmt.Errorf("HTTP request failed: %s", err)
continue
}
// no errors => exit loop
break
}
return resp, err
}