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
net/http: Expose the normal transport RoundTripper for WASM/js #27495
Comments
I quickly tested two hacks to do this, and both seemed to work. Both pass the tests on normal platforms, but I don't know how to run the unit tests on WASM. Hack 1: Don't implement the special RoundTripper on Hack 2: Mod the special RoundTripper to instead use the normal RoundTripper if the caller has set options on the Transport. Example: 8137bf5 |
This is also related to #25695. |
running into this problem when trying to do HTTPS requests in wasm/js (using self signed certs over a websocket connection). e8050da seems like an easy and clean solution to enable a lot more flexibility when desired. as far as I can tell the only place where I understand #25695 discusses a more major problem, but specially considering the impact of fixing that as a whole, maybe a simple fix like this would be a good start? |
I agree Hack 1 as proposed by @antong is a reasonable short term approach. Are you interested in contributing this? |
Sure! Do you mean that I should make a CL based on Hack 1 (e8050da)? |
Yes, that sounds good. |
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
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
Change https://golang.org/cl/166617 mentions this issue: |
Hi all, i just bumped into this issues as i tried to implement some tunneling for http request in wasm. As Transport currently does not provide any ways to customize the wasm/js RoundTrip behavior i wonder if it would not be fair to simple fallback to the "normal" roundTrip implementation in case a custom Transport struct is the caller and not the DeafultTransport ( |
Change https://golang.org/cl/330852 mentions this issue: |
On WASM, the standard
http.Transfer
RoundTripper is not exposed for external use. Buildflags for wasm replace the standard implementation of RoundTripper with a special version that uses the browser Fetch API. The special JS RoundTripper does not seem to use or honor any of the documented settings provided byhttp.Transport
, such asDial
orDialContext
. But the way the special JS RoundTripper is implemented as methods onhttp.Transport
, it prevents using the standard RoundTripper.There are use cases for using the normal RoundTripper, for instance for testing and simulation in the browser. The normal RoundTripper is compiled and included in the binary anyway, and works perfectly well, but is masked by the browser version.
What version of Go are you using (
go version
)?go1.11
What operating system and processor architecture are you using (
go env
)?GOOS=js
GOARCH=wasm
What did you do?
I tried to configure a
http.Client
to use a transport with a custom dial function:https://play.golang.org/p/dnyTivQ643J
This works on all other platforms including the playground, but on WASM, the same, special Roundtripper is always used and there is no way to use the standard Roundtripper.
What did you expect to see?
What did you see instead?
On WASM:
On other platforms, I get the expected result
The text was updated successfully, but these errors were encountered: