Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

byte to string unsafe conversion in fasthttpadaptor ConvertRequest method #1375

Merged
merged 5 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions fasthttpadaptor/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,32 @@ import (
"io/ioutil"
"net/http"
"net/url"
"unsafe"

"github.com/valyala/fasthttp"
)

// 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.
mstrYoda marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 := 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
Expand All @@ -44,8 +48,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":
Expand All @@ -57,3 +61,8 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e

return nil
}

func b2s(b []byte) string {
mstrYoda marked this conversation as resolved.
Show resolved Hide resolved
/* #nosec G103 */
return *(*string)(unsafe.Pointer(&b))
}
26 changes: 26 additions & 0 deletions fasthttpadaptor/request_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}