Skip to content

Commit

Permalink
net/http: support configuring fetch options
Browse files Browse the repository at this point in the history
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.

Improves golang#26769
  • Loading branch information
johanbrandhorst committed Aug 3, 2018
1 parent 4cc09cd commit bbaf1b7
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,29 @@ import (
"syscall/js"
)

// ModeHeader is a magic prefix for Request.Header map keys
// 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 = "Mode:"

// CredentialsHeader is a magic prefix for Request.Header map keys
// 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 = "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() {
Expand All @@ -40,12 +52,30 @@ func (t *Transport) RoundTrip(req *Request) (*Response, error) {
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, ok := req.Header[CredentialsHeader]; ok {
if len(h) > 0 {
opt.Set("credentials", h[0])
}
req.Header.Del(CredentialsHeader)
}
if h, ok := req.Header[ModeHeader]; ok {
if len(h) > 0 {
opt.Set("mode", h[0])
}
req.Header.Del(ModeHeader)
}
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.
Expand Down

0 comments on commit bbaf1b7

Please sign in to comment.