-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add ConvertNetHttpRequestToFastHttpRequest adaptor function #2104
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
5efed7c
5edfdd3
896ff56
e66afbb
63f8c00
f3d31bc
aa6e422
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |||
| import ( | ||||
| "bytes" | ||||
| "io" | ||||
| "math" | ||||
| "net" | ||||
| "net/http" | ||||
| "net/url" | ||||
| "strings" | ||||
|
|
@@ -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) { | ||||
aaydin-tr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
aaydin-tr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||
| 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()) | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| } | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
|
||||
| ctx.Request.Header.SetProtocol(r.Proto) | ||||
| ctx.Request.SetHost(r.Host) | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
|
|
||||
| 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) | ||||
| } | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| } | ||||
| } | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
|
||||
| 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) | ||||
| } | ||||
| } | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||
|
|
||||
| func parseRemoteAddr(addr string) net.Addr { | ||||
| if tcpAddr, err := net.ResolveTCPAddr("tcp", addr); err == nil { | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: Line 479 in 3471acf
|
||||
| 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, "[]") | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| return &net.TCPAddr{IP: net.ParseIP(host)} | ||||
aaydin-tr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
| } | ||||
Uh oh!
There was an error while loading. Please reload this page.