Skip to content

Commit d7e2dc8

Browse files
authored
bitswap/httpnet: do not follow redirects (#878)
* 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 * bitswap/httpnet: close response bodies "The default HTTP client's Transport may not reuse HTTP/1.x "keep-alive" TCP connections if the Body is not read to completion and closed."
1 parent cf6b088 commit d7e2dc8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

bitswap/network/httpnet/httpnet.go

+6
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

+7-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ func (sender *httpMsgSender) tryURL(ctx context.Context, u *senderURL, entry bsm
247247

248248
return nil, serr
249249
}
250+
defer resp.Body.Close()
250251

251252
// Record request size
252253
var buf bytes.Buffer
@@ -302,7 +303,12 @@ func (sender *httpMsgSender) tryURL(ctx context.Context, u *senderURL, entry bsm
302303
case http.StatusNotFound,
303304
http.StatusGone,
304305
http.StatusForbidden,
305-
http.StatusUnavailableForLegalReasons:
306+
http.StatusUnavailableForLegalReasons,
307+
http.StatusMovedPermanently,
308+
http.StatusFound,
309+
http.StatusSeeOther,
310+
http.StatusTemporaryRedirect,
311+
http.StatusPermanentRedirect:
306312

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

0 commit comments

Comments
 (0)