From e818eeb800ccdceb03ffdf5ed7ac19deb015ddfb Mon Sep 17 00:00:00 2001 From: William Elwood Date: Wed, 30 Oct 2019 04:12:50 +0000 Subject: [PATCH] Refactor reading a URL's content to own function No longer shadows `url` package with variable of the same name. --- dnscrypt-proxy/sources.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/dnscrypt-proxy/sources.go b/dnscrypt-proxy/sources.go index bc976f9c..7101fe40 100644 --- a/dnscrypt-proxy/sources.go +++ b/dnscrypt-proxy/sources.go @@ -79,6 +79,15 @@ func fetchFromCache(cacheFile string, refreshDelay time.Duration) (in string, ex return } +func fetchFromURL(xTransport *XTransport, u *url.URL) (bin []byte, err error) { + var resp *http.Response + if resp, _, err = xTransport.Get(u, "", DefaultTimeout); err == nil { + bin, err = ioutil.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) + resp.Body.Close() + } + return +} + func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, refreshDelay time.Duration) (in string, cached bool, delayTillNextUpdate time.Duration, err error) { cached = false expired := false @@ -98,24 +107,16 @@ func fetchWithCache(xTransport *XTransport, urlStr string, cacheFile string, ref return } - var resp *http.Response dlog.Infof("Loading source information from URL [%s]", urlStr) - url, err := url.Parse(urlStr) - if err != nil { - return - } - resp, _, err = xTransport.Get(url, "", 30*time.Second) - if err != nil { + var u *url.URL + if u, err = url.Parse(urlStr); err != nil { return } var bin []byte - bin, err = ioutil.ReadAll(io.LimitReader(resp.Body, MaxHTTPBodyLength)) - resp.Body.Close() - if err != nil { + if bin, err = fetchFromURL(xTransport, u); err != nil { return } - err = nil cached = false in = string(bin) delayTillNextUpdate = MinSourcesUpdateDelay