-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathhttpcontext.go
268 lines (231 loc) · 8.31 KB
/
httpcontext.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package goku
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"path"
)
// http context
type HttpContext struct {
Request *http.Request // http request
responseWriter http.ResponseWriter // http response
Method string // http method
//self fields
RouteData *RouteData // route data
ViewData map[string]interface{} // view data for template
Data map[string]interface{} // data for httpcontex
Result ActionResulter // action result
Err error // process error
User string // user name
Canceled bool // cancel continue process the request and return
// private fileds
requestHandler *RequestHandler
responseContentCache *bytes.Buffer // cache response content, will write at end request
responseStatusCode int // cache response status code, will write at end request
//responseHeaderCache Header // cache response header, will write at end request
}
func (ctx *HttpContext) flushToResponse() {
// if len(ctx.responseHeaderCache) > 0 {
// for k, v := range ctx.responseHeaderCache {
// ctx.responseWriter.Header().Set(key, value)
// }
// }
if ctx.responseStatusCode > 0 {
ctx.responseWriter.WriteHeader(ctx.responseStatusCode)
}
if ctx.responseContentCache.Len() > 0 {
ctx.responseContentCache.WriteTo(ctx.responseWriter)
}
}
// Try not to use this unless you know exactly what you are doing
func (ctx *HttpContext) ResponseWriter() http.ResponseWriter {
return ctx.responseWriter
}
func (ctx *HttpContext) RootDir() string {
return ctx.requestHandler.ServerConfig.RootDir
}
func (ctx *HttpContext) StaticPath() string {
return path.Join(ctx.RootDir(), ctx.requestHandler.ServerConfig.StaticPath)
}
func (ctx *HttpContext) ViewPath() string {
return path.Join(ctx.RootDir(), ctx.requestHandler.ServerConfig.ViewPath)
}
// get the requert param,
// get from RouteData first,
// if no, get from Requet.FormValue
func (ctx *HttpContext) Get(name string) string {
v, ok := ctx.RouteData.Get(name)
if ok {
return v
}
return ctx.Request.FormValue(name)
}
// Header gets the response header
func (ctx *HttpContext) Header() http.Header {
return ctx.responseWriter.Header()
}
// set the response header
func (ctx *HttpContext) SetHeader(key string, value string) {
ctx.responseWriter.Header().Set(key, value)
//ctx.responseHeaderCache.Set(key, value)
}
// AddHeader adds response header
func (ctx *HttpContext) AddHeader(key string, value string) {
ctx.responseWriter.Header().Add(key, value)
}
// set response cookie header
func (ctx *HttpContext) SetCookie(cookie *http.Cookie) {
ctx.responseWriter.Header().Add("Set-Cookie", cookie.String())
}
func (ctx *HttpContext) GetHeader(key string) string {
return ctx.Request.Header.Get(key)
}
func (ctx *HttpContext) ContentType(ctype string) {
ctx.responseWriter.Header().Set("Content-Type", ctype)
//ctx.responseHeaderCache["Content-Type"] = ctype
}
func (ctx *HttpContext) Status(code int) {
//ctx.responseWriter.WriteHeader(code)
ctx.responseStatusCode = code
}
func (ctx *HttpContext) Write(b []byte) (int, error) {
//return ctx.ResponseWriter.Write(b)
return ctx.responseContentCache.Write(b)
}
func (ctx *HttpContext) WriteBuffer(bf *bytes.Buffer) {
//bf.WriteTo(ctx.ResponseWriter)
bf.WriteTo(ctx.responseContentCache)
}
func (ctx *HttpContext) WriteString(content string) {
//ctx.ResponseWriter.Write([]byte(content))
ctx.responseContentCache.Write([]byte(content))
}
func (ctx *HttpContext) WriteHeader(code int) {
//ctx.responseWriter.WriteHeader(code)
ctx.responseStatusCode = code
}
// IsAjax gets whether the request is by ajax
func (ctx *HttpContext) IsAjax() bool {
return ctx.GetHeader("X-Requested-With") == "XMLHttpRequest"
}
// render the view and return a *ViewResult.
// it will find the view in these rules:
// 1. /{ViewPath}/{Controller}/{viewName}
// 2. /{ViewPath}/shared/{viewName}
// if viewName start with '/',
// it will find the view direct by viewpath:
// 1. /{ViewPath}/{viewName}
func (ctx *HttpContext) Render(viewName string, viewModel interface{}) *ViewResult {
return ctx.rederView(viewName, "", viewModel, false)
}
// RenderWithLayout renders the view and return a *ViewResult
// it will find the view in these rules:
// 1. /{ViewPath}/{Controller}/{viewName}
// 2. /{ViewPath}/shared/{viewName}
func (ctx *HttpContext) RenderWithLayout(viewName, layout string, viewModel interface{}) *ViewResult {
return ctx.rederView(viewName, layout, viewModel, false)
}
// RenderPartial renders a Partial view and return a *ViewResult.
// this is not use layout.
// it will find the view in these rules:
// 1. /{ViewPath}/{Controller}/{viewName}
// 2. /{ViewPath}/shared/{viewName}
func (ctx *HttpContext) RenderPartial(viewName string, viewModel interface{}) *ViewResult {
return ctx.rederView(viewName, "", viewModel, true)
}
func (ctx *HttpContext) rederView(viewName, layout string, viewModel interface{}, isPartial bool) *ViewResult {
vr := &ViewResult{
ViewEngine: ctx.requestHandler.ViewEnginer,
TemplateEngine: ctx.requestHandler.TemplateEnginer,
ViewData: ctx.ViewData,
ViewModel: viewModel,
ViewName: viewName,
Layout: layout,
IsPartial: isPartial,
}
vr.Body = new(bytes.Buffer)
vr.Headers = map[string]string{"Content-Type": "text/html"}
return vr
}
// View renders the view and return a *ViewResult
// it will find the view in these rules:
// 1. /{ViewPath}/{Controller}/{action}
// 2. /{ViewPath}/shared/{action}
func (ctx *HttpContext) View(viewData interface{}) *ViewResult {
return ctx.Render("", viewData)
}
func (ctx *HttpContext) Redirect(url_ string) ActionResulter {
return &ActionResult{
StatusCode: http.StatusFound,
Headers: map[string]string{"Content-Type": "text/html", "Location": url_},
Body: bytes.NewBufferString("Redirecting to: " + url_),
}
}
func (ctx *HttpContext) RedirectPermanent(url_ string) ActionResulter {
return &ActionResult{
StatusCode: http.StatusMovedPermanently,
Headers: map[string]string{"Content-Type": "text/html", "Location": url_},
Body: bytes.NewBufferString("Redirecting to: " + url_),
}
}
// page not found
func (ctx *HttpContext) NotFound(message string) ActionResulter {
if message == "" {
message = "Page Not Found!"
}
return &ActionResult{
StatusCode: http.StatusNotFound,
Headers: map[string]string{"Content-Type": "text/html"},
Body: bytes.NewBufferString(message),
}
}
// content not modified
func (ctx *HttpContext) NotModified() ActionResulter {
return &ActionResult{
StatusCode: http.StatusNotModified,
}
}
func (ctx *HttpContext) Error(err interface{}) ActionResulter {
msg := fmt.Sprintf("%v", err)
return &ActionResult{
StatusCode: http.StatusInternalServerError,
Headers: map[string]string{"Content-Type": "text/plain"},
Body: bytes.NewBufferString(msg),
}
}
func (ctx *HttpContext) Raw(data string) ActionResulter {
return &ActionResult{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": "text/plain"},
Body: bytes.NewBufferString(data),
}
}
func (ctx *HttpContext) Html(data string) ActionResulter {
return &ActionResult{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": "text/html"},
Body: bytes.NewBufferString(data),
}
}
// Json returns json string result
// ctx.Json(obj) or ctx.Json(obj, "text/html")
func (ctx *HttpContext) Json(data interface{}, contentType ...string) ActionResulter {
var ct string
if len(contentType) == 1 {
ct = contentType[0]
} else {
ct = "text/javascript"
}
ar := &ActionResult{
StatusCode: http.StatusOK,
Headers: map[string]string{"Content-Type": ct},
Body: new(bytes.Buffer),
}
ec := json.NewEncoder(ar.Body)
ec.Encode(data)
return ar
}
// this.content = function(filename) {
// return new ContentResult(filename);
// }