src/net/http/roundtrip_js.go | 38 +++++++++++++++++++++++++++++++------- diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go index cb4a4392b5198543e2af34f534b4d22d414e7408..16b7b891c86b1562763640ea97f078ee33a25fd8 100644 --- a/src/net/http/roundtrip_js.go +++ b/src/net/http/roundtrip_js.go @@ -17,17 +17,27 @@ "strings" "syscall/js" ) +// jsFetchMode is a Request.Header map key that, if present, +// signals that the map entry is actually an option to the Fetch API mode setting. +// Valid values are: "cors", "no-cors", "same-origin", "navigate" +// The default is "same-origin". +// +// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters +const jsFetchMode = "js.fetch:mode" + +// jsFetchCreds is a Request.Header map key that, if present, +// signals that the map entry is actually an option to the Fetch API credentials setting. +// Valid values are: "omit", "same-origin", "include" +// The default is "same-origin". +// +// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters +const jsFetchCreds = "js.fetch:credentials" + // RoundTrip implements the RoundTripper interface using the WHATWG Fetch API. func (t *Transport) RoundTrip(req *Request) (*Response, error) { if useFakeNetwork() { return t.roundTrip(req) } - headers := js.Global().Get("Headers").New() - for key, values := range req.Header { - for _, value := range values { - headers.Call("append", key, value) - } - } ac := js.Global().Get("AbortController") if ac != js.Undefined() { @@ -40,12 +50,26 @@ opt := js.Global().Get("Object").New() // See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch // for options available. - opt.Set("headers", headers) opt.Set("method", req.Method) opt.Set("credentials", "same-origin") + if h := req.Header.Get(jsFetchCreds); h != "" { + opt.Set("credentials", h) + req.Header.Del(jsFetchCreds) + } + if h := req.Header.Get(jsFetchMode); h != "" { + opt.Set("mode", h) + req.Header.Del(jsFetchMode) + } if ac != js.Undefined() { opt.Set("signal", ac.Get("signal")) } + headers := js.Global().Get("Headers").New() + for key, values := range req.Header { + for _, value := range values { + headers.Call("append", key, value) + } + } + opt.Set("headers", headers) if req.Body != nil { // TODO(johanbrandhorst): Stream request body when possible.