Skip to content

Commit 8577bd7

Browse files
committed
bitswap/httpnet: do not follow redirects
Avoid following redirects. We treat them as an incorrect provider record. We don't want to be coerced into opening new connections to new hosts and we prefer provider records to be up to date rather than them relying in redirects being followed. There are also problems with potential loops etc. #862
1 parent f1d5312 commit 8577bd7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

bitswap/network/httpnet/httpnet.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ func New(host host.Host, opts ...Option) network.BitSwapNetwork {
267267

268268
c := &http.Client{
269269
Transport: t,
270+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
271+
// we do not follow redirects. Providers should keep
272+
// announcements up to
273+
// date. https://github.com/boxo/issues/862.
274+
return http.ErrUseLastResponse
275+
},
270276
}
271277
htnet.client = c
272278

bitswap/network/httpnet/msg_sender.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ func (sender *httpMsgSender) tryURL(ctx context.Context, u *senderURL, entry bsm
302302
case http.StatusNotFound,
303303
http.StatusGone,
304304
http.StatusForbidden,
305-
http.StatusUnavailableForLegalReasons:
305+
http.StatusUnavailableForLegalReasons,
306+
http.StatusMovedPermanently,
307+
http.StatusFound,
308+
http.StatusSeeOther,
309+
http.StatusTemporaryRedirect,
310+
http.StatusPermanentRedirect:
306311

307312
err := fmt.Errorf("%s %q -> %d: %q", req.Method, req.URL, statusCode, string(body))
308313
log.Debug(err)

0 commit comments

Comments
 (0)