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

c.Request.RequestURI()方法在http和https场景下的返回值形式不一致 #1241

Open
Jeremyly opened this issue Nov 28, 2024 · 0 comments

Comments

@Jeremyly
Copy link

Describe the bug

假设请求完整路径是http://127.0.0.1/ping时,c.Request.RequestURI()方法的值是/ping
但是,当启用TLS后,完整路径变成https://127.0.0.1/ping时,c.Request.RequestURI()方法的值是https://127.0.0.1/ping

显然,对于http和https两种监听模式下, 该方法返回的值的形式不统一,我认为这是一个bug

To Reproduce
复现代码:

package main

import (
	"context"
	"crypto/tls"
	"fmt"
	"os"
	"time"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
	"github.com/hertz-contrib/http2/config"
	"github.com/hertz-contrib/http2/factory"
	"golang.org/x/net/http2"
)

func main() {
	// set TLS server
	// default is standard.NewTransporter
	h1 := CreateHttpsServer()

	h1.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
		fmt.Println("TLS:" + string(c.Request.RequestURI()))
		c.String(consts.StatusOK, "TLS test\n")
	})

	go h1.Spin()

	h2 := CreateHttpServer()

	h2.GET("/ping", func(ctx context.Context, c *app.RequestContext) {
		fmt.Println("HTTP:" + string(c.Request.RequestURI()))
		c.String(consts.StatusOK, "http test\n")
	})

	h2.Spin()

}

// 创建https服务
func CreateHttpsServer() *server.Hertz {
	// 读取文件内容
	certPEM, err := os.ReadFile("./certificate.crt")
	if err != nil {
		panic(err)
	}

	keyPEM, err := os.ReadFile("./private.key")
	if err != nil {
		panic(err)
	}

	cert, err := tls.X509KeyPair(certPEM, keyPEM)
	if err != nil {
		fmt.Println(err.Error())
	}
	cfg := &tls.Config{
		// add certificate
		Certificates: []tls.Certificate{cert},
		MaxVersion:   tls.VersionTLS13,
		// cipher suites supported
		CipherSuites: []uint16{
			tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
			tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
			tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
		},
		// set application protocol http2
		NextProtos: []string{http2.NextProtoTLS},
	}
	h := server.New(server.WithHostPorts(":https"), server.WithALPN(true), server.WithTLS(cfg))

	// register http2 server factory
	h.AddProtocol("h2", factory.NewServerFactory(
		config.WithReadTimeout(time.Minute),
		config.WithDisableKeepAlive(false)))
	cfg.NextProtos = append(cfg.NextProtos, "h2")

	return h

}

// 创建http服务
func CreateHttpServer() *server.Hertz {
	return server.Default(server.WithHostPorts(":http"))
}

Expected behavior
期望该方法返回的值的形式保持一致

Hertz version:

v0.9.3

Environment:

go 1.22

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

No branches or pull requests

1 participant