aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohan Brandhorst <johan.brandhorst@gmail.com>2018-08-03 14:48:16 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2018-08-13 19:04:16 +0000
commit4fc7b93ad7c36b769ef54b4706e6a045d7a8e311 (patch)
treee6c29b3afccc81641f5ec906236d33c4d461cdcc
parent7a6fc1f30bc7339726cd3f93f96be3e0d36ff7cb (diff)
downloadgo-4fc7b93ad7c36b769ef54b4706e6a045d7a8e311.tar.gz
go-4fc7b93ad7c36b769ef54b4706e6a045d7a8e311.zip
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 Change-Id: If42d24418c4ffb17211f57e36708cf460fb4c579 GitHub-Last-Rev: b230502084d628938cd50818d3d336f9f911d48d GitHub-Pull-Request: golang/go#26784 Reviewed-on: https://go-review.googlesource.com/127718 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
-rw-r--r--src/net/http/roundtrip_js.go38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go
index cb4a4392b5..16b7b891c8 100644
--- a/src/net/http/roundtrip_js.go
+++ b/src/net/http/roundtrip_js.go
@@ -17,17 +17,27 @@ import (
"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 @@ 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 := 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.