-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (159 loc) · 3.33 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/signal"
"strings"
"time"
"github.com/ernesto-jimenez/crawler"
"github.com/ernesto-jimenez/httplogger"
)
var (
path string
startURL string
verbose bool
client = &http.Client{}
checked = map[string]error{}
)
func main() {
var ()
flag.BoolVar(&verbose, "verbose", false, "verbose logs information about every http request")
flag.Parse()
if verbose {
client.Transport = httplogger.DefaultLoggedTransport
}
log.SetFlags(0)
switch arg := flag.Arg(0); {
case arg == "":
path = "public"
case strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://"):
startURL = arg
path = ""
default:
path = arg
}
if path != "" {
_, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
s := httptest.NewServer(http.FileServer(http.Dir(path)))
defer s.Close()
startURL = s.URL
}
start, err := url.Parse(startURL)
if err != nil {
log.Fatal(err)
}
cr, err := crawler.New(
crawler.WithHTTPClient(client),
crawler.WithAllowedHosts(start.Host),
)
if err != nil {
log.Fatal(err)
}
var failures []failure
ctx, cancel := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
cancel()
}()
err = cr.Crawl(startURL, func(url string, res *crawler.Response, err error) error {
if err != nil {
failures = append(failures, failure{
inURL: "",
err: err,
kind: "page",
res: strings.TrimPrefix(url, startURL),
})
return nil
}
if verbose {
log.Printf("%s - Links: %d Assets: %d", url, len(res.Links), len(res.Assets))
}
in := strings.TrimPrefix(url, startURL)
for _, u := range res.Links {
err, ok := checked[u.URL]
if !ok {
err = checkURL(ctx, u.URL)
}
if err != nil {
failures = append(failures, failure{
inURL: in,
err: err,
kind: "link",
res: strings.TrimPrefix(u.URL, startURL),
})
}
checked[u.URL] = err
}
for _, u := range res.Assets {
err, ok := checked[u.URL]
if !ok {
err = checkURL(ctx, u.URL)
}
if err != nil {
failures = append(failures, failure{
inURL: in,
err: err,
kind: "resource",
res: strings.TrimPrefix(u.URL, startURL),
})
}
checked[u.URL] = err
}
return ctx.Err()
})
if err != nil {
log.Fatal(err)
}
for _, f := range failures {
log.Println(f.Error())
}
if l := len(failures); l > 0 {
log.Fatalf("%d broken", l)
}
}
type failure struct {
inURL string
kind string
res string
err error
}
func (f failure) Error() string {
return fmt.Sprintf("in: %s %s: %s - %s", f.inURL, f.kind, f.res, f.err.Error())
}
func checkURL(ctx context.Context, u string) error {
if err := ctx.Err(); err != nil {
return err
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("responded with %s", res.Status)
}
return nil
}
func init() {
// TODO: Remove this with Go 1.8
// Disable http2 since the Go 1.7 client has an bug caulsing false positives
os.Setenv("GODEBUG", "http2client=0")
}