Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
71 changes: 71 additions & 0 deletions fasthttpadaptor/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import (
"bytes"
"io"
"math"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -68,3 +70,72 @@

return nil
}

// ConvertNetHttpRequestToFastHttpRequest converts an http.Request to a fasthttp.RequestCtx.
//
// The caller is responsible for the lifecycle of the fasthttp.RequestCtx and the
// underlying fasthttp.Request. The ctx (and its Request) must only be used for
// the duration that fasthttp considers it valid (typically within a handler),
// and must not be accessed after the handler has returned.
//
// The request body is not copied. If r.Body is non-nil, it is passed directly to
// ctx.Request via SetBodyStream. This means:
// - r.Body must remain readable for as long as ctx may need to read it.
// - r.Body should not be read from, written to, or closed by the caller until
// fasthttp is done with ctx.
// - The same r.Body must not be reused concurrently in other goroutines while
// it is attached to ctx.Request.
//
// After calling this function, you should treat r.Body as effectively owned by
// ctx.Request for the lifetime of that context.
func ConvertNetHttpRequestToFastHttpRequest(r *http.Request, ctx *fasthttp.RequestCtx) {

Check failure on line 91 in fasthttpadaptor/request.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: func ConvertNetHttpRequestToFastHttpRequest should be ConvertNetHTTPRequestToFastHTTPRequest (staticcheck)
ctx.Request.Header.SetMethod(r.Method)

if r.RequestURI != "" {
ctx.Request.SetRequestURI(r.RequestURI)
} else if r.URL != nil {
ctx.Request.SetRequestURI(r.URL.RequestURI())
}

ctx.Request.Header.SetProtocol(r.Proto)
ctx.Request.SetHost(r.Host)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r.Host can be empty in which case it should be r.URL.Host.


for k, values := range r.Header {
for i, v := range values {
if i == 0 {
ctx.Request.Header.Set(k, v)
} else {
ctx.Request.Header.Add(k, v)
}
}
}

if r.Body != nil {
contentLength := int(r.ContentLength)
if r.ContentLength >= int64(math.MaxInt) {
contentLength = -1
}

ctx.Request.SetBodyStream(r.Body, contentLength)
}

if r.RemoteAddr != "" {
addr := parseRemoteAddr(r.RemoteAddr)
ctx.SetRemoteAddr(addr)
}
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are missing some properties that aren't being copied right now. For example r.TLS and r.Close, r.TransferEncoding, r.Trailer, and r.URL.Scheme which are all used by net/http.


func parseRemoteAddr(addr string) net.Addr {
if tcpAddr, err := net.ResolveTCPAddr("tcp", addr); err == nil {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no timeout on this and it could potentially hang. We have this code here, I'm not sure if it should somehow be reused since it's not exposed:

func resolveTCPAddrs(addr string, dualStack bool, resolver Resolver, deadline time.Time) ([]net.TCPAddr, error) {

return tcpAddr
}

if _, _, err := net.SplitHostPort(addr); err != nil {
if tcpAddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, "0")); err == nil {
return tcpAddr
}
}

host := strings.Trim(addr, "[]")
return &net.TCPAddr{IP: net.ParseIP(host)}
}
Loading
Loading