Skip to content

x/net/http2/h2c: upgrade fails with request body #38064

@suiriass

Description

@suiriass

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

$ go version
1.14.1

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\simms.shi\AppData\Local\go-build
set GOENV=C:\Users\simms.shi\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS= -mod=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=code.aliyun.com
set GONOSUMDB=code.aliyun.com
set GOOS=windows
set GOPATH=C:\Users\simms.shi\go
set GOPRIVATE=code.aliyun.com
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Go
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\gowork\fabio-esb-agent\fabio\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\simms.shi\AppData\Local\Temp\go-build693576242=/tmp/go-build -gno-record-gcc-switc
hes

What did you do?

// ex: https://godoc.org/golang.org/x/net/http2/h2c
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a

func HTTP2CServer(l config.Listen, h http.Handler, cfg *tls.Config) *http.Server {
	return &http.Server{
		Addr: l.Addr,
		//Handler:      h,
		Handler:      h2c.NewHandler(h, &http2.Server{}),
		ReadTimeout:  l.ReadTimeout,
		WriteTimeout: l.WriteTimeout,
		TLSConfig:    cfg,
	}
}

I created a server(h2c) as per the above,

java11 code

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;


public class MainH2 {
   public static void main(String[] args) throws IOException, InterruptedException {
   	//1.set connect timeout
   	HttpClient client = HttpClient.newBuilder()
   		.version(HttpClient.Version.HTTP_2)
   		.connectTimeout(Duration.ofMillis(50000))
   		.followRedirects(HttpClient.Redirect.NORMAL)
   		.build();

   	//2.set read timeout
   	HttpRequest request = HttpRequest.newBuilder()
//			.GET()
   		.POST(HttpRequest.BodyPublishers.ofString("{\"yearMonth\":\"2019-12\"}", StandardCharsets.UTF_8))
   		.uri(URI.create("http://127.0.0.1:9999/xxxx/month/amount/pie"))
   		.header("Content-Type", "application/json")
   		.timeout(Duration.ofMillis(50009))
   		.build();

   	HttpResponse<String> response =
   		client.send(request, HttpResponse.BodyHandlers.ofString());
   	System.out.println(response.version());
   	System.out.println(response.statusCode());
   	System.out.println(response.body());
   }

}

What did you expect to see?

I tried using the latest version of net/http2/h2c and The problem is the same,
H2c server is expected to work properly in the case of the post method.
thank you very much.

What did you see instead?

This client works correctly if it accesses Java's undertow server, not work if it accesses go's h2c server,
Error message source call chain:
(ServeHTTP>h2cUpgrade>drainClientPreface)
golang.org/x/net/http2/h2c/h2c.go
20200326085403
[return fmt.Errorf("Client never sent: %s", http2.ClientPreface)], but The Get method is correct

Analyze TCP messages and discover that the location of http2.ClientPreface(PRI * HTTP/2.0...) may not conform to the go's rules
{3934C354-98C2-4D0E-8035-ACA9E641C7E5}_20200325191526

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions