-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
181 lines (143 loc) · 3.87 KB
/
request.go
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package request
import (
"encoding/json"
"io"
"io/ioutil"
"mime"
"net/http"
"strconv"
"strings"
"github.com/gorilla/mux"
"github.com/macinnir/dvc/core/lib/utils/types"
)
// AuthHeaderKey is the name of the Authorization header
const AuthHeaderKey = "Authorization"
// AuthHeaderValuePrefix is the start of the Authorization string (in the authorization header) used to authorize the request
const AuthHeaderValuePrefix = "Bearer "
// Request is a request object
type Request struct {
Method string
Path string
Headers map[string]string
Params map[string]string
Body string
ControllerMethod string
ResponseCode int64
Error string
UserID int64
User types.IUserContainer
RootRequest *http.Request `json:"-"`
ActionType int64
BodyReadCloser io.ReadCloser
}
func isFileUpload(r *http.Request) bool {
v := r.Header.Get("Content-Type")
if v == "" {
return false
}
d, _, _ := mime.ParseMediaType(v)
return d == "multipart/form-data"
}
// NewRequest is a factory method for a request
func NewRequest(r *http.Request) *Request {
req := &Request{
Method: r.Method,
Path: r.URL.RequestURI(),
Headers: map[string]string{},
Params: map[string]string{},
Body: "",
RootRequest: r,
ActionType: UserLogActionTypeAPI.Int64(),
BodyReadCloser: r.Body,
}
// Headers
for name := range r.Header {
if len(r.Header[name]) > 0 {
req.Headers[name] = r.Header[name][0]
}
}
// URL Params
req.Params = mux.Vars(r)
if !isFileUpload(r) {
bodyBytes, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
req.Body = string(bodyBytes)
}
return req
}
// ToJSONString returns a json string representation of the request
func (r *Request) ToJSONString() string {
j, _ := json.MarshalIndent(r, "", " ")
return string(j)
}
// ArgInt64 returns an int64 value for an argument in the request named `name`
// If it exists, attempts to part it to a 64-bit integer
// Else, `defaultVal` is returned
func (r *Request) ArgInt64(name string, defaultVal int64) int64 {
val := int64(0)
if _, ok := r.Params[name]; !ok {
return defaultVal
}
var e error
if val, e = strconv.ParseInt(r.Params[name], 10, 64); e != nil {
return defaultVal
}
return val
}
// ArgInt returns an int value for an argument in the request named `name`
// If it exists, attempts to part it to a 64-bit integer
// Else, `defaultVal` is returned
func (r *Request) ArgInt(name string, defaultVal int) int {
val := int(0)
if _, ok := r.Params[name]; !ok {
return defaultVal
}
var e error
if val, e = strconv.Atoi(r.Params[name]); e != nil {
return defaultVal
}
return val
}
// Arg returns a string value for an argument in the request named `name`
// If it exists, returns it as a string
// Else, returns the `defaultVal`
func (r *Request) Arg(name string, defaultVal string) string {
if _, ok := r.Params[name]; !ok {
return defaultVal
}
return r.Params[name]
}
// Header gets a header by its name
func (r *Request) Header(name string) string {
if _, ok := r.Headers[name]; ok {
return r.Headers[name]
}
return ""
}
// BodyJSON extracts the json from the body of a post or put request
func (r *Request) BodyJSON(obj interface{}) {
json.Unmarshal([]byte(r.Body), obj)
}
// AuthKey returns the authorization key from the request header
func (r *Request) AuthKey() string {
authKey := r.Header(AuthHeaderKey)
if len(AuthHeaderValuePrefix) >= len(authKey) {
return ""
}
return authKey[len(AuthHeaderValuePrefix):]
}
// IP returns the IP from which the request originated
func (r *Request) IP() string {
var ip string
var ok bool
ip, ok = r.Headers["X-Forwarded-For"]
// Return localhost by default
if !ok || len(ip) == 0 {
return "127.0.0.1"
}
if strings.Contains(ip, ", ") {
ips := strings.Split(ip, ", ")
return ips[0]
}
return ip
}