-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
43 lines (39 loc) · 977 Bytes
/
http.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
func getFile(fileUrlString string) ([]byte, error) {
fileUrl, err := url.Parse(fileUrlString)
if err == nil && fileUrl.Scheme == "file" {
fileUrl.Scheme = ""
return ioutil.ReadFile(fileUrl.String())
} else if err != nil || fileUrl.Scheme == "" {
return ioutil.ReadFile(fileUrlString)
}
client := &http.Client{}
client.Timeout = time.Second * 30
resp, err := client.Get(fileUrlString)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("received bad status code %s", resp.Status)
}
return ioutil.ReadAll(resp.Body)
}
func getHttpHeadResult(url string) (responseCode int, err error) {
client := &http.Client{}
client.Timeout = time.Second * 30
var response *http.Response
response, err = client.Head(url)
if err != nil {
return 0, err
}
defer response.Body.Close()
return response.StatusCode, err
}