// NewCookie is a helper method that returns a new http.Cookie object.
// Duration is specified in seconds. If the duration is zero, the cookie is permanent.
// This can be used in conjunction with ctx.SetCookie.
760615
func NewCookie(name string, value string, age int64) *http.Cookie {
	var utctime time.Time
	if age == 0 {
		// 2^31 - 1 seconds (roughly 2038)
		utctime = time.Unix(2147483647, 0)
	} else {
		utctime = time.Unix(time.Now().Unix()+age, 0)
	}
	return &http.Cookie{Name: name, Value: value, Expires: utctime}
}
1025704
func SetUserCookie(w http.ResponseWriter, r *http.Request, user User) {

	cookie := &http.Cookie{
		Name:     Config.CookieName,
		Path:     "/",
		Domain:   r.URL.Host,
		HttpOnly: Config.CookieHttpOnly,
		Secure:   Config.CookieSecure,
	}

	// if not a session cookie set the MaxAge
	if Config.CookieMaxAge > 0 {
		cookie.MaxAge = Config.CookieMaxAge
	}

	SetUserCookieOpts(w, cookie, user)
}
1082465
func (r *Request) WithCookie(k, v string) *Request {
	if r.chain.failed() {
		return r
	}
	r.http.AddCookie(&http.Cookie{
		Name:  k,
		Value: v,
	})
	return r
}
156159
func (p *WebPage) AddCookie(cookie *http.Cookie) (bool, error) {
	var resp struct {
		ReturnValue bool `json:"returnValue"`
	}
	req := map[string]interface{}{"ref": p.ref.id, "cookie": encodeCookieJSON(cookie)}
	if err := p.ref.process.doJSON("POST", "/webpage/AddCookie", req, &resp); err != nil {
		return false, err
	}
	return resp.ReturnValue, nil
}
478410
func (ctx *context) RemoveCookie(name string, options ...CookieOption) {
	c := &http.Cookie{}
	c.Name = name
	c.Value = ""
	c.Path = "/" // if user wants to change it, use of the CookieOption `CookiePath` is required if not `ctx.SetCookie`.
	c.HttpOnly = true
	// RFC says 1 second, but let's do it 1  to make sure is working
	exp := time.Now().Add(-time.Duration(1) * time.Minute)
	c.Expires = exp
	c.MaxAge = -1
	ctx.SetCookie(c, options...)
	// delete request's cookie also, which is temporary available.
	ctx.request.Header.Set("Cookie", "")
}
831186
func (c *cookies) Expire(key string, expire time.Time) {
	ck := c.Get(key)
	if ck != nil {
		ck.Expires = expire
		ck.MaxAge = int(expire.Sub(time.Now()).Seconds())
		c.Set(ck)
	}
}
1030754
func (fn CookieReaderFunc) ReadCookie(r io.Reader) (CookieJar, error) {
	return fn(r)
}
1268526
func RemoveCookie(w http.ResponseWriter, r *http.Request, config Config) {
	c, err := r.Cookie(config.Cookie)
	if err != nil {
		return
	}

	c.Expires = CookieExpireDelete
	// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
	c.MaxAge = -1
	c.Value = ""
	c.Path = "/"
	c.Domain = formatCookieDomain(r.URL.Host, config.DisableSubdomainPersistence)
	AddCookie(w, r, c, config.AllowReclaim)

	if config.AllowReclaim {
		// delete request's cookie also, which is temporary available.
		r.Header.Set("Cookie", "")
	}
}
507820
func (p *WebPage) Cookies() ([]*http.Cookie, error) {
	var resp struct {
		Value []cookieJSON `json:"value"`
	}
	if err := p.ref.process.doJSON("POST", "/webpage/Cookies", map[string]interface{}{"ref": p.ref.id}, &resp); err != nil {
		return nil, err
	}

	a := make([]*http.Cookie, len(resp.Value))
	for i := range resp.Value {
		a[i] = decodeCookieJSON(resp.Value[i])
	}
	return a, nil
}
1009620
func (this *Request) Cookie(n string, i ...interface{}) string {

	cookie, err := this.Request.Cookie(n)

	if err != nil {
		return ""
	}

	/*
	   Unescape the cookie.
	*/
	var v string
	var e error
	v, e = url.QueryUnescape(cookie.Value)
	v, e = Decode(v)

	if e != nil {
		return ""
	}

	/*
	   If no interface was given then just return the Value.
	*/

	if len(i) == 0 {
		return v
	}

	json.Unmarshal([]byte(v), i[0])

	return v
}
562427
func (r *FastHttpHeader) GetCookie(key string) (value revel.ServerCookie, err error) {
	if !r.isResponse {
		var cookie []byte
		if cookie = r.Source.(*FastHttpRequest).Original.Request.Header.Cookie(key); cookie != nil {
			value = FastHttpCookie(cookie)
		} else {
			err = http.ErrNoCookie
		}

	}
	return
}
583142
func (r *FastHttpHeader) SetCookie(cookie string) {
	if r.isResponse {
		r.Source.(*FastHttpResponse).Original.Response.Header.Add("Set-Cookie", cookie)
	}
}
429152
func NewCookie(name string, value string, args ...interface{}) *http.Cookie {
	var (
		alen     = len(args)
		age      int64
		path     string
		domain   string
		secure   bool
		httpOnly bool
	)
	switch alen {
	case 5:
		httpOnly, _ = args[4].(bool)
		fallthrough
	case 4:
		secure, _ = args[3].(bool)
		fallthrough
	case 3:
		domain, _ = args[2].(string)
		fallthrough
	case 2:
		path, _ = args[1].(string)
		fallthrough
	case 1:
		switch args[0].(type) {
		case int:
			age = int64(args[0].(int))
		case int64:
			age = args[0].(int64)
		case time.Duration:
			age = int64(args[0].(time.Duration))
		}
	}
	cookie := &http.Cookie{
		Name:     name,
		Value:    value,
		Path:     path,
		Domain:   domain,
		MaxAge:   0,
		Secure:   secure,
		HttpOnly: httpOnly,
	}
	if age > 0 {
		cookie.Expires = time.Unix(time.Now().Unix()+age, 0)
	} else if age < 0 {
		cookie.Expires = time.Unix(1, 0)
	}
	return cookie
}
691277
func (c CookieOptions) deleteCookie(w http.ResponseWriter) {
	cookie := &http.Cookie{
		// If the browser refuses to delete it, set value to "" so subsequent
		// requests replace it when it does not point to a valid session id.
		Path:     c.Path,
		Domain:   c.Domain,
		Value:    "",
		Name:     c.Name,
		MaxAge:   -1,
		Expires:  time.Now().UTC().AddDate(-1, 0, 0),
		HttpOnly: c.HTTPOnly,
		Secure:   c.Secure,
	}

	w.(cookieWriter).SetCookie(cookie)
}
342297
func (state *UserState) UsernameCookie(req *http.Request) (string, error) {
	username, ok := cookie.SecureCookie(req, "user", state.cookieSecret)
	if ok && (username != "") {
		return username, nil
	}
	return "", ErrCookieGetUsername

}
738989
func (l *listenContext) createCookie(id stack.TransportEndpointID, seq seqnum.Value, data uint32) seqnum.Value {
	ts := timeStamp()
	v := l.cookieHash(id, 0, 0) + uint32(seq) + (ts << tsOffset)
	v += (l.cookieHash(id, ts, 1) + data) & hashMask
	return seqnum.Value(v)
}
255944
func (c *cookie) Expire(w http.ResponseWriter) {
	// to expire cookie set the time in the past
	cookie := http.Cookie{
		Name:     DefaultCookieName,
		Value:    "none",
		HttpOnly: true,
		Path:     "/",
		Expires:  c.Now().Add(-1 * time.Hour),
	}

	http.SetCookie(w, &cookie)
}