-
Notifications
You must be signed in to change notification settings - Fork 34
/
fetch.go
118 lines (85 loc) · 2.1 KB
/
fetch.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
package hargo
import (
"bufio"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"os"
"path"
"path/filepath"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
// Fetch downloads all resources references in .har file
func Fetch(r *bufio.Reader) error {
har, err := Decode(r)
check(err)
datestring := time.Now().Format("20060102150405")
outdir := "." + string(filepath.Separator) + "hargo-fetch-" + datestring
err = os.Mkdir(outdir, 0777)
check(err)
for _, entry := range har.Log.Entries {
//TODO create goroutine here to parallelize requests
fmt.Println("URL: " + entry.Request.URL)
req, _ := http.NewRequest(entry.Request.Method, entry.Request.URL, nil)
for _, h := range entry.Request.Headers {
if !strings.HasPrefix(h.Name, ":") {
req.Header.Add(h.Name, h.Value)
}
}
for _, c := range entry.Request.Cookies {
cookie := &http.Cookie{Name: c.Name, Value: c.Value, HttpOnly: false, Domain: c.Domain}
req.AddCookie(cookie)
}
//cookie := &http.Cookie{Name: "_hargo", Value: "true", HttpOnly: false}
//req.AddCookie(cookie)
err = downloadFile(req, outdir)
if err != nil {
log.Error(err)
return err
}
}
return nil
}
func downloadFile(req *http.Request, outdir string) error {
fileName := path.Base(req.URL.Path)
if fileName == "/" || fileName == "" {
fileName = "index.html"
}
fileName = outdir + string(filepath.Separator) + fileName
if len(fileName) == 0 {
return nil
}
file, err := os.Create(fileName)
if err != nil {
log.Error(err)
return err
}
defer file.Close()
jar, _ := cookiejar.New(nil)
jar.SetCookies(req.URL, req.Cookies())
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
Jar: jar,
}
// spew.Dump(client)
// spew.Dump(req)
resp, err := client.Do(req) //.Get(rawURL) // add a filter to check redirect
if err != nil {
log.Error(err)
return err
}
defer resp.Body.Close()
size, err := io.Copy(file, resp.Body)
if err != nil {
log.Error(err)
return err
}
fmt.Printf("Downloaded %s [%v bytes]\n", fileName, size)
return nil
}