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: no way to set Fetch options when making outgoing http calls with js/wasm #26769

Open
eginez opened this Issue Aug 2, 2018 · 14 comments

Comments

Projects
None yet
5 participants
@eginez

eginez commented Aug 2, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version devel +c814ac44c0 Thu Jul 19 21:30:27 2018 +0000 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="wasm"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="js"
GOPATH="/Users/eginez/repos/goland"
GOPROXY=""
GORACE=""
GOROOT="/Users/eginez/repos/go"
GOTMPDIR=""
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/pm/tlg5p5nx4vj3s05h4rfsv3t40000gn/T/go-build136489492=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

The native http client, maps to the fetch() api. With the current client it is impossible to set additional options as supported by the fetch call, see: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch.

What did you expect to see?

A way to specify options for the fetch call

@eginez eginez changed the title from No way to set options when making outgoing http calls WASM to Wasm: No way to set options when making outgoing http calls Aug 2, 2018

@eginez eginez changed the title from Wasm: No way to set options when making outgoing http calls to Wasm: No way to set fetch options when making outgoing http calls Aug 2, 2018

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 2, 2018

Yes, this is known. Currently, there is no good way to expose fetch parameters from user level to the http.RoundTripper which is what we are using to make the fetch call. It is not very straightforward, as we have to make some decisions on how best to integrate browser options into a Go API.

See a very small discussion here - https://go-review.googlesource.com/c/go/+/114515/8/src/net/http/roundtrip_js.go#34

The main tracking issue for a better wasm http client is #25695. I would suggest that you comment on that issue with your suggestions on how best we can take this forward.

@bradfitz

This comment has been minimized.

Member

bradfitz commented Aug 2, 2018

@agnivade, things aren't that doomed. We can use http.Request.WithContext or HTTP headers to something to set options for Fetch.

/cc @neelance @dmitshur

@bradfitz

This comment has been minimized.

Member

bradfitz commented Aug 2, 2018

(That is, we don't need to wait for a new HTTP client package design to make progress on this)

@bradfitz bradfitz changed the title from Wasm: No way to set fetch options when making outgoing http calls to net/http: no way to set Fetch options when making outgoing http calls with js/wasm Aug 2, 2018

@bradfitz bradfitz added this to the Unplanned milestone Aug 2, 2018

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 2, 2018

Ah, using WithContext sounds like a good idea. I was overcomplicating things.

Also @johanbrandhorst

@johanbrandhorst

This comment has been minimized.

Member

johanbrandhorst commented Aug 2, 2018

That's a cool idea, but how do we make it obvious to the user? Is using the context as a transport for arbitrary downstream method invocations something well established? I'd be happy to implement this if there's a proposal.

@bradfitz

This comment has been minimized.

Member

bradfitz commented Aug 2, 2018

Using contexts for options is actually generally considered a gross practice. But we can do so if needed.

But before designing anything, what are the specific option(s) we're talking about here? Maybe they already map to something we have in Go.

@agnivade

This comment has been minimized.

Member

agnivade commented Aug 3, 2018

To me, mode and credentials are the 2 most important options that we should expose.

  • mode can take cors, no-cors, same-origin.
  • credentials can take omit, same-origin, include.

These are settings which arise only in a browser context. So as I see, there is no clear way to map them in Go.

We also have redirect which somehow maps with client.CheckRedirect.

See here for remaining options - https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters

@bradfitz

This comment has been minimized.

Member

bradfitz commented Aug 3, 2018

Another option: we could use magic header values, like https://golang.org/pkg/net/http/#TrailerPrefix (it contains a colon which is illegal in general, so we special case its semantics).

@johanbrandhorst

This comment has been minimized.

Member

johanbrandhorst commented Aug 3, 2018

I have the beginnings of a CL ready for this. Will put it up later today.

johanbrandhorst added a commit to johanbrandhorst/go that referenced this issue Aug 3, 2018

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.

Improves golang#26769

johanbrandhorst added a commit to johanbrandhorst/go that referenced this issue Aug 3, 2018

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 golang#26769
@gopherbot

This comment has been minimized.

gopherbot commented Aug 3, 2018

Change https://golang.org/cl/127718 mentions this issue: net/http: support configuring fetch options

johanbrandhorst added a commit to johanbrandhorst/go that referenced this issue Aug 3, 2018

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 golang#26769
@eginez

This comment has been minimized.

eginez commented Aug 3, 2018

@johanbrandhorst thanks for the quick turnaround on this issue. One quick comment I have on your PR. As a stop gap and until we can rehaul parts of the client, I see no problems with this approach. On the other hand I am really not inclined with the solution as presented if it this is going to be the long term answer. The idea that @bradfitz had about using context seems less magical to me. Thoughts?

@johanbrandhorst

This comment has been minimized.

Member

johanbrandhorst commented Aug 3, 2018

Please put comments on the CL: https://go-review.googlesource.com/c/go/+/127718

gopherbot pushed a commit that referenced this issue Aug 13, 2018

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: b230502
GitHub-Pull-Request: #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>
@johanbrandhorst

This comment has been minimized.

Member

johanbrandhorst commented Aug 13, 2018

With #26784 merged we now have mode and credentials covered, so this issue is now about adding support for configuring redirect. Any more thoughts on how we might accomplish that?

@bradfitz

This comment has been minimized.

Member

bradfitz commented Aug 14, 2018

I don't see an obvious mapping from anything already in net/http to the Fetch redirect knob. So probably we should just do the same thing as the other two.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment