Check if host works first before assigning random host
This commit is contained in:
@@ -198,11 +198,12 @@ func (r *HTTPClient) replaceHostIfNeeded(req *http.Request) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
r.log.Debug("Not found, assigning random IPv6 host")
|
||||
// random IPv6 host
|
||||
if !found && !r.CanFetchFirstByte(req.URL.String()) {
|
||||
req.Host = r.ipv6Hosts[rand.Intn(len(r.ipv6Hosts))]
|
||||
req.URL.Host = req.Host
|
||||
r.log.Debugf("Host is not a valid IPv6 host, assigning a random IPv6 host: %s", req.Host)
|
||||
} else {
|
||||
r.ipv6Hosts = append(r.ipv6Hosts, req.Host)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,3 +268,28 @@ func backoffFunc(attempt int) time.Duration {
|
||||
}
|
||||
return time.Duration(backoff) * time.Second
|
||||
}
|
||||
|
||||
func (r *HTTPClient) CanFetchFirstByte(url string) bool {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Set("Range", "bytes=0-0")
|
||||
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// If server supports partial content
|
||||
if resp.StatusCode/100 == 2 {
|
||||
buffer := make([]byte, 1)
|
||||
_, err = resp.Body.Read(buffer)
|
||||
if err == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user