mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:26:26 +01:00
Merge branch 'master' into dev
Former-commit-id: d338e6e846b4379199015dc4ae76677b2d8cbe53
This commit is contained in:
commit
91ee8287a8
|
@ -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.
|
||||
|
||||
|
|
|
@ -198,14 +198,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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,7 +52,7 @@ func RemoveCookie(ctx context.Context, name string, purge bool) {
|
|||
c.MaxAge = -1
|
||||
c.Value = ""
|
||||
c.Path = "/"
|
||||
AddCookie(ctx, c)
|
||||
AddCookie(ctx, c, purge)
|
||||
|
||||
if purge {
|
||||
// delete request's cookie also, which is temporary available.
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user