-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
48 lines (43 loc) · 961 Bytes
/
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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"time"
)
func fetchRemoteResource(client *http.Client, url string) ([]byte, error) {
r, err := client.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
return io.ReadAll(r.Body)
}
func redirectPolicyFunc(req *http.Request, via []*http.Request) error {
if len(via) >= 1 {
return errors.New(fmt.Sprintf("Attempted redirect to %s", req.URL))
}
return nil
}
func createHTTPClientWithTimeout(d time.Duration) *http.Client {
client := http.Client{
Timeout: d,
CheckRedirect: redirectPolicyFunc,
}
return &client
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stdout, "Must specify a HTTP URL to get data from")
os.Exit(1)
}
client := createHTTPClientWithTimeout(15 * time.Second)
body, err := fetchRemoteResource(client, os.Args[1])
if err != nil {
fmt.Fprintf(os.Stdout, "%v\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "%s\n", body)
}