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

x/net/http2/h2c: h2c handler doesn't drain initial request body #43654

Closed
NadeemAfana opened this issue Jan 12, 2021 · 4 comments
Closed

x/net/http2/h2c: h2c handler doesn't drain initial request body #43654

NadeemAfana opened this issue Jan 12, 2021 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@NadeemAfana
Copy link

NadeemAfana commented Jan 12, 2021

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

$ go version
go version go1.15.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

linux/amd64

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/nadeem/.cache/go-build"
GOENV="/home/nadeem/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/nadeem/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/nadeem/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/mnt/c/Go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/mnt/c/Go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/testing/go.mod"
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-build541459271=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I am trying to use an http2 sever without TLS using h2c. I am trying to read the request body and return it as a response.

I am trying the example from https://pkg.go.dev/golang.org/x/net/http2/h2c after a slight modification:

handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		b,err := ioutil.ReadAll(r.Body)
		if err != nil {
			w.WriteHeader(400)
			w.Write([]byte(err.Error()))
			return
		}
		w.Write(b)
	})

	h2s := &http2.Server{
		// ...
	}
	h1s := &http.Server{
		Addr:    ":3000",
		Handler: h2c.NewHandler(handler, h2s),
	}
curl -X POST --http2  --data "Hello" http://localhost:3000/

What did you expect to see?

I expected to see the word "Hello".

What did you see instead?

The request hanging with a console error "http: response.Write on hijacked connection from main.main.func1 (main.go:21)"

Not sure if I am doing something wrong or if I misunderstood the process.

@seankhliao
Copy link
Member

This happens when the h2c handler attempts to perform an upgrade from http1.1 to h2c.
https://github.com/golang/net/blob/6772e930b67b/http2/h2c/h2c.go#L147 drains the client preface, but not the initial request body that comes before it, so it ends up not draining enough / not matching the expected preface and falling back to http1.1 even though the connection has already been hijacked

@seankhliao seankhliao changed the title x/net/http2: h2c erroring when passing a request body with http2 x/net/http2/h2c h2c handler doesn't drain iinitial request body Jan 12, 2021
@seankhliao seankhliao changed the title x/net/http2/h2c h2c handler doesn't drain iinitial request body x/net/http2/h2c h2c handler doesn't drain initial request body Jan 12, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jan 12, 2021
@NadeemAfana
Copy link
Author

NadeemAfana commented Jan 12, 2021

This happens when the h2c handler attempts to perform an upgrade from http1.1 to h2c.
https://github.com/golang/net/blob/6772e930b67b/http2/h2c/h2c.go#L147 drains the client preface, but not the initial request body that comes before it, so it ends up not draining enough / not matching the expected preface and falling back to http1.1 even though the connection has already been hijacked

Thanks.
Is this the expected behavior or a bug? How do I get around that?

@seankhliao seankhliao changed the title x/net/http2/h2c h2c handler doesn't drain initial request body x/net/http2/h2c: h2c handler doesn't drain initial request body Jan 12, 2021
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 13, 2021
@cagedmantis
Copy link
Contributor

/cc @bradfitz @tombergan @empijei

@seankhliao
Copy link
Member

duplicate of #38064 #41445

@golang golang locked and limited conversation to collaborators Jan 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants