Skip to content
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/httputil: ReverseProxy does not pass previous headers when upgrade connection #29407

Closed
juliens opened this issue Dec 24, 2018 · 1 comment

Comments

@juliens
Copy link
Contributor

juliens commented Dec 24, 2018

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

go version go1.12beta1 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/juliens/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/juliens/dev/go"
GOPROXY=""
GORACE=""
GOROOT="/home/juliens/go-current"
GOTMPDIR=""
GOTOOLDIR="/home/juliens/go-current/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build626877303=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Use the httputil.ReverseProxy with switch protocol , and set a response header in a middleware before the reverse proxy.

func TestName(t *testing.T) {
	backend := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		if req.Header.Get("Connection") == "Upgrade" {
			rw.Header().Set("Connection", "Upgrade")
			rw.Header().Set("Upgrade", "websocket")

			rw.WriteHeader(http.StatusSwitchingProtocols)
		}
	}))
	defer backend.Close()

	backendURL, _ := url.Parse(backend.URL)
	proxy := httputil.NewSingleHostReverseProxy(backendURL)

	middleware := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("X-Header", "X-Value")
		proxy.ServeHTTP(rw, req)
	})

	frontendProxy := httptest.NewServer(middleware)
	defer frontendProxy.Close()

	req, _ := http.NewRequest("GET", frontendProxy.URL, nil)
	req.Header.Set("Connection", "Upgrade")
	req.Header.Set("Upgrade", "websocket")

	c := frontendProxy.Client()
	res, err := c.Do(req)
	if err != nil {
		t.Fatal(err)
	}
	if res.StatusCode != 101 {
		t.Fatalf("status = %v; want 101", res.Status)
	}

	got := res.Header.Get("X-Header")
	want := "X-Value"
	if got != want {
		t.Errorf("Header(XHeader) = %q; want %q", got, want)
	}
}

What did you expect to see?

Previous headers in response.

What did you see instead?

No previous headers in response.

@gopherbot
Copy link

Change https://golang.org/cl/155741 mentions this issue: net/http/httputil: fix missing previous headers in response when switching protocol in ReverseProxy

@golang golang locked and limited conversation to collaborators Jan 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants