summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2021-09-03 21:07:22 +0000
committerDamien Neil <dneil@google.com>2021-10-28 17:57:57 +0000
commit266626211e40d1f2c3a34fa4cd2023f5310cbd7d (patch)
tree12d22ceba0951d6e8351d46fbc845d354c3d7837 /src
parent31249688487ca3a60f856ad082d4e84c4d5f79fb (diff)
downloadgo-git-266626211e40d1f2c3a34fa4cd2023f5310cbd7d.tar.gz
net/http: guarantee that the Transport dial functions are respected in js/wasm
The net/http package has a documented contract that if DialContext, DialDLSContext, Dial or DialTLS are specified in an instance of Transport, that they will be used to set up the connection. If they are not specified, then a reasonable fallback is made (e.g. using the net package). This is ordinarily true, except for when compiling for the js/wasm target, where the browser's Fetch API is preferred in all cases (except for when it is undefined/unavailable) and therefore the dial functions are all ignored. As a result, the http.Transport implementation under js/wasm doesn't meet that contract. This PR updates the RoundTrip behaviour of http.Transport so that if DialContext, DialTLSContext, Dial or DialTLS are specified, they are used as expected. The Fetch API will be used as a fallback if they are not specified. Fixes #27495 Change-Id: I88c6eb6ffdd077827b421d606f3e60ebdafd538f GitHub-Last-Rev: 948a0ed97ec4661f03cd346335f1d3d180f4c1ae GitHub-Pull-Request: golang/go#46923 Reviewed-on: https://go-review.googlesource.com/c/go/+/330852 Reviewed-by: Damien Neil <dneil@google.com> Trust: Damien Neil <dneil@google.com> Trust: Carlos Amedee <carlos@golang.org> Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/net/http/roundtrip_js.go12
-rw-r--r--src/net/http/transport.go4
-rw-r--r--src/net/http/transport_default_js.go17
-rw-r--r--src/net/http/transport_default_other.go17
4 files changed, 46 insertions, 4 deletions
diff --git a/src/net/http/roundtrip_js.go b/src/net/http/roundtrip_js.go
index 362dbcbdde..4a8595ebcf 100644
--- a/src/net/http/roundtrip_js.go
+++ b/src/net/http/roundtrip_js.go
@@ -41,11 +41,19 @@ const jsFetchCreds = "js.fetch:credentials"
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
const jsFetchRedirect = "js.fetch:redirect"
-var useFakeNetwork = js.Global().Get("fetch").IsUndefined()
+// jsFetchMissing will be true if the Fetch API is not present in
+// the browser globals.
+var jsFetchMissing = js.Global().Get("fetch").IsUndefined()
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
- if useFakeNetwork {
+ // The Transport has a documented contract that states that if the DialContext or
+ // DialTLSContext functions are set, they will be used to set up the connections.
+ // If they aren't set then the documented contract is to use Dial or DialTLS, even
+ // though they are deprecated. Therefore, if any of these are set, we should obey
+ // the contract and dial using the regular round-trip instead. Otherwise, we'll try
+ // to fall back on the Fetch API, unless it's not available.
+ if t.Dial != nil || t.DialContext != nil || t.DialTLS != nil || t.DialTLSContext != nil || jsFetchMissing {
return t.roundTrip(req)
}
diff --git a/src/net/http/transport.go b/src/net/http/transport.go
index 17f0047b59..0e60992e6c 100644
--- a/src/net/http/transport.go
+++ b/src/net/http/transport.go
@@ -42,10 +42,10 @@ import (
// $no_proxy) environment variables.
var DefaultTransport RoundTripper = &Transport{
Proxy: ProxyFromEnvironment,
- DialContext: (&net.Dialer{
+ DialContext: defaultTransportDialContext(&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
- }).DialContext,
+ }),
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
diff --git a/src/net/http/transport_default_js.go b/src/net/http/transport_default_js.go
new file mode 100644
index 0000000000..c07d35ef86
--- /dev/null
+++ b/src/net/http/transport_default_js.go
@@ -0,0 +1,17 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build js && wasm
+// +build js,wasm
+
+package http
+
+import (
+ "context"
+ "net"
+)
+
+func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
+ return nil
+}
diff --git a/src/net/http/transport_default_other.go b/src/net/http/transport_default_other.go
new file mode 100644
index 0000000000..8a2f1cc42b
--- /dev/null
+++ b/src/net/http/transport_default_other.go
@@ -0,0 +1,17 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build !(js && wasm)
+// +build !js !wasm
+
+package http
+
+import (
+ "context"
+ "net"
+)
+
+func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
+ return dialer.DialContext
+}