-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
81 lines (67 loc) · 1.33 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
package gost
import (
"bytes"
"net/http"
)
type _Param struct {
key []byte
value []byte
next *_Param
}
func (self *_Param) find(key []byte) (*_Param, bool) {
switch bytes.Compare(self.key, key) {
case 0:
return self, true
case 1:
return nil, false
case -1:
if self.next == nil {
return self, false
}
target, found := self.next.find(key)
if target == nil {
return self, false
}
return target, found
}
return nil, false
}
type Context struct {
Request *http.Request
Writer http.ResponseWriter
urlParam *_Param
handlers *_Handler
middlewares *_Handler
}
func (self *Context) Get(key string) (string, bool) {
for current := self.urlParam; current != nil; current = current.next {
if bytes.Compare(current.key, []byte(key)) == 0 {
return string(current.value), true
}
}
return "", false
}
func (self *Context) setUrlParam(key, value []byte) {
keyBuf := []byte(key)
if self.urlParam == nil {
self.urlParam = new(_Param)
self.urlParam.key = keyBuf
self.urlParam.value = value
return
}
position, found := self.urlParam.find([]byte(key))
if found {
position.value = value
return
}
param := new(_Param)
param.key = keyBuf
param.value = value
if position == nil {
param.next = self.urlParam
self.urlParam = param
return
}
param.next = position.next
position.next = param
}