mirror of
https://github.com/golang/go
synced 2024-11-07 14:46:14 -07:00
net/http: support configuring fetch options
The default WASM RoundTripper is implemented using the browser Fetch API. Some options don't readily map to existing http.Request options, so we use the precedent set by the TrailerPrefix constant to allow a user to configure the "mode" and "credentials" options by supplying them as headers in the http.Request. Updates #26769
This commit is contained in:
parent
4cc09cd532
commit
b230502084
@ -17,17 +17,27 @@ import (
|
|||||||
"syscall/js"
|
"syscall/js"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ModeHeader 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 ModeHeader = "Fetch:Mode"
|
||||||
|
|
||||||
|
// CredentialsHeader 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 CredentialsHeader = "Fetch:Credentials"
|
||||||
|
|
||||||
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
|
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
|
||||||
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
|
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
|
||||||
if useFakeNetwork() {
|
if useFakeNetwork() {
|
||||||
return t.roundTrip(req)
|
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")
|
ac := js.Global().Get("AbortController")
|
||||||
if ac != js.Undefined() {
|
if ac != js.Undefined() {
|
||||||
@ -40,12 +50,26 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
|
|||||||
opt := js.Global().Get("Object").New()
|
opt := js.Global().Get("Object").New()
|
||||||
// See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
// See https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
|
||||||
// for options available.
|
// for options available.
|
||||||
opt.Set("headers", headers)
|
|
||||||
opt.Set("method", req.Method)
|
opt.Set("method", req.Method)
|
||||||
opt.Set("credentials", "same-origin")
|
opt.Set("credentials", "same-origin")
|
||||||
|
if h := req.Header.Get(CredentialsHeader); h != "" {
|
||||||
|
opt.Set("credentials", h)
|
||||||
|
req.Header.Del(CredentialsHeader)
|
||||||
|
}
|
||||||
|
if h := req.Header.Get(ModeHeader); h != "" {
|
||||||
|
opt.Set("credentials", h)
|
||||||
|
req.Header.Del(ModeHeader)
|
||||||
|
}
|
||||||
if ac != js.Undefined() {
|
if ac != js.Undefined() {
|
||||||
opt.Set("signal", ac.Get("signal"))
|
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 {
|
if req.Body != nil {
|
||||||
// TODO(johanbrandhorst): Stream request body when possible.
|
// TODO(johanbrandhorst): Stream request body when possible.
|
||||||
|
Loading…
Reference in New Issue
Block a user