Skip to content

Commit e77cf4b

Browse files
antongAnton Gyllenberg
authored and
Anton Gyllenberg
committed
net/http: expose standard transport for js
Implement a separate type jsTransport that implements RoundTripper using the WHATWG Fetch API, and make it the DefaultTransport on js/wasm. This allows using the standard Transport implementation on js/wasm by specifying a DialContext for the Transport. Note that the default would use net.Dial, that can not be used in the browser. Before this change the same Transport type was used also on js, but with a different implementation. This implementation disregarded any of the documented Transport settings and just used the browser Fetch API. This made it impossible to use the Transport implementation, e.g., by supplying a DialContext that works in the browser. Fixes golang#27495
1 parent e2dc41b commit e77cf4b

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/net/http/default_transport.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2011 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !js
6+
7+
package http
8+
9+
// DefaultTransport is the default implementation of Transport and is
10+
// used by DefaultClient. It establishes network connections as needed
11+
// and caches them for reuse by subsequent calls. It uses HTTP proxies
12+
// as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
13+
// $no_proxy) environment variables.
14+
var DefaultTransport RoundTripper = defaultTransport

src/net/http/default_transport_js.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build js,wasm
6+
7+
package http
8+
9+
// DefaultTransport is the default implementation of Transport and is
10+
// used by DefaultClient. It implements the RoundTripper interface
11+
// using the WHATWG Fetch API.
12+
var DefaultTransport RoundTripper = (*jsTransport)(nil)

src/net/http/roundtrip.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build !js !wasm
6-
75
package http
86

97
// RoundTrip implements the RoundTripper interface.

src/net/http/roundtrip_js.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ const jsFetchCreds = "js.fetch:credentials"
4141
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters
4242
const jsFetchRedirect = "js.fetch:redirect"
4343

44+
type jsTransport struct{}
45+
4446
// RoundTrip implements the RoundTripper interface using the WHATWG Fetch API.
45-
func (t *Transport) RoundTrip(req *Request) (*Response, error) {
47+
func (_ *jsTransport) RoundTrip(req *Request) (*Response, error) {
4648
if useFakeNetwork() {
47-
return t.roundTrip(req)
49+
return defaultTransport.roundTrip(req)
4850
}
4951

5052
ac := js.Global().Get("AbortController")

src/net/http/transport.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ import (
3434
"internal/x/net/http/httpproxy"
3535
)
3636

37-
// DefaultTransport is the default implementation of Transport and is
38-
// used by DefaultClient. It establishes network connections as needed
39-
// and caches them for reuse by subsequent calls. It uses HTTP proxies
40-
// as directed by the $HTTP_PROXY and $NO_PROXY (or $http_proxy and
41-
// $no_proxy) environment variables.
42-
var DefaultTransport RoundTripper = &Transport{
37+
var defaultTransport = &Transport{
4338
Proxy: ProxyFromEnvironment,
4439
DialContext: (&net.Dialer{
4540
Timeout: 30 * time.Second,

0 commit comments

Comments
 (0)