Skip to content

Commit 59bcbe2

Browse files
committed
utils.go: fixUrl(): Check the Location value before return
1 parent aa96f1a commit 59bcbe2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

utils.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"math/rand"
1111
"net"
1212
"net/http"
13+
"net/url"
1314
"strings"
1415
"sync/atomic"
1516
"time"
@@ -125,7 +126,7 @@ func isContentType(contentType string, h *http.Header) bool {
125126
// see:
126127
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
127128
// https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
128-
func getUrlContent(url, contentType string, bootstrap []string, timeout time.Duration) (string, error) {
129+
func getUrlContent(theUrl, contentType string, bootstrap []string, timeout time.Duration) (string, error) {
129130
var transport http.RoundTripper
130131

131132
if len(bootstrap) != 0 {
@@ -156,7 +157,7 @@ func getUrlContent(url, contentType string, bootstrap []string, timeout time.Dur
156157
// Fallback to use system default resolvers, which located at /etc/resolv.conf
157158
}
158159

159-
req, err := http.NewRequest(http.MethodGet, url, nil)
160+
req, err := http.NewRequest(http.MethodGet, theUrl, nil)
160161
if err != nil {
161162
return "", err
162163
}
@@ -178,10 +179,10 @@ func getUrlContent(url, contentType string, bootstrap []string, timeout time.Dur
178179
}
179180

180181
if len(contentType) != 0 && !isContentType(contentType, &resp.Header) {
181-
if url, err := fixUrl(url, resp.Header); err != nil {
182+
if theUrl, err = fixUrl(theUrl, resp.Header); err != nil {
182183
return "", err
183184
} else {
184-
return getUrlContent(url, contentType, bootstrap, timeout)
185+
return getUrlContent(theUrl, contentType, bootstrap, timeout)
185186
}
186187
}
187188

@@ -193,13 +194,16 @@ func getUrlContent(url, contentType string, bootstrap []string, timeout time.Dur
193194
return string(content), nil
194195
}
195196

196-
func fixUrl(url string, h http.Header) (string, error) {
197+
func fixUrl(theUrl string, h http.Header) (string, error) {
197198
const LocationKey = "Location"
198199
location := h.Get(LocationKey)
199200
if location != "" {
201+
if _, err := url.Parse(theUrl); err != nil {
202+
return "", fmt.Errorf("fixUrl(): url.Parse(): %w", err)
203+
}
200204
return location, nil
201205
}
202-
return "", fmt.Errorf("%q header key not found in %v", LocationKey, url)
206+
return "", fmt.Errorf("%q header key not found in %v", LocationKey, theUrl)
203207
}
204208

205209
func stringHash(str string) uint64 {

0 commit comments

Comments
 (0)