mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-16 16:17:38 +03:00
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package fasthttpadaptor
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"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.
|
|
func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) error {
|
|
body := ctx.PostBody()
|
|
strRequestURI := string(ctx.RequestURI())
|
|
|
|
rURL, err := url.ParseRequestURI(strRequestURI)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
r.Method = string(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.TLS = ctx.TLSConnectionState()
|
|
r.Body = ioutil.NopCloser(bytes.NewReader(body))
|
|
r.URL = rURL
|
|
|
|
if forServer {
|
|
r.RequestURI = strRequestURI
|
|
}
|
|
|
|
if r.Header == nil {
|
|
r.Header = make(http.Header)
|
|
} else if len(r.Header) > 0 {
|
|
for k := range r.Header {
|
|
delete(r.Header, k)
|
|
}
|
|
}
|
|
|
|
ctx.Request.Header.VisitAll(func(k, v []byte) {
|
|
sk := string(k)
|
|
sv := string(v)
|
|
|
|
switch sk {
|
|
case "Transfer-Encoding":
|
|
r.TransferEncoding = append(r.TransferEncoding, sv)
|
|
default:
|
|
r.Header.Set(sk, sv)
|
|
}
|
|
})
|
|
|
|
return nil
|
|
}
|