File tree Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
"os"
14
14
"path/filepath"
15
15
"strings"
16
+ "sync"
16
17
)
17
18
18
19
type (
@@ -198,6 +199,7 @@ type (
198
199
handler HandlerFunc
199
200
store Map
200
201
echo * Echo
202
+ lock sync.RWMutex
201
203
}
202
204
)
203
205
@@ -360,10 +362,18 @@ func (c *context) Cookies() []*http.Cookie {
360
362
}
361
363
362
364
func (c * context ) Get (key string ) interface {} {
365
+ if c .echo .SafeContext {
366
+ c .lock .RLock ()
367
+ defer c .lock .RUnlock ()
368
+ }
363
369
return c .store [key ]
364
370
}
365
371
366
372
func (c * context ) Set (key string , val interface {}) {
373
+ if c .echo .SafeContext {
374
+ c .lock .Lock ()
375
+ defer c .lock .Unlock ()
376
+ }
367
377
if c .store == nil {
368
378
c .store = make (Map )
369
379
}
Original file line number Diff line number Diff line change @@ -390,12 +390,26 @@ func TestContextRedirect(t *testing.T) {
390
390
}
391
391
392
392
func TestContextStore (t * testing.T ) {
393
- var c Context
394
- c = new (context )
393
+ e := & Echo {}
394
+
395
+ c := & context {
396
+ echo : e ,
397
+ }
395
398
c .Set ("name" , "Jon Snow" )
396
399
assert .Equal (t , "Jon Snow" , c .Get ("name" ))
397
400
}
398
401
402
+ func TestContextSafeStore (t * testing.T ) {
403
+ e := & Echo {}
404
+ e .SafeContext = true
405
+
406
+ c := & context {
407
+ echo : e ,
408
+ }
409
+ c .Set ("name" , "Jon Safe" )
410
+ assert .Equal (t , "Jon Safe" , c .Get ("name" ))
411
+ }
412
+
399
413
func TestContextHandler (t * testing.T ) {
400
414
e := New ()
401
415
r := e .Router ()
Original file line number Diff line number Diff line change 84
84
Validator Validator
85
85
Renderer Renderer
86
86
Logger Logger
87
+ SafeContext bool
87
88
}
88
89
89
90
// Route contains a handler and information for matching against requests.
You can’t perform that action at this time.
0 commit comments