-
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 1 commit
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,8 +3,10 @@ package fasthttpadaptor | |
| import ( | ||
| "bytes" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/valyala/fasthttp" | ||
|
|
@@ -68,3 +70,62 @@ func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) e | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ConvertNetHttpToFastHttp converts an http.Request to a fasthttp.RequestCtx. | ||
| // The caller is responsible for the lifecycle of the fasthttp.RequestCtx. | ||
|
aaydin-tr marked this conversation as resolved.
Outdated
|
||
| func ConvertNetHttpRequestToFastHttpRequest(r *http.Request, ctx *fasthttp.RequestCtx) error { | ||
| 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.
|
||
| } | ||
|
aaydin-tr marked this conversation as 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.
|
||
| } | ||
| } | ||
|
aaydin-tr marked this conversation as resolved.
|
||
|
|
||
| if r.Body != nil { | ||
| body, err := io.ReadAll(r.Body) | ||
|
aaydin-tr marked this conversation as resolved.
Outdated
|
||
| if err != nil { | ||
| return err | ||
| } | ||
| ctx.Request.SetBody(body) | ||
| } | ||
|
|
||
| if r.RemoteAddr != "" { | ||
| addr := parseRemoteAddr(r.RemoteAddr) | ||
| ctx.SetRemoteAddr(addr) | ||
| } | ||
|
|
||
|
aaydin-tr marked this conversation as resolved.
Outdated
|
||
| return 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. You are missing some properties that aren't being copied right now. For example |
||
|
|
||
| func parseRemoteAddr(addr string) net.Addr { | ||
| host, port, err := net.SplitHostPort(addr) | ||
| if err != nil { | ||
| return &net.TCPAddr{IP: net.ParseIP(addr)} | ||
| } | ||
| return &net.TCPAddr{ | ||
| IP: net.ParseIP(host), | ||
| Port: parsePort(port), | ||
| } | ||
|
aaydin-tr marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| func parsePort(port string) int { | ||
|
aaydin-tr marked this conversation as resolved.
Outdated
|
||
| p, err := strconv.Atoi(port) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| return p | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.