Skip to content

net/http: guarantee that the Transport dial functions are respected in js/wasm #46923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ Nathan Youngman <[email protected]>
Nathaniel Cook <[email protected]>
Naveen Kumar Sangi <[email protected]>
Neelesh Chandola <[email protected]>
Neil Alexander <[email protected]>
Neil Lyons <[email protected]>
Netflix, Inc.
Neuman Vong <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,7 @@ Naveen Kumar Sangi <[email protected]>
Neeilan Selvalingam <[email protected]>
Neelesh Chandola <[email protected]>
Nehal J Wani <[email protected]>
Neil Alexander <[email protected]>
Neil Lyons <[email protected]>
Neuman Vong <[email protected]>
Neven Sajko <[email protected]>
Expand Down
12 changes: 10 additions & 2 deletions src/net/http/roundtrip_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/net/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/net/http/transport_default_js.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions src/net/http/transport_default_other.go
Original file line number Diff line number Diff line change
@@ -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
}