-
Notifications
You must be signed in to change notification settings - Fork 8
/
context.go
245 lines (200 loc) · 7.03 KB
/
context.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package yarf
import (
"compress/gzip"
"encoding/json"
"encoding/xml"
"net/http"
"strings"
)
// ContextData interface represents a common get/set/del set of methods to handle data storage.
// Is designed to be used as the Data property of the Context obejct.
// The Data property is a free storage unit that apps using the framework can implement to their convenience
// to share context data during a request life.
// All methods returns an error status that different implementations can design to fulfill their needs.
type ContextData interface {
// Get retrieves a data item by it's key name.
Get(key string) (interface{}, error)
// Set saves a data item under a key name.
Set(key string, data interface{}) error
// Del removes the data item and key name for a given key.
Del(key string) error
}
// Params wraps a map[string]string and adds Get/Set/Del methods to work with it.
// Inspired on url.Values but simpler as it doesn't handles a map[string][]string
type Params map[string]string
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns
// the empty string.
func (p Params) Get(key string) string {
if p == nil {
return ""
}
param, _ := p[key]
return param
}
// Set sets the key to value. It replaces any existing values.
func (p Params) Set(key, value string) {
p[key] = value
}
// Del deletes the values associated with key.
func (p Params) Del(key string) {
delete(p, key)
}
// Context is the data/status storage of every YARF request.
// Every request will instantiate a new Context object and fill in with all the request data.
// Each request Context will be shared along the entire request life to ensure accesibility of its data at all levels.
type Context struct {
// The *http.Request object as received by the HandleFunc.
Request *http.Request
// The http.ResponseWriter object as received by the HandleFunc.
Response http.ResponseWriter
// Parameters received through URL route
Params Params
// Free storage to be used freely by apps to their convenience.
Data ContextData
// Group route storage for dispatch
groupDispatch []Router
}
// NewContext creates a new *Context object with default values and returns it.
func NewContext(r *http.Request, rw http.ResponseWriter) *Context {
return &Context{
Request: r,
Response: rw,
Params: Params{},
}
}
// Status sets the HTTP status code to be returned on the response.
func (c *Context) Status(code int) {
c.Response.WriteHeader(code)
}
// Param is a wrapper for c.Params.Get()
func (c *Context) Param(name string) string {
return c.Params.Get(name)
}
// FormValue is a wrapper for c.Request.Form.Get() and it calls c.Request.ParseForm().
func (c *Context) FormValue(name string) string {
c.Request.ParseForm()
return c.Request.Form.Get(name)
}
// QueryValue is a wrapper for c.Request.URL.Query().Get().
func (c *Context) QueryValue(name string) string {
return c.Request.URL.Query().Get(name)
}
// GetClientIP retrieves the client IP address from the request information.
// It detects common proxy headers to return the actual client's IP and not the proxy's.
func (c *Context) GetClientIP() (ip string) {
var pIPs string
var pIPList []string
if pIPs = c.Request.Header.Get("X-Real-Ip"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else if pIPs = c.Request.Header.Get("Real-Ip"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else if pIPs = c.Request.Header.Get("X-Forwarded-For"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else if pIPs = c.Request.Header.Get("X-Forwarded"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else if pIPs = c.Request.Header.Get("Forwarded-For"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else if pIPs = c.Request.Header.Get("Forwarded"); pIPs != "" {
pIPList = strings.Split(pIPs, ",")
ip = strings.TrimSpace(pIPList[0])
} else {
ip = c.Request.RemoteAddr
}
return strings.Split(ip, ":")[0]
}
// Redirect sends the corresponding HTTP redirect response with the provided URL and status code.
// It's just a wrapper for net/http.Redirect()
func (c *Context) Redirect(url string, code int) {
http.Redirect(c.Response, c.Request, url, code)
}
// Render writes a string to the http.ResponseWriter.
// This is the default renderer that just sends the string to the client.
// Check other Render[Type] functions for different types.
func (c *Context) Render(content string) {
// Write response
c.Response.Write([]byte(content))
}
// RenderGzip takes a []byte content and if the client accepts compressed responses,
// writes the compressed version of the content to the response.
// Otherwise it just writes the plain []byte to it.
func (c *Context) RenderGzip(content []byte) error {
// Check if client accepts compression
if !strings.Contains(c.Request.Header.Get("Accept-Encoding"), "gzip") {
c.Response.Write(content)
return nil
}
// Detect content type
c.Response.Header().Set("Content-Type", http.DetectContentType(content))
// Write compressed content
gz := gzip.NewWriter(c.Response)
defer gz.Close()
c.Response.Header().Set("Content-Encoding", "gzip")
c.Response.Header().Set("Vary", "Accept-Encoding")
c.Response.Header().Del("Content-Length")
gz.Write(content)
return nil
}
// RenderJSON takes a interface{} object and writes the JSON encoded string of it.
func (c *Context) RenderJSON(data interface{}) {
// Set content
encoded, err := json.Marshal(data)
if err != nil {
c.Response.Write([]byte(err.Error()))
} else {
c.Response.Write(encoded)
}
}
// RenderJSONIndent is the indented (beauty) of RenderJSON
func (c *Context) RenderJSONIndent(data interface{}) {
// Set content
encoded, err := json.MarshalIndent(data, "", " ")
if err != nil {
c.Response.Write([]byte(err.Error()))
} else {
c.Response.Write(encoded)
}
}
// RenderGzipJSON takes a interface{} object and writes the JSON verion through RenderGzip.
func (c *Context) RenderGzipJSON(data interface{}) {
// Create JSON content
encoded, err := json.Marshal(data)
if err != nil {
c.Response.Write([]byte(err.Error()))
}
c.RenderGzip(encoded)
}
// RenderXML takes a interface{} object and writes the XML encoded string of it.
func (c *Context) RenderXML(data interface{}) {
// Set content
encoded, err := xml.Marshal(data)
if err != nil {
c.Response.Write([]byte(err.Error()))
} else {
c.Response.Write(encoded)
}
}
// RenderXMLIndent is the indented (beauty) of RenderXML
func (c *Context) RenderXMLIndent(data interface{}) {
// Set content
encoded, err := xml.MarshalIndent(data, "", " ")
if err != nil {
c.Response.Write([]byte(err.Error()))
} else {
c.Response.Write(encoded)
}
}
// RenderGzipXML takes a interface{} object and writes the XML verion through RenderGzip.
func (c *Context) RenderGzipXML(data interface{}) {
// Set XML content
encoded, err := xml.Marshal(data)
if err != nil {
c.Response.Write([]byte(err.Error()))
}
c.RenderGzip(encoded)
}