mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
(#1554) Add support for all common compressions (write and read)
- Remove the context.Context interface and export the *context, the iris.Context now points to the pointer\nSupport compression and rate limiting in the FileServer\nBit of code organisation Former-commit-id: ad1c61bf968059510c6be9e7f2cceec7da70ba17
This commit is contained in:
@@ -54,7 +54,7 @@ type (
|
||||
// return a unique session id.
|
||||
// By default we will use a uuid impl package to generate
|
||||
// that, but developers can change that with simple assignment.
|
||||
SessionIDGenerator func(ctx context.Context) string
|
||||
SessionIDGenerator func(ctx *context.Context) string
|
||||
|
||||
// DisableSubdomainPersistence set it to true in order dissallow your subdomains to have access to the session cookie
|
||||
//
|
||||
@@ -70,7 +70,7 @@ func (c Config) Validate() Config {
|
||||
}
|
||||
|
||||
if c.SessionIDGenerator == nil {
|
||||
c.SessionIDGenerator = func(context.Context) string {
|
||||
c.SessionIDGenerator = func(*context.Context) string {
|
||||
id, _ := uuid.NewRandom()
|
||||
return id.String()
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func (s *Sessions) GetCookieOptions() []context.CookieOption {
|
||||
}
|
||||
|
||||
// updateCookie gains the ability of updating the session browser cookie to any method which wants to update it
|
||||
func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Duration, options ...context.CookieOption) {
|
||||
func (s *Sessions) updateCookie(ctx *context.Context, sid string, expires time.Duration, options ...context.CookieOption) {
|
||||
cookie := &http.Cookie{}
|
||||
|
||||
// The RFC makes no mention of encoding url value, so here I think to encode both sessionid key and the value using the safe(to put and to use as cookie) url-encoding
|
||||
@@ -76,7 +76,7 @@ func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Du
|
||||
// on MVC and APIContainer as well.
|
||||
//
|
||||
// NOTE: Use `app.Use(sess.Handler())` instead, avoid using `Start` manually.
|
||||
func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOption) *Session {
|
||||
func (s *Sessions) Start(ctx *context.Context, cookieOptions ...context.CookieOption) *Session {
|
||||
cookieValue := ctx.GetCookie(s.config.Cookie, cookieOptions...)
|
||||
|
||||
if cookieValue == "" { // cookie doesn't exist, let's generate a session and set a cookie.
|
||||
@@ -123,7 +123,7 @@ func (s *Sessions) Handler(cookieOptions ...context.CookieOption) context.Handle
|
||||
requestOptions = append(requestOptions, context.CookieEncoding(s.config.Encoding, s.config.Cookie))
|
||||
}
|
||||
|
||||
return func(ctx context.Context) {
|
||||
return func(ctx *context.Context) {
|
||||
ctx.AddCookieOptions(requestOptions...) // request life-cycle options.
|
||||
|
||||
session := s.Start(ctx, cookieOptions...) // this cookie's end-developer's custom options.
|
||||
@@ -142,7 +142,7 @@ func (s *Sessions) Handler(cookieOptions ...context.CookieOption) context.Handle
|
||||
// Note: It will return nil if the session got destroyed by the same request.
|
||||
// If you need to destroy and start a new session in the same request you need to call
|
||||
// sessions manager's `Start` method after Destroy.
|
||||
func Get(ctx context.Context) *Session {
|
||||
func Get(ctx *context.Context) *Session {
|
||||
if v := ctx.Values().Get(sessionContextKey); v != nil {
|
||||
if sess, ok := v.(*Session); ok {
|
||||
return sess
|
||||
@@ -154,14 +154,14 @@ func Get(ctx context.Context) *Session {
|
||||
}
|
||||
|
||||
// StartWithPath same as `Start` but it explicitly accepts the cookie path option.
|
||||
func (s *Sessions) StartWithPath(ctx context.Context, path string) *Session {
|
||||
func (s *Sessions) StartWithPath(ctx *context.Context, path string) *Session {
|
||||
return s.Start(ctx, context.CookiePath(path))
|
||||
}
|
||||
|
||||
// ShiftExpiration move the expire date of a session to a new date
|
||||
// by using session default timeout configuration.
|
||||
// It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet.
|
||||
func (s *Sessions) ShiftExpiration(ctx context.Context, cookieOptions ...context.CookieOption) error {
|
||||
func (s *Sessions) ShiftExpiration(ctx *context.Context, cookieOptions ...context.CookieOption) error {
|
||||
return s.UpdateExpiration(ctx, s.config.Expires, cookieOptions...)
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ func (s *Sessions) ShiftExpiration(ctx context.Context, cookieOptions ...context
|
||||
// by using timeout value passed by `expires` receiver.
|
||||
// It will return `ErrNotFound` when trying to update expiration on a non-existence or not valid session entry.
|
||||
// It will return `ErrNotImplemented` if a database is used and it does not support this feature, yet.
|
||||
func (s *Sessions) UpdateExpiration(ctx context.Context, expires time.Duration, cookieOptions ...context.CookieOption) error {
|
||||
func (s *Sessions) UpdateExpiration(ctx *context.Context, expires time.Duration, cookieOptions ...context.CookieOption) error {
|
||||
cookieValue := ctx.GetCookie(s.config.Cookie)
|
||||
if cookieValue == "" {
|
||||
return ErrNotFound
|
||||
@@ -203,7 +203,7 @@ func (s *Sessions) OnDestroy(listeners ...DestroyListener) {
|
||||
// Next calls of `sessions.Get` will occur to a nil Session,
|
||||
// use `Sessions#Start` method for renewal
|
||||
// or use the Session's Destroy method which does keep the session entry with its values cleared.
|
||||
func (s *Sessions) Destroy(ctx context.Context) {
|
||||
func (s *Sessions) Destroy(ctx *context.Context) {
|
||||
cookieValue := ctx.GetCookie(s.config.Cookie)
|
||||
if cookieValue == "" { // nothing to destroy
|
||||
return
|
||||
|
||||
@@ -31,7 +31,7 @@ func testSessions(t *testing.T, app *iris.Application) {
|
||||
"Secret": "dsads£2132215£%%Ssdsa",
|
||||
}
|
||||
|
||||
writeValues := func(ctx context.Context) {
|
||||
writeValues := func(ctx *context.Context) {
|
||||
s := sessions.Get(ctx)
|
||||
sessValues := s.GetAll()
|
||||
|
||||
@@ -45,7 +45,7 @@ func testSessions(t *testing.T, app *iris.Application) {
|
||||
app.Party("subdomain.").Get("/get", writeValues)
|
||||
}
|
||||
|
||||
app.Post("/set", func(ctx context.Context) {
|
||||
app.Post("/set", func(ctx *context.Context) {
|
||||
s := sessions.Get(ctx)
|
||||
vals := make(map[string]interface{})
|
||||
if err := ctx.ReadJSON(&vals); err != nil {
|
||||
@@ -56,16 +56,16 @@ func testSessions(t *testing.T, app *iris.Application) {
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx *context.Context) {
|
||||
writeValues(ctx)
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx *context.Context) {
|
||||
sessions.Get(ctx).Clear()
|
||||
writeValues(ctx)
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx *context.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
if session.IsNew() {
|
||||
t.Fatal("expected session not to be nil on destroy")
|
||||
@@ -82,16 +82,16 @@ func testSessions(t *testing.T, app *iris.Application) {
|
||||
})
|
||||
|
||||
// cookie should be new.
|
||||
app.Get("/after_destroy_renew", func(ctx context.Context) {
|
||||
app.Get("/after_destroy_renew", func(ctx *context.Context) {
|
||||
isNew := sessions.Get(ctx).IsNew()
|
||||
ctx.Writef("%v", isNew)
|
||||
})
|
||||
|
||||
app.Get("/multi_start_set_get", func(ctx context.Context) {
|
||||
app.Get("/multi_start_set_get", func(ctx *context.Context) {
|
||||
s := sessions.Get(ctx)
|
||||
s.Set("key", "value")
|
||||
ctx.Next()
|
||||
}, func(ctx context.Context) {
|
||||
}, func(ctx *context.Context) {
|
||||
s := sessions.Get(ctx)
|
||||
_, err := ctx.Writef(s.GetString("key"))
|
||||
if err != nil {
|
||||
@@ -140,12 +140,12 @@ func TestFlashMessages(t *testing.T) {
|
||||
"Secret": "dsads£2132215£%%Ssdsa",
|
||||
}
|
||||
|
||||
writeValues := func(ctx context.Context, values map[string]interface{}) error {
|
||||
writeValues := func(ctx *context.Context, values map[string]interface{}) error {
|
||||
_, err := ctx.JSON(values)
|
||||
return err
|
||||
}
|
||||
|
||||
app.Post("/set", func(ctx context.Context) {
|
||||
app.Post("/set", func(ctx *context.Context) {
|
||||
vals := make(map[string]interface{})
|
||||
if err := ctx.ReadJSON(&vals); err != nil {
|
||||
t.Fatalf("Cannot readjson. Trace %s", err.Error())
|
||||
@@ -158,7 +158,7 @@ func TestFlashMessages(t *testing.T) {
|
||||
ctx.StatusCode(iris.StatusOK)
|
||||
})
|
||||
|
||||
writeFlashValues := func(ctx context.Context) {
|
||||
writeFlashValues := func(ctx *context.Context) {
|
||||
s := sess.Start(ctx)
|
||||
|
||||
flashes := s.GetFlashes()
|
||||
@@ -167,23 +167,23 @@ func TestFlashMessages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
app.Get("/get_single", func(ctx context.Context) {
|
||||
app.Get("/get_single", func(ctx *context.Context) {
|
||||
s := sess.Start(ctx)
|
||||
flashMsgString := s.GetFlashString(valueSingleKey)
|
||||
ctx.WriteString(flashMsgString)
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx *context.Context) {
|
||||
writeFlashValues(ctx)
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx context.Context) {
|
||||
app.Get("/clear", func(ctx *context.Context) {
|
||||
s := sess.Start(ctx)
|
||||
s.ClearFlashes()
|
||||
writeFlashValues(ctx)
|
||||
})
|
||||
|
||||
app.Get("/destroy", func(ctx context.Context) {
|
||||
app.Get("/destroy", func(ctx *context.Context) {
|
||||
sess.Destroy(ctx)
|
||||
writeFlashValues(ctx)
|
||||
ctx.StatusCode(iris.StatusOK)
|
||||
@@ -191,7 +191,7 @@ func TestFlashMessages(t *testing.T) {
|
||||
})
|
||||
|
||||
// request cookie should be empty
|
||||
app.Get("/after_destroy", func(ctx context.Context) {
|
||||
app.Get("/after_destroy", func(ctx *context.Context) {
|
||||
ctx.StatusCode(iris.StatusOK)
|
||||
})
|
||||
|
||||
@@ -233,7 +233,7 @@ func TestSessionsUpdateExpiration(t *testing.T) {
|
||||
Logged bool `json:"logged"`
|
||||
}
|
||||
|
||||
var writeResponse = func(ctx context.Context) {
|
||||
var writeResponse = func(ctx *context.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
ctx.JSON(response{
|
||||
SessionID: session.ID(),
|
||||
@@ -241,7 +241,7 @@ func TestSessionsUpdateExpiration(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
app.Get("/get", func(ctx context.Context) {
|
||||
app.Get("/get", func(ctx *context.Context) {
|
||||
writeResponse(ctx)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user