Browse Source

Update session module

Vova Tkach 6 years ago
parent
commit
b581fb6f4e

+ 1 - 1
go.mod

@@ -4,7 +4,7 @@ require (
 	github.com/go-sql-driver/mysql v1.4.1
 	github.com/vladimirok5959/golang-server-bootstrap v1.0.4
 	github.com/vladimirok5959/golang-server-resources v1.0.2
-	github.com/vladimirok5959/golang-server-sessions v1.0.1
+	github.com/vladimirok5959/golang-server-sessions v1.0.2
 	github.com/vladimirok5959/golang-server-static v1.0.0
 	google.golang.org/appengine v1.4.0 // indirect
 )

+ 2 - 0
go.sum

@@ -11,6 +11,8 @@ github.com/vladimirok5959/golang-server-resources v1.0.2 h1:XwxFXyaOtfDGRmYp8P9q
 github.com/vladimirok5959/golang-server-resources v1.0.2/go.mod h1:tsf2oAEf3E3ukiQSCO7dstl0IXbEXec68UUIiMWysBc=
 github.com/vladimirok5959/golang-server-sessions v1.0.1 h1:cQsLk8hz7pkIV7/XectfejDF2j+61FE+/s4xErxBNJw=
 github.com/vladimirok5959/golang-server-sessions v1.0.1/go.mod h1:W6eCEIltyTs5IUvN1DfzLk59z+M1031kr8bMFUiq8vU=
+github.com/vladimirok5959/golang-server-sessions v1.0.2 h1:VG14aTnH8+vb81quhekkrJ1vTi57EU4USDX1UzDYIzw=
+github.com/vladimirok5959/golang-server-sessions v1.0.2/go.mod h1:W6eCEIltyTs5IUvN1DfzLk59z+M1031kr8bMFUiq8vU=
 github.com/vladimirok5959/golang-server-static v1.0.0 h1:jmVNUCVF44+Am0euUMYFN4L3gdJYwm7aS8LFUmUAHJk=
 github.com/vladimirok5959/golang-server-static v1.0.0/go.mod h1:dxZsjCCpT65Z9dLP6p7RmR2rbgtYj6E4FSSSUqkNrsw=
 golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

+ 4 - 1
vendor/github.com/vladimirok5959/golang-server-sessions/session/bool.go

@@ -17,6 +17,9 @@ func (this *Session) GetBool(name string, def bool) bool {
 }
 
 func (this *Session) SetBool(name string, value bool) {
+	isset := this.IsSetBool(name)
 	this.v.Bool[name] = value
-	this.c = true
+	if isset || value != false {
+		this.c = true
+	}
 }

+ 1 - 3
vendor/github.com/vladimirok5959/golang-server-sessions/session/clean.go

@@ -11,11 +11,9 @@ func Clean(tmpdir string) error {
 	if err != nil {
 		return err
 	}
-	now := time.Now()
-	exp := 7 * 24 * time.Hour
 	for _, file := range files {
 		if len(file.Name()) == 40 {
-			if diff := now.Sub(file.ModTime()); diff > exp {
+			if diff := time.Now().Sub(file.ModTime()); diff > 24*time.Hour {
 				err = os.Remove(tmpdir + string(os.PathSeparator) + file.Name())
 				if err != nil {
 					return err

+ 4 - 1
vendor/github.com/vladimirok5959/golang-server-sessions/session/int.go

@@ -17,6 +17,9 @@ func (this *Session) GetInt(name string, def int) int {
 }
 
 func (this *Session) SetInt(name string, value int) {
+	isset := this.IsSetInt(name)
 	this.v.Int[name] = value
-	this.c = true
+	if isset || value != 0 {
+		this.c = true
+	}
 }

+ 9 - 2
vendor/github.com/vladimirok5959/golang-server-sessions/session/session.go

@@ -32,7 +32,8 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 	if err == nil && len(cookie.Value) == 40 {
 		// Load from file
 		sess.i = cookie.Value
-		f, err := os.Open(sess.d + string(os.PathSeparator) + sess.i)
+		fname := sess.d + string(os.PathSeparator) + sess.i
+		f, err := os.Open(fname)
 		if err == nil {
 			defer f.Close()
 			dec := json.NewDecoder(f)
@@ -40,6 +41,13 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 			if err == nil {
 				return &sess
 			}
+
+			// Update file last modify time if needs
+			if info, err := os.Stat(fname); err == nil {
+				if time.Now().Sub(info.ModTime()) > 30*time.Minute {
+					_ = os.Chtimes(fname, time.Now(), time.Now())
+				}
+			}
 		}
 	} else {
 		// Create new
@@ -63,7 +71,6 @@ func New(w http.ResponseWriter, r *http.Request, tmpdir string) *Session {
 		Int:    map[string]int{},
 		String: map[string]string{},
 	}
-	sess.c = true
 
 	return &sess
 }

+ 4 - 1
vendor/github.com/vladimirok5959/golang-server-sessions/session/string.go

@@ -17,6 +17,9 @@ func (this *Session) GetString(name string, def string) string {
 }
 
 func (this *Session) SetString(name string, value string) {
+	isset := this.IsSetString(name)
 	this.v.String[name] = value
-	this.c = true
+	if isset || value != "" {
+		this.c = true
+	}
 }

+ 1 - 1
vendor/modules.txt

@@ -4,7 +4,7 @@ github.com/go-sql-driver/mysql
 github.com/vladimirok5959/golang-server-bootstrap/bootstrap
 # github.com/vladimirok5959/golang-server-resources v1.0.2
 github.com/vladimirok5959/golang-server-resources/resource
-# github.com/vladimirok5959/golang-server-sessions v1.0.1
+# github.com/vladimirok5959/golang-server-sessions v1.0.2
 github.com/vladimirok5959/golang-server-sessions/session
 # github.com/vladimirok5959/golang-server-static v1.0.0
 github.com/vladimirok5959/golang-server-static/static