From 94cbfc2f7f6b375426381c81d8efe78b12e058c3 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Sun, 3 Mar 2019 20:16:13 +0000 Subject: [PATCH] net/http: support configuring redirect fetch option Adds a magic header value that is translated to the Fetch API redirect option, following existing practices. Updates #26769 Change-Id: Iaf1c9f710de63ea941a360b73f1b4bb725331a35 Reviewed-on: https://go-review.googlesource.com/c/go/+/164666 Reviewed-by: Richard Musiol Reviewed-by: Agniva De Sarker Run-TryBot: Agniva De Sarker TryBot-Result: Gobot Gobot --- src/net/http/roundtrip_js.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index 1e38b908d3..21d19515fa 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -33,6 +33,14 @@ const jsFetchMode = "js.fetch:mode" // Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters const jsFetchCreds = "js.fetch:credentials" +// jsFetchRedirect is a Request.Header map key that, if present, +// signals that the map entry is actually an option to the Fetch API redirect setting. +// Valid values are: "follow", "error", "manual" +// The default is "follow". +// +// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters +const jsFetchRedirect = "js.fetch:redirect" + // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API. func (t *Transport) RoundTrip(req *Request) (*Response, error) { if useFakeNetwork() { @@ -60,6 +68,10 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) { opt.Set("mode", h) req.Header.Del(jsFetchMode) } + if h := req.Header.Get(jsFetchRedirect); h != "" { + opt.Set("redirect", h) + req.Header.Del(jsFetchRedirect) + } if ac != js.Undefined() { opt.Set("signal", ac.Get("signal")) } -- 2.50.0