/
check.go
254 lines (238 loc) · 5.62 KB
/
check.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
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
package usecase
import (
"bufio"
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"golang.org/x/sync/errgroup"
"mvdan.cc/xurls/v2"
"github.com/pkg/errors"
"github.com/scylladb/go-set/strset"
"github.com/suzuki-shunsuke/durl/internal/domain"
)
func (lgc *logic) Check(stdin io.Reader, cfgPath string) error {
// get file paths
files, err := lgc.logic.GetFiles(stdin)
if err != nil {
return err
}
// urls is a map whose key is url and value is file paths which include the url
// url -> file paths
urls, err := lgc.logic.ExtractURLsFromFiles(files)
if err != nil {
return err
}
// filter url
for u := range urls {
if lgc.logic.IsIgnoredURL(u) {
delete(urls, u)
}
}
return lgc.logic.CheckURLs(urls)
}
func (lgc *logic) IsIgnoredURL(uri string) bool {
u, err := url.Parse(uri)
if err != nil {
// ignore url if it is failed to parse the url
return true
}
if u.Scheme != "http" && u.Scheme != "https" {
return true
}
for _, ignoreHost := range domain.IgnoreHosts {
if u.Host == ignoreHost || strings.HasPrefix(u.Host, fmt.Sprintf("%s:", ignoreHost)) {
return true
}
}
for _, u := range lgc.cfg.IgnoreURLs {
if uri == u {
return true
}
}
for _, h := range lgc.cfg.IgnoreHosts {
if u.Host == h {
return true
}
}
return false
}
func (lgc *logic) CheckURLs(urls map[string]*strset.Set) error {
if len(urls) == 0 {
return nil
}
if lgc.cfg.MaxRequestCount == 0 {
lgc.cfg.MaxRequestCount = domain.DefaultMaxRequestCount
}
semaphore := make(chan struct{}, lgc.cfg.MaxRequestCount)
resultChan := make(chan error, len(urls))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for u, files := range urls {
go func(u string, files *strset.Set) {
semaphore <- struct{}{}
err := lgc.logic.CheckURL(ctx, u)
<-semaphore
if err == nil {
resultChan <- nil
return
}
resultChan <- errors.Wrapf(err, "failed to check a url: %s %s", u, files.String())
}(u, files)
}
endCount := len(urls)
failedCount := lgc.cfg.MaxFailedRequestCount
for {
select {
case err := <-resultChan:
endCount--
if err != nil {
failedCount--
fmt.Fprintln(os.Stderr, err)
}
if endCount == 0 {
if failedCount != lgc.cfg.MaxFailedRequestCount {
return fmt.Errorf("")
}
return nil
}
if lgc.cfg.MaxFailedRequestCount != -1 && failedCount == -1 {
return fmt.Errorf("too many urls are dead")
}
case <-ctx.Done():
return fmt.Errorf("context is caceled")
}
}
}
func (lgc *logic) CheckURLWithMethod(
ctx context.Context, u, method string,
) error {
req, err := http.NewRequest(method, u, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := lgc.client.Do(req)
if err != nil {
return err
}
resp.Body.Close()
// check status code
if resp.StatusCode/100 != 2 {
return fmt.Errorf("%s is dead (%d)", u, resp.StatusCode)
}
return nil
}
func (lgc *logic) CheckURL(ctx context.Context, u string) error {
switch lgc.cfg.HTTPMethod {
case "head,get":
if err := lgc.logic.CheckURLWithMethod(ctx, u, http.MethodHead); err == nil {
return nil
}
return lgc.logic.CheckURLWithMethod(ctx, u, http.MethodGet)
case "":
if err := lgc.logic.CheckURLWithMethod(ctx, u, http.MethodHead); err == nil {
return nil
}
return lgc.logic.CheckURLWithMethod(ctx, u, http.MethodGet)
case "get":
return lgc.logic.CheckURLWithMethod(ctx, u, http.MethodGet)
case "head":
return lgc.logic.CheckURLWithMethod(ctx, u, http.MethodHead)
default:
return fmt.Errorf(`invalid http_method_type: %s`, lgc.cfg.HTTPMethod)
}
}
func (lgc *logic) ExtractURLsFromFiles(files *strset.Set) (map[string]*strset.Set, error) {
// return a map whose key is url and value is file paths which include the url
size := files.Size()
if size == 0 {
return nil, nil
}
// extract urls from all files
type (
File struct {
path string
urls *strset.Set
}
)
urlsChan := make(chan File, size)
eg, ctx := errgroup.WithContext(context.Background())
files.Each(func(p string) bool {
eg.Go(func() error {
// open a file and extract urls from it
arr, err := lgc.logic.ExtractURLsFromFile(ctx, p)
if err != nil {
// https://github.com/suzuki-shunsuke/durl/issues/27
fmt.Fprintf(os.Stderr, "failed to extract urls from a file %s: %s\n", p, err)
return nil
}
urlsChan <- File{path: p, urls: arr}
return nil
})
return true
})
if err := eg.Wait(); err != nil {
return nil, err
}
close(urlsChan)
// url -> file paths
urls := map[string]*strset.Set{}
for f := range urlsChan {
f.urls.Each(func(u string) bool {
v, ok := urls[u]
if ok {
v.Add(f.path)
return true
}
urls[u] = strset.New(f.path)
return true
})
}
return urls, nil
}
func (lgc *logic) ExtractURLsFromFile(ctx context.Context, p string) (*strset.Set, error) {
// open a file and extract urls from it
urls := strset.New()
errChan := make(chan error, 1)
reg := xurls.Strict()
go func() {
// open a file
fi, err := lgc.fsys.Open(p)
if err != nil {
errChan <- err
return
}
defer fi.Close()
// read a file per a line
scanner := bufio.NewScanner(fi)
for scanner.Scan() {
// extract urls from a line
urls.Add(reg.FindAllString(scanner.Text(), -1)...)
}
errChan <- scanner.Err()
}()
select {
case <-ctx.Done():
return nil, nil
case err := <-errChan:
if err != nil {
return urls, errors.Wrapf(err, "failed to read %s", p)
}
return urls, nil
}
}
func (lgc *logic) GetFiles(stdin io.Reader) (*strset.Set, error) {
files := strset.New()
if stdin != nil {
scanner := bufio.NewScanner(stdin)
for scanner.Scan() {
files.Add(scanner.Text())
}
return files, scanner.Err()
}
return files, nil
}