-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (49 loc) · 1.01 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
package main
import (
"fmt"
"log"
"net/http"
"os"
)
var httpClient *http.Client
func main() {
if len(os.Args) < 2 {
log.Fatal("No URLs supplied")
}
c := make(chan []string)
for _, v := range os.Args[1:] {
go makeRequest(http.MethodGet, v, c)
}
fmt.Println()
for range os.Args[1:] {
v := <-c
fmt.Printf("URL: %v - Redirected URL: %v\n", v[0], v[1])
}
}
func makeRequest(method, url string, c chan []string) {
req, err := getRequestClient(method, url)
if err != nil {
c <- []string{url, err.Error()}
return
}
req.Header.Set("Content-Type", "application/json")
resp, err := getHTTPClient().Do(req)
if err != nil {
c <- []string{url, err.Error()}
return
}
if resp.Request.URL != nil {
c <- []string{url, resp.Request.URL.Host}
} else {
c <- []string{url, ""}
}
}
func getHTTPClient() *http.Client {
if httpClient == nil {
httpClient = &http.Client{}
}
return httpClient
}
func getRequestClient(method, url string) (*http.Request, error) {
return http.NewRequest(method, url, nil)
}