Skip to content

Commit 8c4fa85

Browse files
Set safe context as default.
Allow use of unsafe context.
1 parent 4fe4d2d commit 8c4fa85

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

context.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,15 @@ func (c *context) Cookies() []*http.Cookie {
362362
}
363363

364364
func (c *context) Get(key string) interface{} {
365-
if c.echo.SafeContext {
365+
if !c.echo.UnsafeContext {
366366
c.lock.RLock()
367367
defer c.lock.RUnlock()
368368
}
369369
return c.store[key]
370370
}
371371

372372
func (c *context) Set(key string, val interface{}) {
373-
if c.echo.SafeContext {
373+
if !c.echo.UnsafeContext {
374374
c.lock.Lock()
375375
defer c.lock.Unlock()
376376
}
@@ -607,4 +607,3 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
607607
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
608608
// c.pvalues = nil
609609
}
610-

context_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,21 +528,54 @@ func TestContextStore(t *testing.T) {
528528
c := &context{
529529
echo: e,
530530
}
531+
531532
c.Set("name", "Jon Snow")
532533
testify.Equal(t, "Jon Snow", c.Get("name"))
533534
}
534535

535-
func TestContextSafeStore(t *testing.T) {
536+
func BenchmarkContext_Store(b *testing.B) {
537+
e := &Echo{}
538+
539+
c := &context{
540+
echo: e,
541+
}
542+
543+
for n := 0; n < b.N; n++ {
544+
c.Set("name", "Jon Snow")
545+
if c.Get("name") != "Jon Snow" {
546+
b.Fail()
547+
}
548+
}
549+
}
550+
551+
func TestContextUnsafeStore(t *testing.T) {
536552
e := &Echo{}
537-
e.SafeContext = true
553+
e.UnsafeContext = true
538554

539555
c := &context{
540556
echo: e,
541557
}
558+
542559
c.Set("name", "Jon Safe")
543560
testify.Equal(t, "Jon Safe", c.Get("name"))
544561
}
545562

563+
func BenchmarkContext_UnsafeStore(b *testing.B) {
564+
e := &Echo{}
565+
e.UnsafeContext = true
566+
567+
c := &context{
568+
echo: e,
569+
}
570+
571+
for n := 0; n < b.N; n++ {
572+
c.Set("name", "Jon Snow")
573+
if c.Get("name") != "Jon Snow" {
574+
b.Fail()
575+
}
576+
}
577+
}
578+
546579
func TestContextHandler(t *testing.T) {
547580
e := New()
548581
r := e.Router()

echo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type (
8484
Validator Validator
8585
Renderer Renderer
8686
Logger Logger
87-
SafeContext bool
87+
UnsafeContext bool
8888
}
8989

9090
// Route contains a handler and information for matching against requests.

0 commit comments

Comments
 (0)