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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
package http
import (
"encoding/json"
"fmt"
"github.com/spiral/roadrunner"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
const (
defaultMaxMemory = 32 << 20 // 32 MB
contentNone = iota + 900
contentStream
contentMultipart
contentFormData
)
// Request maps net/http requests to PSR7 compatible structure and managed state of temporary uploaded files.
type Request struct {
// Protocol includes HTTP protocol version.
Protocol string `json:"protocol"`
// Method contains name of HTTP method used for the request.
Method string `json:"method"`
// Uri contains full request Uri with scheme and query.
Uri string `json:"uri"`
// Headers contains list of request headers.
Headers http.Header `json:"headers"`
// Cookies contains list of request cookies.
Cookies map[string]string `json:"cookies"`
// RawQuery contains non parsed query string (to be parsed on php end).
RawQuery string `json:"rawQuery"`
// Parsed indicates that request body has been parsed on RR end.
Parsed bool `json:"parsed"`
// Uploads contains list of uploaded files, their names, sized and associations with temporary files.
Uploads *Uploads `json:"uploads"`
// request body can be parsedData or []byte
body interface{}
}
// NewRequest creates new PSR7 compatible request using net/http request.
func NewRequest(r *http.Request, cfg *UploadsConfig) (req *Request, err error) {
req = &Request{
Protocol: r.Proto,
Method: r.Method,
Uri: uri(r),
Headers: r.Header,
Cookies: make(map[string]string),
RawQuery: r.URL.RawQuery,
}
for _, c := range r.Cookies() {
if v, err := url.QueryUnescape(c.Value); err == nil {
req.Cookies[c.Name] = v
}
}
switch req.contentType() {
case contentNone:
return req, nil
case contentStream:
req.body, err = ioutil.ReadAll(r.Body)
return req, err
case contentMultipart:
if err = r.ParseMultipartForm(defaultMaxMemory); err != nil {
return nil, err
}
req.Uploads = parseUploads(r, cfg)
fallthrough
case contentFormData:
if err = r.ParseForm(); err != nil {
return nil, err
}
req.body = parseData(r)
}
req.Parsed = true
return req, nil
}
// Open moves all uploaded files to temporary directory so it can be given to php later.
func (r *Request) Open() error {
if r.Uploads == nil {
return nil
}
return r.Uploads.Open()
}
// Close clears all temp file uploads
func (r *Request) Close() {
if r.Uploads == nil {
return
}
r.Uploads.Clear()
}
// Payload request marshaled RoadRunner payload based on PSR7 data. Default encode method is JSON. Make sure to open
// files prior to calling this method.
func (r *Request) Payload() (p *roadrunner.Payload, err error) {
p = &roadrunner.Payload{}
if p.Context, err = json.Marshal(r); err != nil {
return nil, err
}
if r.Parsed {
if p.Body, err = json.Marshal(r.body); err != nil {
return nil, err
}
} else if r.body != nil {
p.Body = r.body.([]byte)
}
return p, nil
}
// contentType returns the payload content type.
func (r *Request) contentType() int {
if r.Method != "POST" && r.Method != "PUT" && r.Method != "PATCH" {
return contentNone
}
ct := r.Headers.Get("content-type")
if ct == "application/x-www-form-urlencoded" {
return contentFormData
}
if strings.Contains(ct, "multipart/form-data") {
return contentMultipart
}
return contentStream
}
// uri fetches full uri from request in a form of string (including https scheme if TLS connection is enabled).
func uri(r *http.Request) string {
if r.TLS != nil {
return fmt.Sprintf("https://%s%s", r.Host, r.URL.String())
}
return fmt.Sprintf("http://%s%s", r.Host, r.URL.String())
}
|