forked from go-playground/lars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_17.go
163 lines (143 loc) · 4.92 KB
/
context_17.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
// +build go1.7
package lars
import (
"context"
"io"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
)
// Context is the context interface type
type Context interface {
context.Context
Request() *http.Request
Response() *Response
WebSocket() *websocket.Conn
Param(name string) string
QueryParams() url.Values
ParseForm() error
ParseMultipartForm(maxMemory int64) error
Set(key interface{}, value interface{})
Get(key interface{}) (value interface{}, exists bool)
Context() context.Context
WithContext(context.Context)
WithCancel() context.CancelFunc
WithDeadline(time.Time) context.CancelFunc
WithTimeout(time.Duration) context.CancelFunc
WithValue(key interface{}, val interface{})
Next()
RequestStart(w http.ResponseWriter, r *http.Request)
RequestEnd()
ClientIP() (clientIP string)
AcceptedLanguages(lowercase bool) []string
HandlerName() string
Stream(step func(w io.Writer) bool)
JSON(int, interface{}) error
JSONBytes(int, []byte) error
JSONP(int, interface{}, string) error
XML(int, interface{}) error
XMLBytes(int, []byte) error
Text(int, string) error
TextBytes(int, []byte) error
Attachment(r io.Reader, filename string) (err error)
Inline(r io.Reader, filename string) (err error)
Decode(includeFormQueryParams bool, maxMemory int64, v interface{}) (err error)
BaseContext() *Ctx
}
var _ context.Context = &Ctx{}
// Ctx encapsulates the http request, response context
type Ctx struct {
request *http.Request
response *Response
websocket *websocket.Conn
params Params
queryParams url.Values
handlers HandlersChain
parent Context
handlerName string
index int
formParsed bool
multipartFormParsed bool
}
// RequestStart resets the Context to it's default request state
func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
c.request = r
c.response.reset(w)
c.params = c.params[0:0]
c.queryParams = nil
c.index = -1
c.handlers = nil
c.formParsed = false
c.multipartFormParsed = false
}
// Set is used to store a new key/value pair using the
// context contained on this Ctx.
// It is a shortcut for context.WithValue(..., ...)
func (c *Ctx) Set(key interface{}, value interface{}) {
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, value))
}
// Get returns the value for the given key and is a shortcut
// for context.Value(...) ... but it
// also returns if the value was found or not.
func (c *Ctx) Get(key interface{}) (value interface{}, exists bool) {
value = c.request.Context().Value(key)
exists = value != nil
return
}
// context functions to comply with context.Context interface and keep context update on lars.Context object
// Context returns the request's context. To change the context, use
// WithContext.
//
// The returned context is always non-nil.
func (c *Ctx) Context() context.Context {
return c.request.Context()
}
// WithContext updates the underlying request's context with to ctx
// The provided ctx must be non-nil.
func (c *Ctx) WithContext(ctx context.Context) {
c.request = c.request.WithContext(ctx)
}
// Deadline calls the underlying context.Deadline()
func (c *Ctx) Deadline() (deadline time.Time, ok bool) {
return c.request.Context().Deadline()
}
// Done calls the underlying context.Done()
func (c *Ctx) Done() <-chan struct{} {
return c.request.Context().Done()
}
// Err calls the underlying context.Err()
func (c *Ctx) Err() error {
return c.request.Context().Err()
}
// Value calls the underlying context.Value()
func (c *Ctx) Value(key interface{}) interface{} {
return c.request.Context().Value(key)
}
// WithCancel calls context.WithCancel and automatically
// updates context on the containing lars.Ctx object.
func (c *Ctx) WithCancel() context.CancelFunc {
ctx, cf := context.WithCancel(c.request.Context())
c.request = c.request.WithContext(ctx)
return cf
}
// WithDeadline calls context.WithDeadline and automatically
// updates context on the containing lars.Ctx object.
func (c *Ctx) WithDeadline(deadline time.Time) context.CancelFunc {
ctx, cf := context.WithDeadline(c.request.Context(), deadline)
c.request = c.request.WithContext(ctx)
return cf
}
// WithTimeout calls context.WithTimeout and automatically
// updates context on the containing lars.Ctx object.
func (c *Ctx) WithTimeout(timeout time.Duration) context.CancelFunc {
ctx, cf := context.WithTimeout(c.request.Context(), timeout)
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
return cf
}
// WithValue calls golang.org/x/net/context WithValue and automatically
// updates context on the containing lars.Ctx object.
// Can also use Set() function on Ctx object (Recommended)
func (c *Ctx) WithValue(key interface{}, val interface{}) {
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, val)) // temporarily shallow copying to avoid problems with external libraries
}