From 859a62bf01b2c8d0ecd1b10ed0fcbbd5e82f52b4 Mon Sep 17 00:00:00 2001 From: Wing Gao Date: Fri, 15 Dec 2017 21:46:18 +0800 Subject: [PATCH 1/5] fix cookie expire equals to -1 when call UpdateExpiration Former-commit-id: 69107db0f9be87db2057c6d261bd896c0358bc45 --- sessions/config.go | 3 ++- sessions/cookie.go | 13 +++++-------- sessions/sessions.go | 5 +++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/sessions/config.go b/sessions/config.go index 37d94ca9..ecfd99a6 100644 --- a/sessions/config.go +++ b/sessions/config.go @@ -58,7 +58,8 @@ type ( // AllowReclaim will allow to // Destroy and Start a session in the same request handler. - // All it does is that it removes the cookie for both `Request` and `ResponseWriter`. + // All it does is that it removes the cookie for both `Request` and `ResponseWriter` while `Destroy` + // or add a new cookie to `Request` while `Start`. // // Defaults to false. AllowReclaim bool diff --git a/sessions/cookie.go b/sessions/cookie.go index b4698b39..d95de513 100644 --- a/sessions/cookie.go +++ b/sessions/cookie.go @@ -31,9 +31,11 @@ func GetCookie(ctx context.Context, name string) string { } // AddCookie adds a cookie -func AddCookie(ctx context.Context, cookie *http.Cookie) { +func AddCookie(ctx context.Context, cookie *http.Cookie, reclaim bool) { // http.SetCookie(ctx.ResponseWriter(), cookie) - // ctx.Request().AddCookie(cookie) + if reclaim { + ctx.Request().AddCookie(cookie) + } ctx.SetCookie(cookie) } @@ -50,12 +52,7 @@ func RemoveCookie(ctx context.Context, name string, purge bool) { c.MaxAge = -1 c.Value = "" c.Path = "/" - AddCookie(ctx, c) - - if purge { - // delete request's cookie also, which is temporary available. - ctx.Request().Header.Set("Cookie", "") - } + AddCookie(ctx, c, purge) } // IsValidCookieDomain returns true if the receiver is a valid domain to set diff --git a/sessions/sessions.go b/sessions/sessions.go index 3a45e26a..e4025f84 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -96,7 +96,7 @@ func (s *Sessions) updateCookie(ctx context.Context, sid string, expires time.Du // encode the session id cookie client value right before send it. cookie.Value = s.encodeCookieValue(cookie.Value) - AddCookie(ctx, cookie) + AddCookie(ctx, cookie, s.config.AllowReclaim) } // Start should start the session for the particular request. @@ -131,7 +131,8 @@ func (s *Sessions) UpdateExpiration(ctx context.Context, expires time.Duration) cookieValue := s.decodeCookieValue(GetCookie(ctx, s.config.Cookie)) if cookieValue != "" { - if s.provider.UpdateExpiration(cookieValue, expires) { + // we should also allow it to expire when the browser closed + if s.provider.UpdateExpiration(cookieValue, expires) || expires == -1 { s.updateCookie(ctx, cookieValue, expires) } } From c6b12ab7548eef1190ea486a5fe9980aecca5f74 Mon Sep 17 00:00:00 2001 From: Wing Gao Date: Fri, 15 Dec 2017 22:42:18 +0800 Subject: [PATCH 2/5] need a better way to replace an existed cookie Former-commit-id: bee4a686d50e61e607e7f86c1dee93a877cd1413 --- sessions/cookie.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sessions/cookie.go b/sessions/cookie.go index d95de513..537f8d19 100644 --- a/sessions/cookie.go +++ b/sessions/cookie.go @@ -53,6 +53,11 @@ func RemoveCookie(ctx context.Context, name string, purge bool) { c.Value = "" c.Path = "/" AddCookie(ctx, c, purge) + + if purge { + // delete request's cookie also, which is temporary available. + ctx.Request().Header.Set("Cookie", "") + } } // IsValidCookieDomain returns true if the receiver is a valid domain to set From e4aa414add031309ea6ea57596031549cda5b2eb Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Sun, 17 Dec 2017 21:45:11 -0600 Subject: [PATCH 3/5] fix a typo Former-commit-id: 266f99ad325eb0463c76fb3f6123dc0cdaba1aad --- core/router/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/router/router.go b/core/router/router.go index 05c5af11..a774708a 100644 --- a/core/router/router.go +++ b/core/router/router.go @@ -83,7 +83,7 @@ func (router *Router) BuildRouter(cPool *context.Pool, requestHandler RequestHan // be aware to change the global variables of 'ParamStart' and 'ParamWildcardStart'. // can be used to implement a custom proxy or // a custom router which should work with raw ResponseWriter, *Request -// instead of the Context(which agaiin, can be retrieved by the Cramework's context pool). +// instead of the Context(which again, can be retrieved by the Cramework's context pool). // // Note: Downgrade will by-pass the Wrapper, the caller is responsible for everything. // Downgrade is thread-safe. From 835e2f8c087ab1435de12c5af0855001552e09f8 Mon Sep 17 00:00:00 2001 From: Cody Oss <6331106+codyoss@users.noreply.github.com> Date: Mon, 18 Dec 2017 00:25:57 -0600 Subject: [PATCH 4/5] fix another minor typo Former-commit-id: 0961f0882e63d3d43ef8c176a873bcd4e0d155ec --- core/router/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/router/router.go b/core/router/router.go index a774708a..805c0df1 100644 --- a/core/router/router.go +++ b/core/router/router.go @@ -83,7 +83,7 @@ func (router *Router) BuildRouter(cPool *context.Pool, requestHandler RequestHan // be aware to change the global variables of 'ParamStart' and 'ParamWildcardStart'. // can be used to implement a custom proxy or // a custom router which should work with raw ResponseWriter, *Request -// instead of the Context(which again, can be retrieved by the Cramework's context pool). +// instead of the Context(which again, can be retrieved by the Framework's context pool). // // Note: Downgrade will by-pass the Wrapper, the caller is responsible for everything. // Downgrade is thread-safe. From 5cbc3d6827bee22b122bc87a5e29ff95a3e8be09 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 21 Dec 2017 14:42:39 +0200 Subject: [PATCH 5/5] fix https://github.com/kataras/iris/issues/846 Former-commit-id: 5ade70e6133406c8bbb4ecef36541fdd0d857f6f --- README.md | 2 +- core/router/api_builder.go | 6 ++---- core/router/path.go | 20 ++++++++++++++++++++ core/router/path_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6b8f678a..15c6ac65 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## I'm working hard on the [dev](https://github.com/kataras/iris/tree/dev) branch for the next release of Iris, v9. +## I'm working hard on the [dev](https://github.com/kataras/iris/tree/dev) branch for the next release of Iris. Do you remember, last Christmas? I did publish the version 6 with net/http and HTTP/2 support, and you've embraced Iris with so much love, ultimately it was a successful move. diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 475b68d7..dc1438b7 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -199,14 +199,12 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co // This method is used behind the scenes at the `Controller` function // in order to handle more than one paths for the same controller instance. func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti string, handlers ...context.Handler) (routes []*Route) { - trimmedPath := strings.Trim(relativePathorMulti, " ") - trimmedMethod := strings.Trim(methodOrMulti, " ") // at least slash // a space // at least one other slash for the next path // app.Controller("/user /user{id}", new(UserController)) - paths := strings.Split(trimmedPath, " ") - methods := strings.Split(trimmedMethod, " ") + paths := splitPath(relativePathorMulti) + methods := splitMethod(methodOrMulti) for _, p := range paths { if p != "" { for _, method := range methods { diff --git a/core/router/path.go b/core/router/path.go index 914f75e7..5311e4d2 100644 --- a/core/router/path.go +++ b/core/router/path.go @@ -45,6 +45,26 @@ func suffix(s string, suffix string) string { return s } +func splitMethod(methodMany string) []string { + methodMany = strings.Trim(methodMany, " ") + return strings.Split(methodMany, " ") +} + +func splitPath(pathMany string) (paths []string) { + pathMany = strings.Trim(pathMany, " ") + pathsWithoutSlashFromFirstAndSoOn := strings.Split(pathMany, " /") + for _, path := range pathsWithoutSlashFromFirstAndSoOn { + if path == "" { + continue + } + if path[0] != '/' { + path = "/" + path + } + paths = append(paths, path) + } + return +} + func joinPath(path1 string, path2 string) string { return path.Join(path1, path2) } diff --git a/core/router/path_test.go b/core/router/path_test.go index 31194871..28c49b13 100644 --- a/core/router/path_test.go +++ b/core/router/path_test.go @@ -4,6 +4,44 @@ import ( "testing" ) +func TestSplitPath(t *testing.T) { + tests := []struct { + path string + expected []string + }{ + {"/v2/stores/{id:string format(uuid)} /v3", + []string{"/v2/stores/{id:string format(uuid)}", "/v3"}}, + {"/user/{id:int} /admin/{id:int}", + []string{"/user/{id:int}", "/admin/{id:int}"}}, + {"/user /admin", + []string{"/user", "/admin"}}, + {"/single_no_params", + []string{"/single_no_params"}}, + {"/single/{id:int}", + []string{"/single/{id:int}"}}, + } + + equalSlice := func(s1 []string, s2 []string) bool { + if len(s1) != len(s2) { + return false + } + + for i := range s1 { + if s2[i] != s1[i] { + return false + } + } + + return true + } + + for i, tt := range tests { + paths := splitPath(tt.path) + if expected, got := tt.expected, paths; !equalSlice(expected, got) { + t.Fatalf("[%d] - expected paths '%#v' but got '%#v'", i, expected, got) + } + } +} func TestSplitSubdomainAndPath(t *testing.T) { tests := []struct { original string