forked from valyala/fasthttp
/
request.go
64 lines (53 loc) · 1.28 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fasthttpadaptor
import (
"bytes"
"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.
func ConvertRequest(ctx *fasthttp.RequestCtx, r *http.Request, forServer bool) error {
body := ctx.PostBody()
strRequestURI := b2s(ctx.RequestURI())
rURL, err := url.ParseRequestURI(strRequestURI)
if err != nil {
return err
}
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 = b2s(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 := b2s(k)
sv := b2s(v)
switch sk {
case "Transfer-Encoding":
r.TransferEncoding = append(r.TransferEncoding, sv)
default:
r.Header.Set(sk, sv)
}
})
return nil
}
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}