-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathurl.go
76 lines (65 loc) · 1.64 KB
/
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
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
69
70
71
72
73
74
75
76
package netutil
import (
"net"
"net/http"
"net/url"
"time"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/retry/backoff"
)
// ValidateHttpUrl validates a URL for an HTTP scheme
func ValidateHttpUrl(
value string,
requireSecureConnection bool,
fetchContent bool,
) error {
parsed, err := url.Parse(value)
if err != nil {
return err
}
if len(parsed.Scheme) == 0 {
// Add a HTTP scheme by default
value = "http://" + value
parsed, err = url.Parse(value)
if err != nil {
return err
}
}
if requireSecureConnection && parsed.Scheme != "https" {
return errors.New("url scheme must be https")
}
if parsed.Scheme != "http" && parsed.Scheme != "https" {
return errors.New("url scheme must be http or https")
}
if len(parsed.Host) == 0 {
return errors.New("host component missing")
} else if err := ValidateDomainName(parsed.Host); err != nil {
return errors.Wrap(err, "host is not a valid domain name")
}
if fetchContent {
// Best-effort attempt to fetch the content
var resp *http.Response
_, err = retry.Retry(
func() error {
resp, err = http.Get(value)
return err
},
retry.Limit(5),
retry.Backoff(backoff.BinaryExponential(100*time.Millisecond), time.Second),
)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Errorf("%d status code fetching content", resp.StatusCode)
}
} else {
// If not fetching content, then ensure the hostname is valid
_, err := net.LookupIP(parsed.Hostname())
if err != nil {
return errors.Wrap(err, "error resolving hostname")
}
}
return nil
}