-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverctx.go
69 lines (58 loc) · 1.38 KB
/
serverctx.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
package tpl
import (
"context"
"net/http"
"sync"
"github.com/KarpelesLab/webutil"
)
type serverCtxOp int
const serverCtxReset serverCtxOp = iota
type serverCtx struct {
context.Context
request *http.Request
getV map[string]interface{}
postV map[string]interface{}
requestV map[string]interface{}
get, post sync.Once
}
func ServerCtx(request *http.Request) context.Context {
return &serverCtx{Context: request.Context(), request: request}
}
func ResetServerCtx(ctx context.Context) {
ctx.Value(serverCtxReset)
}
func (s *serverCtx) Value(key interface{}) interface{} {
if op, ok := key.(serverCtxOp); ok {
switch op {
case serverCtxReset:
s.get = sync.Once{}
s.post = sync.Once{}
}
}
keyStr, ok := key.(string)
if !ok {
return s.Context.Value(key)
}
switch keyStr {
case "$_get":
s.get.Do(func() {
s.getV = webutil.ParsePhpQuery(s.request.URL.RawQuery)
})
return s.getV
case "$_post":
s.post.Do(func() {
s.request.ParseMultipartForm(2 * 1024 * 1024)
s.postV = webutil.ConvertPhpQuery(s.request.PostForm)
s.requestV = webutil.ConvertPhpQuery(s.request.Form)
})
return s.postV
case "$_request":
s.post.Do(func() {
s.request.ParseMultipartForm(2 * 1024 * 1024)
s.postV = webutil.ConvertPhpQuery(s.request.PostForm)
s.requestV = webutil.ConvertPhpQuery(s.request.Form)
})
return s.requestV
}
return s.Context.Value(key)
}