2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
2013-09-26 10:07:00 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2013-04-05 15:50:53 +00:00
|
|
|
"io/ioutil"
|
2014-01-05 06:48:36 +00:00
|
|
|
"net/http"
|
2013-04-05 15:50:53 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
filepder = &FileProvider{}
|
|
|
|
gcmaxlifetime int64
|
|
|
|
)
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// FileSessionStore File session store
|
2013-04-05 15:50:53 +00:00
|
|
|
type FileSessionStore struct {
|
|
|
|
sid string
|
|
|
|
lock sync.RWMutex
|
|
|
|
values map[interface{}]interface{}
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:05:56 +00:00
|
|
|
// Set value to file session
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fs *FileSessionStore) Set(key, value interface{}) error {
|
|
|
|
fs.lock.Lock()
|
|
|
|
defer fs.lock.Unlock()
|
|
|
|
fs.values[key] = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:05:56 +00:00
|
|
|
// Get value from file session
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fs *FileSessionStore) Get(key interface{}) interface{} {
|
|
|
|
fs.lock.RLock()
|
|
|
|
defer fs.lock.RUnlock()
|
|
|
|
if v, ok := fs.values[key]; ok {
|
|
|
|
return v
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
return nil
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 17:05:56 +00:00
|
|
|
// Delete value in file session by given key
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fs *FileSessionStore) Delete(key interface{}) error {
|
|
|
|
fs.lock.Lock()
|
|
|
|
defer fs.lock.Unlock()
|
|
|
|
delete(fs.values, key)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// Flush Clean all values in file session
|
2013-09-26 10:07:00 +00:00
|
|
|
func (fs *FileSessionStore) Flush() error {
|
|
|
|
fs.lock.Lock()
|
|
|
|
defer fs.lock.Unlock()
|
|
|
|
fs.values = make(map[interface{}]interface{})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionID Get file session store id
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fs *FileSessionStore) SessionID() string {
|
|
|
|
return fs.sid
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRelease Write file session to local file with Gob string
|
2014-01-05 06:48:36 +00:00
|
|
|
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
|
2014-04-04 00:31:22 +00:00
|
|
|
b, err := EncodeGob(fs.values)
|
2013-05-13 23:33:00 +00:00
|
|
|
if err != nil {
|
2016-03-24 14:43:57 +00:00
|
|
|
SLogger.Println(err)
|
2013-05-13 23:33:00 +00:00
|
|
|
return
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
2014-08-18 06:42:17 +00:00
|
|
|
_, err = os.Stat(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
|
|
|
|
var f *os.File
|
|
|
|
if err == nil {
|
|
|
|
f, err = os.OpenFile(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid), os.O_RDWR, 0777)
|
2016-03-25 02:48:59 +00:00
|
|
|
SLogger.Println(err)
|
2014-08-18 06:42:17 +00:00
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
f, err = os.Create(path.Join(filepder.savePath, string(fs.sid[0]), string(fs.sid[1]), fs.sid))
|
2016-03-25 02:48:59 +00:00
|
|
|
SLogger.Println(err)
|
2014-08-18 06:42:17 +00:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.Truncate(0)
|
|
|
|
f.Seek(0, 0)
|
|
|
|
f.Write(b)
|
|
|
|
f.Close()
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// FileProvider File session provider
|
2013-04-05 15:50:53 +00:00
|
|
|
type FileProvider struct {
|
2014-02-27 13:34:38 +00:00
|
|
|
lock sync.RWMutex
|
2013-04-05 15:50:53 +00:00
|
|
|
maxlifetime int64
|
|
|
|
savePath string
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionInit Init file session provider.
|
2014-01-28 17:05:56 +00:00
|
|
|
// savePath sets the session files path.
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fp *FileProvider) SessionInit(maxlifetime int64, savePath string) error {
|
|
|
|
fp.maxlifetime = maxlifetime
|
|
|
|
fp.savePath = savePath
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRead Read file session by sid.
|
2014-01-28 17:05:56 +00:00
|
|
|
// if file is not exist, create it.
|
|
|
|
// the file path is generated from sid string.
|
2015-09-12 14:53:55 +00:00
|
|
|
func (fp *FileProvider) SessionRead(sid string) (Store, error) {
|
2014-02-27 13:34:38 +00:00
|
|
|
filepder.lock.Lock()
|
|
|
|
defer filepder.lock.Unlock()
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
err := os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
|
|
|
|
if err != nil {
|
2016-03-24 14:43:57 +00:00
|
|
|
SLogger.Println(err.Error())
|
2013-04-05 15:50:53 +00:00
|
|
|
}
|
|
|
|
_, err = os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
var f *os.File
|
|
|
|
if err == nil {
|
|
|
|
f, err = os.OpenFile(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), os.O_RDWR, 0777)
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
f, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
|
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(b) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
2014-04-04 00:31:22 +00:00
|
|
|
kv, err = DecodeGob(b)
|
2013-04-05 15:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Close()
|
2014-08-18 06:42:17 +00:00
|
|
|
ss := &FileSessionStore{sid: sid, values: kv}
|
2013-04-05 15:50:53 +00:00
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionExist Check file session exist.
|
2014-01-28 17:05:56 +00:00
|
|
|
// it checkes the file named from sid exist or not.
|
2013-11-05 14:23:48 +00:00
|
|
|
func (fp *FileProvider) SessionExist(sid string) bool {
|
2014-02-27 13:34:38 +00:00
|
|
|
filepder.lock.Lock()
|
|
|
|
defer filepder.lock.Unlock()
|
|
|
|
|
2013-11-05 14:23:48 +00:00
|
|
|
_, err := os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
if err == nil {
|
|
|
|
return true
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
return false
|
2013-11-05 14:23:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionDestroy Remove all files in this save path
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fp *FileProvider) SessionDestroy(sid string) error {
|
2014-02-27 13:34:38 +00:00
|
|
|
filepder.lock.Lock()
|
|
|
|
defer filepder.lock.Unlock()
|
2014-02-13 10:24:05 +00:00
|
|
|
os.Remove(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
2013-04-05 15:50:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionGC Recycle files in save path
|
2013-04-05 15:50:53 +00:00
|
|
|
func (fp *FileProvider) SessionGC() {
|
2014-02-27 13:34:38 +00:00
|
|
|
filepder.lock.Lock()
|
|
|
|
defer filepder.lock.Unlock()
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
gcmaxlifetime = fp.maxlifetime
|
|
|
|
filepath.Walk(fp.savePath, gcpath)
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionAll Get active file session number.
|
2014-01-28 17:05:56 +00:00
|
|
|
// it walks save path to count files.
|
2013-11-01 16:16:10 +00:00
|
|
|
func (fp *FileProvider) SessionAll() int {
|
|
|
|
a := &activeSession{}
|
|
|
|
err := filepath.Walk(fp.savePath, func(path string, f os.FileInfo, err error) error {
|
|
|
|
return a.visit(path, f, err)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-03-24 14:43:57 +00:00
|
|
|
SLogger.Printf("filepath.Walk() returned %v\n", err)
|
2013-11-01 16:16:10 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return a.total
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
// SessionRegenerate Generate new sid for file session.
|
2014-01-28 17:05:56 +00:00
|
|
|
// it delete old file and create new file named from new sid.
|
2015-09-12 14:53:55 +00:00
|
|
|
func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (Store, error) {
|
2014-02-27 13:34:38 +00:00
|
|
|
filepder.lock.Lock()
|
|
|
|
defer filepder.lock.Unlock()
|
|
|
|
|
2013-09-26 10:07:00 +00:00
|
|
|
err := os.MkdirAll(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])), 0777)
|
|
|
|
if err != nil {
|
2016-03-24 14:43:57 +00:00
|
|
|
SLogger.Println(err.Error())
|
2013-09-26 10:07:00 +00:00
|
|
|
}
|
|
|
|
err = os.MkdirAll(path.Join(fp.savePath, string(sid[0]), string(sid[1])), 0777)
|
|
|
|
if err != nil {
|
2016-03-24 14:43:57 +00:00
|
|
|
SLogger.Println(err.Error())
|
2013-09-26 10:07:00 +00:00
|
|
|
}
|
|
|
|
_, err = os.Stat(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
var newf *os.File
|
|
|
|
if err == nil {
|
|
|
|
return nil, errors.New("newsid exist")
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
newf, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]), oldsid))
|
|
|
|
var f *os.File
|
|
|
|
if err == nil {
|
|
|
|
f, err = os.OpenFile(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1]), oldsid), os.O_RDWR, 0777)
|
|
|
|
io.Copy(newf, f)
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
newf, err = os.Create(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid))
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
os.Remove(path.Join(fp.savePath, string(oldsid[0]), string(oldsid[1])))
|
|
|
|
os.Chtimes(path.Join(fp.savePath, string(sid[0]), string(sid[1]), sid), time.Now(), time.Now())
|
|
|
|
var kv map[interface{}]interface{}
|
|
|
|
b, err := ioutil.ReadAll(newf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(b) == 0 {
|
|
|
|
kv = make(map[interface{}]interface{})
|
|
|
|
} else {
|
2014-04-04 00:31:22 +00:00
|
|
|
kv, err = DecodeGob(b)
|
2013-09-26 10:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-08-18 06:42:17 +00:00
|
|
|
ss := &FileSessionStore{sid: sid, values: kv}
|
2013-09-26 10:07:00 +00:00
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
|
2014-01-28 17:05:56 +00:00
|
|
|
// remove file in save path if expired
|
2013-04-05 15:50:53 +00:00
|
|
|
func gcpath(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if (info.ModTime().Unix() + gcmaxlifetime) < time.Now().Unix() {
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-11-01 16:16:10 +00:00
|
|
|
type activeSession struct {
|
|
|
|
total int
|
|
|
|
}
|
|
|
|
|
2015-09-12 14:53:55 +00:00
|
|
|
func (as *activeSession) visit(paths string, f os.FileInfo, err error) error {
|
2013-11-01 16:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if f.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2015-09-12 14:53:55 +00:00
|
|
|
as.total = as.total + 1
|
2013-11-01 16:16:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-04-05 15:50:53 +00:00
|
|
|
func init() {
|
|
|
|
Register("file", filepder)
|
|
|
|
}
|