From be591d3221e9f7227ec0e1a0fea21f24e15c8210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Wed, 14 Sep 2022 01:20:12 +0300 Subject: [PATCH 1/5] add byte to string unsafe conversion to fasthttpadaptor ConvertRequest method() --- fasthttpadaptor/request.go | 15 ++++++++++----- fasthttpadaptor/request_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 fasthttpadaptor/request_test.go diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go index 7a49bfd079..d0831a84a6 100644 --- a/fasthttpadaptor/request.go +++ b/fasthttpadaptor/request.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "net/http" "net/url" + "unsafe" "github.com/valyala/fasthttp" ) @@ -13,20 +14,20 @@ import ( // forServer should be set to true when the http.Request is going to passed to a http.Handler. func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) error { body := ctx.PostBody() - strRequestURI := string(ctx.RequestURI()) + strRequestURI := b2s(ctx.RequestURI()) rURL, err := url.ParseRequestURI(strRequestURI) if err != nil { return err } - r.Method = string(ctx.Method()) + r.Method = b2s(ctx.Method()) r.Proto = "HTTP/1.1" r.ProtoMajor = 1 r.ProtoMinor = 1 r.ContentLength = int64(len(body)) r.RemoteAddr = ctx.RemoteAddr().String() - r.Host = string(ctx.Host()) + r.Host = b2s(ctx.Host()) r.TLS = ctx.TLSConnectionState() r.Body = ioutil.NopCloser(bytes.NewReader(body)) r.URL = rURL @@ -44,8 +45,8 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e } ctx.Request.Header.VisitAll(func(k, v []byte) { - sk := string(k) - sv := string(v) + sk := b2s(k) + sv := b2s(v) switch sk { case "Transfer-Encoding": @@ -57,3 +58,7 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e return nil } + +func b2s(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} diff --git a/fasthttpadaptor/request_test.go b/fasthttpadaptor/request_test.go new file mode 100644 index 0000000000..243f925194 --- /dev/null +++ b/fasthttpadaptor/request_test.go @@ -0,0 +1,26 @@ +package fasthttpadaptor + +import ( + "github.com/valyala/fasthttp" + "net/http" + "testing" +) + +func BenchmarkConvertRequest(b *testing.B) { + var httpReq http.Request + + ctx := &fasthttp.RequestCtx{ + Request: fasthttp.Request{ + Header: fasthttp.RequestHeader{}, + UseHostHeader: false, + }, + } + ctx.Request.Header.SetMethod("GET") + ctx.Request.Header.Set("x", "test") + ctx.Request.SetRequestURI("/test") + ctx.Request.SetHost("test") + + for i := 0; i < b.N; i++ { + ConvertRequest(ctx, &httpReq, true) + } +} From aa1d0d20cb9923c6ee89239d7fec88f592e15eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Wed, 14 Sep 2022 09:36:33 +0300 Subject: [PATCH 2/5] add nosec comment line --- fasthttpadaptor/request.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go index d0831a84a6..707da549c4 100644 --- a/fasthttpadaptor/request.go +++ b/fasthttpadaptor/request.go @@ -2,12 +2,11 @@ package fasthttpadaptor import ( "bytes" + "github.com/valyala/fasthttp" "io/ioutil" "net/http" "net/url" "unsafe" - - "github.com/valyala/fasthttp" ) // ConvertRequest convert a fasthttp.Request to an http.Request @@ -60,5 +59,6 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e } func b2s(b []byte) string { + /* #nosec G103 */ return *(*string)(unsafe.Pointer(&b)) } From 135fc926411e46aa88233b51dda8d327e9ef3e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Wed, 14 Sep 2022 21:16:24 +0300 Subject: [PATCH 3/5] Update fasthttpadaptor/request.go Co-authored-by: Erik Dubbelboer --- fasthttpadaptor/request.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go index 707da549c4..2a1fa63ba3 100644 --- a/fasthttpadaptor/request.go +++ b/fasthttpadaptor/request.go @@ -11,6 +11,9 @@ import ( // ConvertRequest convert a fasthttp.Request to an http.Request // forServer should be set to true when the http.Request is going to passed to a http.Handler. +// +// The http.Request must not be used after the fasthttp handler has returned! +// Memory in use by the http.Request will be reused after your handler has returned! func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) error { body := ctx.PostBody() strRequestURI := b2s(ctx.RequestURI()) From 059e6be00dce9a21a054c8a78c66da429eff9079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Wed, 14 Sep 2022 21:18:43 +0300 Subject: [PATCH 4/5] move unsafe package import next to std packages --- fasthttpadaptor/request.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go index 2a1fa63ba3..9631ec7151 100644 --- a/fasthttpadaptor/request.go +++ b/fasthttpadaptor/request.go @@ -2,11 +2,12 @@ package fasthttpadaptor import ( "bytes" - "github.com/valyala/fasthttp" "io/ioutil" "net/http" "net/url" "unsafe" + + "github.com/valyala/fasthttp" ) // ConvertRequest convert a fasthttp.Request to an http.Request From 1884cd2635756f0bd0f240f0e04f5299348c58df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20Savc=C4=B1?= Date: Thu, 15 Sep 2022 22:56:35 +0300 Subject: [PATCH 5/5] fix lint error in test --- fasthttpadaptor/request_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fasthttpadaptor/request_test.go b/fasthttpadaptor/request_test.go index 243f925194..3b6ba54ca7 100644 --- a/fasthttpadaptor/request_test.go +++ b/fasthttpadaptor/request_test.go @@ -17,10 +17,12 @@ func BenchmarkConvertRequest(b *testing.B) { } ctx.Request.Header.SetMethod("GET") ctx.Request.Header.Set("x", "test") + ctx.Request.Header.Set("y", "test") ctx.Request.SetRequestURI("/test") ctx.Request.SetHost("test") + b.ResetTimer() for i := 0; i < b.N; i++ { - ConvertRequest(ctx, &httpReq, true) + _ = ConvertRequest(ctx, &httpReq, true) } }