feature: add ConvertRequest func (#1024)

* feature: add ConvertRequest func

To easy method to convert ctx.RequestCtx to http.Request

* chore: minor changes by code review #1024
This commit is contained in:
Nícolas Barbosa
2021-05-17 04:20:10 -03:00
committed by GitHub
parent 5898006f5b
commit 04cde74c41
2 changed files with 49 additions and 47 deletions
+1 -47
View File
@@ -3,9 +3,7 @@
package fasthttpadaptor
import (
"io"
"net/http"
"net/url"
"github.com/valyala/fasthttp"
)
@@ -49,37 +47,11 @@ func NewFastHTTPHandlerFunc(h http.HandlerFunc) fasthttp.RequestHandler {
func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
var r http.Request
body := ctx.PostBody()
r.Method = string(ctx.Method())
r.Proto = "HTTP/1.1"
r.ProtoMajor = 1
r.ProtoMinor = 1
r.RequestURI = string(ctx.RequestURI())
r.ContentLength = int64(len(body))
r.Host = string(ctx.Host())
r.RemoteAddr = ctx.RemoteAddr().String()
hdr := make(http.Header)
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:
hdr.Add(sk, sv)
}
})
r.Header = hdr
r.Body = &netHTTPBody{body}
rURL, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
if err := ConvertRequest(ctx, &r, true); err != nil {
ctx.Logger().Printf("cannot parse requestURI %q: %s", r.RequestURI, err)
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}
r.URL = rURL
var w netHTTPResponseWriter
h.ServeHTTP(&w, r.WithContext(ctx))
@@ -109,24 +81,6 @@ func NewFastHTTPHandler(h http.Handler) fasthttp.RequestHandler {
}
}
type netHTTPBody struct {
b []byte
}
func (r *netHTTPBody) Read(p []byte) (int, error) {
if len(r.b) == 0 {
return 0, io.EOF
}
n := copy(p, r.b)
r.b = r.b[n:]
return n, nil
}
func (r *netHTTPBody) Close() error {
r.b = r.b[:0]
return nil
}
type netHTTPResponseWriter struct {
statusCode int
h http.Header
+48
View File
@@ -0,0 +1,48 @@
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 {
rURL, err := url.ParseRequestURI(string(ctx.RequestURI()))
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(ctx.PostBody()))
r.RemoteAddr = ctx.RemoteAddr().String()
r.Host = string(ctx.Host())
if forServer {
r.RequestURI = string(ctx.RequestURI())
}
hdr := make(http.Header)
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:
hdr.Set(sk, sv)
}
})
r.Header = hdr
r.Body = ioutil.NopCloser(bytes.NewReader(ctx.PostBody()))
r.URL = rURL
return nil
}