mirror of
https://github.com/astaxie/beego.git
synced 2025-07-02 14:20:19 +00:00
added FlashName,FlashSeperator, & Tests
This commit is contained in:
17
flash.go
17
flash.go
@ -6,9 +6,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// the separation string when encoding flash data.
|
||||
const BEEGO_FLASH_SEP = "#BEEGOFLASH#"
|
||||
|
||||
// FlashData is a tools to maintain data when using across request.
|
||||
type FlashData struct {
|
||||
Data map[string]string
|
||||
@ -54,29 +51,27 @@ func (fd *FlashData) Store(c *Controller) {
|
||||
c.Data["flash"] = fd.Data
|
||||
var flashValue string
|
||||
for key, value := range fd.Data {
|
||||
flashValue += "\x00" + key + BEEGO_FLASH_SEP + value + "\x00"
|
||||
flashValue += "\x00" + key + "\x23" + FlashSeperator + "\x23" + value + "\x00"
|
||||
}
|
||||
c.Ctx.SetCookie("BEEGO_FLASH", url.QueryEscape(flashValue), 0, "/")
|
||||
c.Ctx.SetCookie(FlashName, url.QueryEscape(flashValue), 0, "/")
|
||||
}
|
||||
|
||||
// ReadFromRequest parsed flash data from encoded values in cookie.
|
||||
func ReadFromRequest(c *Controller) *FlashData {
|
||||
flash := &FlashData{
|
||||
Data: make(map[string]string),
|
||||
}
|
||||
if cookie, err := c.Ctx.Request.Cookie("BEEGO_FLASH"); err == nil {
|
||||
flash := NewFlash()
|
||||
if cookie, err := c.Ctx.Request.Cookie(FlashName); err == nil {
|
||||
v, _ := url.QueryUnescape(cookie.Value)
|
||||
vals := strings.Split(v, "\x00")
|
||||
for _, v := range vals {
|
||||
if len(v) > 0 {
|
||||
kv := strings.Split(v, BEEGO_FLASH_SEP)
|
||||
kv := strings.Split(v, FlashSeperator)
|
||||
if len(kv) == 2 {
|
||||
flash.Data[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
//read one time then delete it
|
||||
c.Ctx.SetCookie("BEEGO_FLASH", "", -1, "/")
|
||||
c.Ctx.SetCookie(FlashName, "", -1, "/")
|
||||
}
|
||||
c.Data["flash"] = flash.Data
|
||||
return flash
|
||||
|
Reference in New Issue
Block a user