summaryrefslogtreecommitdiff
path: root/psr7/request.go
blob: 41fbb4bfdd6bc8ac03010856e8343b4682aabcd6 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package psr7

import (
	"net/http"
	"fmt"
	"encoding/json"
	"github.com/spiral/roadrunner"
	"github.com/sirupsen/logrus"
	"strings"
	"io/ioutil"
)

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"`
	ParsedBody bool              `json:"parsedBody"`

	// buffers
	postData postData
	body     []byte
}

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")
		}
		req.ParsedBody = true
	} else {
		req.body, _ = ioutil.ReadAll(r.Body)
	}

	return req, nil
}

func (r *Request) Payload() *roadrunner.Payload {
	ctx, err := json.Marshal(r)
	if err != nil {
		panic(err) //todo: change it
	}

	var body []byte
	if r.ParsedBody {
		// todo: non parseble payloads
		body, err = json.Marshal(r.postData)
		if err != nil {
			panic(err) //todo: change it
		}
	} else {
		body = r.body
	}

	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 {
	if r.Method != "POST" && r.Method != "PUT" && r.Method != "PATCH" {
		return false
	}

	contentType := r.Headers.Get("content-type")

	if strings.Contains(contentType, "multipart/form-data") {
		return true
	}

	if contentType == "application/x-www-form-urlencoded" {
		return true
	}

	return false
}

// 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
}