-
Notifications
You must be signed in to change notification settings - Fork 0
/
url-bodylength.go
68 lines (62 loc) · 1.12 KB
/
url-bodylength.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
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
var urlsFile = flag.String("file", "", "File with URLs")
type Work struct {
url string
length int
proto string
err error
}
func get(ch chan Work, u string) {
r, err := http.Get(u)
if err != nil {
ch <- Work{url: u, err: err}
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ch <- Work{url: u, err: err}
return
}
ch <- Work{url: u, length: len(body), proto: r.Proto}
}
func main() {
flag.Parse()
if *urlsFile == "" {
log.Fatal("-file flag is required")
}
file, err := os.Open(*urlsFile)
if err != nil {
log.Fatal(err)
}
var urls []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
urls = append(urls, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
ch := make(chan Work)
for _, url := range urls {
fmt.Printf("Sending %s for a new worker to get.\n", url)
go get(ch, url)
}
for _ = range urls {
r := <-ch
if r.err != nil {
fmt.Println(r.err)
continue
}
fmt.Printf("%s (%s) length %d.\n", r.url, r.proto, r.length)
}
}