aboutsummaryrefslogtreecommitdiff
path: root/fasthttpadaptor
diff options
context:
space:
mode:
authorGravatar Nícolas Barbosa <ndevbarbosa@gmail.com> 2021-05-17 04:20:10 -0300
committerGravatar GitHub <noreply@github.com> 2021-05-17 09:20:10 +0200
commit04cde74c419c81185c47e28683da50c8a152d01d (patch)
tree4843b7ab5a5fd0e8e67eef4e2861ef86f2b4f299 /fasthttpadaptor
parentUpgrade dependencies and tidy (#1029) (diff)
downloadfasthttp-04cde74c419c81185c47e28683da50c8a152d01d.tar.gz
fasthttp-04cde74c419c81185c47e28683da50c8a152d01d.tar.bz2
fasthttp-04cde74c419c81185c47e28683da50c8a152d01d.zip
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
Diffstat (limited to 'fasthttpadaptor')
-rw-r--r--fasthttpadaptor/adaptor.go48
-rw-r--r--fasthttpadaptor/request.go48
2 files changed, 49 insertions, 47 deletions
diff --git a/fasthttpadaptor/adaptor.go b/fasthttpadaptor/adaptor.go
index e7a4ffe..51b9c7a 100644
--- a/fasthttpadaptor/adaptor.go
+++ b/fasthttpadaptor/adaptor.go
@@ -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
diff --git a/fasthttpadaptor/request.go b/fasthttpadaptor/request.go
new file mode 100644
index 0000000..db1296b
--- /dev/null
+++ b/fasthttpadaptor/request.go
@@ -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
+}