Skip to content

Commit fac0bec

Browse files
authored
handle some broken redirects to pre-signed S3 URLs (#438)
* handle some broken redirects to pre-signed S3 URLs * making this more generic * more concise
1 parent 12a07cb commit fac0bec

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

request/http.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ func (r *Http) SetSecret(secret dmfr.Secret) error {
2626
return nil
2727
}
2828

29+
func removeDefaultPortFromHost(req *http.Request) {
30+
if (req.URL.Scheme == "https" && strings.HasSuffix(req.URL.Host, ":443")) ||
31+
(req.URL.Scheme == "http" && strings.HasSuffix(req.URL.Host, ":80")) {
32+
req.Host = strings.Split(req.URL.Host, ":")[0]
33+
}
34+
}
35+
2936
func (r Http) Download(ctx context.Context, ustr string) (io.ReadCloser, int, error) {
3037
return r.DownloadAuth(ctx, ustr, dmfr.FeedAuthorization{})
3138
}
@@ -68,7 +75,16 @@ func (r Http) DownloadAuth(ctx context.Context, ustr string, auth dmfr.FeedAutho
6875
// Make HTTP request
6976
req.Header.Set("User-Agent", fmt.Sprintf("transitland/%s", tl.Version.Tag))
7077

71-
client := &http.Client{}
78+
// Remove default ports from host header if explicitly specified as it
79+
// may break pre-signed S3 URLs or other systems that rely on the host header
80+
removeDefaultPortFromHost(req)
81+
82+
client := &http.Client{
83+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
84+
removeDefaultPortFromHost(req)
85+
return nil
86+
},
87+
}
7288
resp, err := client.Do(req)
7389
if err != nil {
7490
// return error directly

0 commit comments

Comments
 (0)