forked from tomnomnom/gron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
41 lines (34 loc) · 736 Bytes
/
url.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
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net/http"
"regexp"
"time"
)
func validURL(url string) bool {
r := regexp.MustCompile("(?i)^http(?:s)?://")
return r.MatchString(url)
}
func getURL(url string, insecure bool) (io.Reader, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
}
client := http.Client{
Transport: tr,
Timeout: 20 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf("gron/%s", gronVersion))
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
return bufio.NewReader(resp.Body), err
}