当前位置: 首页>>代码示例>>Golang>>正文


Golang Cookie.Expires方法代码示例

本文整理汇总了Golang中net/http.Cookie.Expires方法的典型用法代码示例。如果您正苦于以下问题:Golang Cookie.Expires方法的具体用法?Golang Cookie.Expires怎么用?Golang Cookie.Expires使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net/http.Cookie的用法示例。


在下文中一共展示了Cookie.Expires方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parse

// parse parses a Set-Cookie header into a cookie.
// It supports space in name unlike net/http.
// Returns nil if invalid.
func parse(s string) *http.Cookie {
	var c http.Cookie
	for i, field := range strings.Split(s, ";") {
		if len(field) == 0 {
			continue
		}
		nv := strings.SplitN(field, "=", 2)
		name := strings.TrimSpace(nv[0])
		value := ""
		if len(nv) > 1 {
			value = strings.TrimSpace(nv[1])
		}
		if i == 0 {
			if len(nv) != 2 {
				continue
			}
			c.Name = name
			c.Value = value
			continue
		}
		switch strings.ToLower(name) {
		case "secure":
			c.Secure = true
		case "httponly":
			c.HttpOnly = true
		case "domain":
			c.Domain = value
		case "max-age":
			secs, err := strconv.Atoi(value)
			if err != nil || secs != 0 && value[0] == '0' {
				continue
			}
			if secs <= 0 {
				c.MaxAge = -1
			} else {
				c.MaxAge = secs
			}
		case "expires":
			exptime, err := time.Parse(time.RFC1123, value)
			if err != nil {
				exptime, err = time.Parse("Mon, 02-Jan-2006 15:04:05 MST", value)
				if err != nil {
					c.Expires = time.Time{}
					continue
				}
			}
			c.Expires = exptime.UTC()
		case "path":
			c.Path = value
		}
	}
	if c.Name == "" {
		return nil
	}
	return &c
}
开发者ID:jhautefeuille,项目名称:misc,代码行数:59,代码来源:spacecookies.go

示例2: login

func (this *homeController) login(w http.ResponseWriter, req *http.Request) {

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	responseWriter.Header().Add("Content-Type", "text/html")

	vm := viewmodels.GetLogin()

	if req.FormValue("submit") == "signup" {
		http.Redirect(w, req, "/signup", http.StatusFound)
	} else {

		if req.Method == "POST" {

			email := req.FormValue("email")
			password := req.FormValue("password")

			member, err := models.GetMember(email, password)

			if err == nil {
				session, err := models.CreateSession(member)

				if err == nil {
					var cookie http.Cookie
					cookie.Name = "goSessionId"
					cookie.Expires = time.Now().Add(10 * time.Minute)
					cookie.Value = strconv.Itoa(session.MemberId())
					responseWriter.Header().Add("Set-Cookie", cookie.String())

					var cookie2 http.Cookie
					cookie2.Name = "loggedName"
					cookie2.Expires = time.Now().Add(10 * time.Minute)
					cookie2.Value = member.FirstName()
					responseWriter.Header().Add("Set-Cookie", cookie2.String())
				}
				vmh := viewmodels.GetHome()

				vmh.LoggedIn = true
				vmh.LoggedName = member.FirstName()

				this.template.Execute(responseWriter, vmh)
			} else {
				this.loginTemplate.Execute(responseWriter, vm)
			}
		} else {
			this.loginTemplate.Execute(responseWriter, vm)
		}
	}
}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:50,代码来源:home.go

示例3: CreateCookie

func CreateCookie(u string, db *sql.DB, persistent bool, app bool) (http.Cookie, error) {
	alphaNum := []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv")
	randValue := ""
	for i := 0; i < 32; i++ {
		num, err := RandKey(58)
		if err != nil {
			return http.Cookie{}, err
		}
		randValue = randValue + string(alphaNum[num])
	}

	authCookie := http.Cookie{
		Name:     "5sur",
		Value:    randValue,
		Path:     "/",
		Domain:   "5sur.com", // Add domain name in the future
		Secure:   true,       // SSL only
		HttpOnly: true,       // HTTP(S) only
	}

	if persistent {
		authCookie.Expires = time.Now().AddDate(0, 1, 0) // One month from now
	} else {
		// Session cookie
	}

	err := updateSession(randValue, u, db, app)
	if err != nil {
		return http.Cookie{}, err
	}
	return authCookie, nil
}
开发者ID:ErikBrown,项目名称:5sur,代码行数:32,代码来源:cookie.go

示例4: createBlankCookie

func createBlankCookie(key string) *http.Cookie {
	cookie := new(http.Cookie)
	cookie.Name = key
	cookie.Value = ""
	l, _ := time.LoadLocation("UTC")
	cookie.Expires = time.Date(1970, time.January, 1, 0, 0, 0, 0, l)
	cookie.HttpOnly = false
	cookie.Path = "/" // Without this ang js cant read cookies we send
	return cookie
}
开发者ID:LDCS,项目名称:monim,代码行数:10,代码来源:monim.go

示例5: AddSignedCookie

// AddSignedCookie adds the specified cookie to the response and also adds an
// additional 'signed' cookie that is used to validate the cookies value when
// SignedCookie is called.
func (c *Context) AddSignedCookie(cookie *http.Cookie) (*http.Cookie, error) {

	// make the signed cookie
	signedCookie := new(http.Cookie)

	// copy the cookie settings
	signedCookie.Path = cookie.Path
	signedCookie.Domain = cookie.Domain
	signedCookie.RawExpires = cookie.RawExpires
	signedCookie.Expires = cookie.Expires
	signedCookie.MaxAge = cookie.MaxAge
	signedCookie.Secure = cookie.Secure
	signedCookie.HttpOnly = cookie.HttpOnly
	signedCookie.Raw = cookie.Raw

	// set the signed cookie specifics
	signedCookie.Name = toSignedCookieName(cookie.Name)
	signedCookie.Value = Hash(cookie.Value)

	// add the cookies
	http.SetCookie(c.ResponseWriter, cookie)
	http.SetCookie(c.ResponseWriter, signedCookie)

	// return the new signed cookie (and no error)
	return signedCookie, nil

}
开发者ID:extrame,项目名称:goweb,代码行数:30,代码来源:cookies.go

示例6: handler

// Answers to /sso, returns the SSO cookie and redirects to /startpage
func handler(w http.ResponseWriter, r *http.Request) {
	re := regexp.MustCompile("CN=([ 0-9A-Za-z_]+)")

	user := r.Header.Get("Certificate-User")
	if user == "" {
		log.Panicf("Did not get user!")
	}

	match := re.FindStringSubmatch(user)

	if len(match) != 2 {
		log.Panicf("No CN found!")
	}

	cn := match[1]

	sessionid := getSessionID(cn, r.Header.Get("X-Forwarded-For"))
	token := generate_session_token(sessionid, cn)
	signature := sign_session_token(token)

	cookie := http.Cookie{}
	cookie.Name = "PLAY_SESSION"
	cookie.Value = signature + "-" + token
	cookie.Path = "/"
	cookie.Domain = external_host
	cookie.Expires = time.Now().Add(356 * 24 * time.Hour)
	cookie.HttpOnly = true
	http.SetCookie(w, &cookie)

	http.Redirect(w, r, "/startpage", http.StatusFound)
}
开发者ID:mikkolehtisalo,项目名称:grl-authen,代码行数:32,代码来源:grl-authen.go

示例7: writeToResponse

func (ss signedCookieSessionHandler) writeToResponse(s SignedCookieSession, resp goanna.Response) {
	bytes, err := s.sessionData.Unmarshal()
	if err != nil {
		log.Println(err.Error())
	}

	cookie := http.Cookie{
		Name:     ss.CookieName,
		Value:    base64.URLEncoding.EncodeToString(bytes),
		HttpOnly: true,
		Secure:   ss.Secure,
		Path:     "/",
	}
	ss.CookieSigner.EncodeCookie(&cookie)

	maxage := int(s.MaxAge() / time.Second)
	if maxage != 0 {
		if s.preferExpires {
			cookie.Expires = time.Now().Add(s.MaxAge())
		} else {
			cookie.MaxAge = maxage
		}
	}
	resp.SetCookie(cookie)
}
开发者ID:99designs,项目名称:goodies,代码行数:25,代码来源:signed_cookie.go

示例8: signup

func (this *homeController) signup(w http.ResponseWriter, req *http.Request) {

	responseWriter := util.GetResponseWriter(w, req)
	defer responseWriter.Close()

	responseWriter.Header().Add("Content-Type", "text/html")

	if req.Method == "POST" {
		firstName := req.FormValue("firstName")
		email := req.FormValue("email")
		password := req.FormValue("password")

		err := models.InsertMember(firstName, email, password)

		if err == nil {
			member, _ := models.GetMember(email, password)

			member, errr := models.GetMember(email, password)

			if errr == nil {
				session, err := models.CreateSession(member)

				if err == nil {
					var cookie http.Cookie
					cookie.Name = "goSessionId"
					cookie.Expires = time.Now().Add(10 * time.Minute)
					cookie.Value = strconv.Itoa(session.MemberId())
					responseWriter.Header().Add("Set-Cookie", cookie.String())

					var cookie2 http.Cookie
					cookie2.Name = "loggedName"
					cookie2.Expires = time.Now().Add(10 * time.Minute)
					cookie2.Value = member.FirstName()
					responseWriter.Header().Add("Set-Cookie", cookie2.String())
				}

				http.Redirect(w, req, "/home", http.StatusFound)
			}
		}
	}

	vm := viewmodels.GetSignup()

	this.signupTemplate.Execute(responseWriter, vm)

}
开发者ID:Ikanant,项目名称:GolangWebApp,代码行数:46,代码来源:home.go

示例9: SetCookieVal

func SetCookieVal(w http.ResponseWriter, name string, value string, path string) {
	var C http.Cookie
	C.Name = name
	C.Value = value
	C.Path = path
	C.Expires = time.Now().AddDate(0, 1, 0)
	http.SetCookie(w, &C)
}
开发者ID:nymoral,项目名称:gothere,代码行数:8,代码来源:cookies.go

示例10: ClearCookie

func (r *OkResponse) ClearCookie(name string) {
	c := http.Cookie{}
	c.Name = name
	c.Value = "none"
	c.MaxAge = -1
	c.Expires = time.Unix(1, 0)
	r.SetCookie(c)
}
开发者ID:vinays,项目名称:goodies,代码行数:8,代码来源:response.go

示例11: createCookie

func (s *FormsAuthSuite) createCookie(token string) http.Cookie {
	cookie := http.Cookie{}
	cookie.Name = FormsAuthenticatorDefaultCookieName
	cookie.Expires = time.Now().UTC().Add(24 * 365 * time.Hour)
	cookie.HttpOnly = true
	cookie.Path = "/"
	cookie.Value = token
	return cookie
}
开发者ID:activegiver,项目名称:logingo,代码行数:9,代码来源:forms_auth_test.go

示例12: createAuthCookie

func (auth *FormsAuthenticator) createAuthCookie(jwtString string) *http.Cookie {
	cookie := http.Cookie{}
	cookie.Name = auth.CookieName
	cookie.Expires = auth.getCookieExpirationTime()
	cookie.HttpOnly = true
	cookie.Path = "/"
	cookie.Value = jwtString
	return &cookie
}
开发者ID:activegiver,项目名称:logingo,代码行数:9,代码来源:forms_auth.go

示例13: newCookie

func newCookie(r *http.Request, session *Session) *http.Cookie {
	cookie := new(http.Cookie)
	cookie.Name = "UH"
	cookie.Value = session.Id
	cookie.Expires = session.Expires
	cookie.Path = "/"
	cookie.Domain = r.URL.Host
	return cookie
}
开发者ID:Joinhack,项目名称:gae_blog,代码行数:9,代码来源:main.go

示例14: cookie

func cookie(name, value string, opt *Options) *http.Cookie {
	c := http.Cookie{
		Name:     name,
		Value:    value,
		Path:     opt.Path,
		Domain:   opt.Domain,
		MaxAge:   opt.MaxAge,
		Secure:   opt.Secure,
		HttpOnly: opt.HttpOnly,
	}
	switch {
	case c.MaxAge < 0:
		c.Expires = time.Unix(1, 0)
	case c.MaxAge > 0:
		c.Expires = time.Now().Add(time.Duration(c.MaxAge) * time.Second)
	}
	return &c
}
开发者ID:oov,项目名称:gothic,代码行数:18,代码来源:gothic.go

示例15: createCookie

func (state *SessionCookieState) createCookie(session *Session) *http.Cookie {
	cookie := http.Cookie{}
	cookie.Name = state.SessionKey
	cookie.Value = session.Id
	cookie.Path = state.Path
	cookie.Domain = state.Domain
	cookie.Secure = state.Secure
	cookie.HttpOnly = state.HttpOnly

	if session.IsExpired() {
		cookie.Expires = time.Now()
	} else {
		if state.Expires != 0 {
			cookie.Expires = time.Now().Add(state.Expires)
		}
	}

	return &cookie
}
开发者ID:felipeweb,项目名称:httpapp,代码行数:19,代码来源:cookiestate.go


注:本文中的net/http.Cookie.Expires方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。