-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
152 lines (128 loc) · 3.25 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
package xingyun
import (
"fmt"
"net/http"
"github.com/gorilla/context"
)
const (
CONTEXT_KEY = "_XINGYUN_CONTEXT_"
)
var (
DefaultFormMaxMemmory int64 = 64 << 20
)
type ContextHandler interface {
ServeContext(ctx *Context)
}
type ContextHandlerFunc func(ctx *Context)
func (h ContextHandlerFunc) ServeContext(ctx *Context) {
h(ctx)
}
func ToHTTPHandlerFunc(h ContextHandler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.ServeContext(getUnInitedContext(r, w))
}
}
func FromHTTPHandlerFunc(h http.HandlerFunc) ContextHandlerFunc {
return func(ctx *Context) {
h.ServeHTTP(ctx.ResponseWriter, ctx.Request)
}
}
func ToHTTPHandler(h ContextHandler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeContext(getUnInitedContext(r, w))
})
}
func FromHTTPHandler(h http.Handler) ContextHandler {
return ContextHandlerFunc(func(ctx *Context) {
h.ServeHTTP(ctx.ResponseWriter, ctx.Request)
})
}
func wrapResponseWriter(w http.ResponseWriter) ResponseWriter {
rw, ok := w.(ResponseWriter)
if ok {
return rw
}
return NewResponseWriter(w)
}
type SessionStorage interface {
SetSession(sessionID string, key string, data []byte)
GetSession(sessionID string, key string) []byte
ClearSession(sessionID string, key string)
}
type Context struct {
ResponseWriter
Request *http.Request
Server *Server
Config *Config
Logger Logger
Params map[string]string
IsPanic bool
PanicError interface{}
StackMessage string
// use for user ContextHandler
Data map[string]interface{}
// use for user PipeHandler. avoid name conflict
PipeHandlerData map[string]interface{}
isInited bool
flash *Flash
staticData map[string][]string
opts *xsrfOptions
xsrf *xsrf
}
func GetContext(r *http.Request) *Context {
obj, ok := context.GetOk(r, CONTEXT_KEY)
if !ok {
panic(fmt.Errorf("can't get context, &r=%p", r))
}
ctx := obj.(*Context)
if !ctx.isInited {
panic(fmt.Errorf("get uninited context, &r=%p", r))
}
return ctx
}
func initContext(r *http.Request, w http.ResponseWriter, s *Server) *Context {
ctx := getUnInitedContext(r, w)
if ctx.isInited {
return ctx
}
*ctx = Context{
ResponseWriter: wrapResponseWriter(w),
Request: r,
Server: s,
Config: s.Config,
Logger: s.logger,
Params: map[string]string{},
Data: map[string]interface{}{},
staticData: map[string][]string{},
}
ctx.parseParams()
ctx.isInited = true
context.Set(r, CONTEXT_KEY, ctx)
s.logger.Debugf("init context, &r=%p", r)
return ctx
}
func getUnInitedContext(r *http.Request, w http.ResponseWriter) *Context {
ctx, ok := context.GetOk(r, CONTEXT_KEY)
if !ok {
newctx := &Context{Request: r, ResponseWriter: wrapResponseWriter(w)}
context.Set(r, CONTEXT_KEY, newctx)
return newctx
}
return ctx.(*Context)
}
func (ctx *Context) parseParams() {
var err error
err = ctx.Request.ParseMultipartForm(DefaultFormMaxMemmory)
if err != nil && err.Error() != http.ErrNotMultipart.Error() {
ctx.Logger.Errorf(err.Error())
return
}
for k, v := range ctx.Request.Form {
ctx.Params[k] = v[0]
}
}
func (ctx *Context) checkHeaderWrite() {
if ctx.ResponseWriter.Written() {
panic(fmt.Errorf("must write header before body"))
}
}