diff options
author | Wolfy-J <[email protected]> | 2018-05-30 16:22:01 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-05-30 16:22:01 +0300 |
commit | ec2af29c17402145547699e719902d0f3f2ec8ec (patch) | |
tree | 2cf8f92cc9144f644aae4f727d808c150cd005b0 /server/psr7/request.go | |
parent | a1f63b412e0bcba76f9f3e4e6235d3fa49be717a (diff) |
more PSR7 support
Diffstat (limited to 'server/psr7/request.go')
-rw-r--r-- | server/psr7/request.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/server/psr7/request.go b/server/psr7/request.go new file mode 100644 index 00000000..f8dedd8f --- /dev/null +++ b/server/psr7/request.go @@ -0,0 +1,101 @@ +package psr7 + +import ( + "net/http" + "fmt" + "encoding/json" + "github.com/spiral/roadrunner" + "github.com/sirupsen/logrus" +) + +type Request struct { + Protocol string `json:"protocol"` + Uri string `json:"uri"` + Method string `json:"method"` + Headers http.Header `json:"headers"` + Cookies map[string]string `json:"cookies"` + RawQuery string `json:"rawQuery"` + Uploads fileData `json:"fileUploads"` + + // buffers + postData postData +} + +func ParseRequest(r *http.Request) (req *Request, err error) { + req = &Request{ + Protocol: r.Proto, + Uri: fmt.Sprintf("%s%s", r.Host, r.URL.String()), + Method: r.Method, + Headers: r.Header, + Cookies: make(map[string]string), + RawQuery: r.URL.RawQuery, + } + + for _, c := range r.Cookies() { + req.Cookies[c.Name] = c.Value + } + + if req.HasBody() { + r.ParseMultipartForm(32 << 20) + + if req.postData, err = parseData(r); err != nil { + return nil, err + } + + if req.Uploads, err = parseFiles(r); err != nil { + return nil, err + } + + if req.Uploads != nil { + logrus.Debug("opening files") + } + } + + return req, nil +} + +func (r *Request) Payload() *roadrunner.Payload { + ctx, err := json.Marshal(r) + if err != nil { + panic(err) //todo: change it + } + + // todo: non parseble payloads + body, err := json.Marshal(r.postData) + if err != nil { + panic(err) //todo: change it + } + + return &roadrunner.Payload{Context: ctx, Body: body} +} + +func (r *Request) Close() { + if r.Uploads != nil { + + } +} + +// HasBody returns true if request might include POST data or file uploads. +func (r *Request) HasBody() bool { + return r.Method == "POST" || r.Method == "PUT" || r.Method == "PATCH" +} + +// parse incoming data request into JSON (including multipart form data) +func parseData(r *http.Request) (postData, error) { + data := make(postData) + for k, v := range r.MultipartForm.Value { + data.push(k, v) + } + + return data, nil +} + +// parse incoming data request into JSON (including multipart form data) +func parseFiles(r *http.Request) (fileData, error) { + data := make(fileData) + for k, v := range r.MultipartForm.File { + data.push(k, v) + } + + return data, nil +} |