本文整理汇总了Golang中net/http.Cookie.String方法的典型用法代码示例。如果您正苦于以下问题:Golang Cookie.String方法的具体用法?Golang Cookie.String怎么用?Golang Cookie.String使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Cookie
的用法示例。
在下文中一共展示了Cookie.String方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getSession
// Get the session from the cookie in the request
// Creates a new session and cookie if none can be found
func getSession(w http.ResponseWriter, req *http.Request) (*Session, bool) {
cookie, err := req.Cookie("id")
if err == nil {
s, ok := sessionSync(cookie.Value)
if ok {
return s, ok
}
log.Println("Invalid session cookie presented: ", cookie.Value)
}
// Create a session
s := newSession()
config.Debug("Creating new session ", s.id)
sessions.Create(s)
// Write the session to the HTTP response
newcookie := http.Cookie{
Name: "id",
Value: s.id,
Path: "/",
MaxAge: math.MaxInt32,
HttpOnly: true}
w.Header().Add("Set-Cookie", newcookie.String())
s.mutex.Lock()
return s, true
}
示例2: login
//User login
func (uc UsersController) login(request *restful.Request,
response *restful.Response) {
loginCredentials := new(UserLoginCredentials)
err := request.ReadEntity(loginCredentials)
if err != nil {
LogError(request, response, err)
WriteIllegalRequestError(response)
return
}
cookieAuth, err := new(UserManager).Login(loginCredentials)
if err != nil {
LogError(request, response, err)
WriteError(err, response)
return
}
//Create an Auth cookie
authCookie := http.Cookie{
Name: "AuthSession",
Value: cookieAuth.AuthToken,
Path: "/",
HttpOnly: true,
}
//Create a CSRF cookie for this session
//Subsequent requests must include this in a header field
//X-Csrf-Token
csrfCookie := http.Cookie{
Name: "CsrfToken",
Value: util.GenHashString(cookieAuth.AuthToken),
Path: "/",
HttpOnly: false,
}
response.AddHeader("Set-Cookie", authCookie.String())
response.AddHeader("Set-Cookie", csrfCookie.String())
response.WriteEntity(BooleanResponse{Success: true})
}
示例3: login
func (this *loginController) login(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "text/html")
responseWriter := util.GetResponseWriter(w, req)
defer responseWriter.Close()
if req.Method == "POST" {
email := req.FormValue("email")
password := req.FormValue("password")
member, err := models.GetMember(email, password)
if err != nil {
responseWriter.Write([]byte(err.Error()))
return
}
session, err := models.CreateSession(member)
if err != nil {
responseWriter.Write([]byte(err.Error()))
return
}
var cookie http.Cookie
cookie.Name = "sessionId"
cookie.Value = session.SessionId()
responseWriter.Header().Add("Set-Cookie", cookie.String())
}
vm := viewmodels.GetLogin()
this.template.Execute(responseWriter, vm)
}
示例4: logout
//User logout
func (uc UsersController) logout(request *restful.Request,
response *restful.Response) {
token := func() string {
for _, cookie := range request.Request.Cookies() {
if cookie.Name == "AuthSession" {
return cookie.Value
}
}
return ""
}()
err := new(UserManager).Logout(token)
if err != nil {
LogError(request, response, err)
WriteError(err, response)
return
}
//Because CouchDB doesn't actually destroy the session,
//best we can do is clear the cookie in the browser.
//This is apparently "not a bug" :|
//http://webmail.dev411.com/t/couchdb/dev/141xwf5vb0/jira-created-couchdb-2042-session-not-cleared-after-delete-session-cookie-auth
theCookie := http.Cookie{
Name: "AuthSession",
Value: "",
Path: "/",
HttpOnly: true,
}
response.AddHeader("Set-Cookie", theCookie.String())
response.WriteEntity(BooleanResponse{Success: true})
}
示例5: login
func (h *homeController) login(w http.ResponseWriter, req *http.Request) {
responseWriter := util.GetResponseWriter(w, req)
defer responseWriter.Close()
w.Header().Add("Content Type", "text/html")
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)
log.Printf("create session err: %v", err)
if err == nil {
var cookie http.Cookie
cookie.Name = "sessionId"
cookie.Value = session.SessionId()
responseWriter.Header().Add("Set-Cookie", cookie.String())
}
} else {
log.Print("User not found!")
}
}
vm := viewmodels.GetLogin()
h.loginTemplate.Execute(responseWriter, vm)
}
示例6: commitSession
func commitSession(headers Headers, env Env, key, secret, domain string) {
cookie := new(http.Cookie)
cookie.Name = key
cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
cookie.Domain = domain
headers.Add("Set-Cookie", cookie.String())
}
示例7: setStickyCookieBackend
func setStickyCookieBackend(res *http.Response, backend string, cookieKey [32]byte) {
cookie := http.Cookie{
Name: stickyCookie,
Value: base64.StdEncoding.EncodeToString(encrypt([]byte(backend), cookieKey)),
Path: "/",
}
res.Header.Add("Set-Cookie", cookie.String())
}
示例8: DeleteCookie
func (s *Session) DeleteCookie(options cookieOptions, response http.ResponseWriter) {
cookie := http.Cookie{
Name: options.Name,
Value: "-",
MaxAge: -1,
}
response.Header().Set("Set-Cookie", cookie.String())
}
示例9: clearAllCookies
func clearAllCookies(rw http.ResponseWriter, req *http.Request) {
for _, ck := range req.Cookies() {
ck2 := http.Cookie{
Name: ck.Name,
MaxAge: -1,
}
rw.Header().Add("Set-Cookie", ck2.String())
}
}
示例10: AddCsrfCookie
func AddCsrfCookie(rw http.ResponseWriter, sessToken string) {
csrfCookie := http.Cookie{
Name: "CsrfToken",
Value: util.GenHashString(sessToken),
Path: "/",
HttpOnly: false,
}
rw.Header().Add("Set-Cookie", csrfCookie.String())
}
示例11: SetCookie
// set cookie
// args: name, value, max age, path, domain, secure, http only, expires
func (ctx *Context) SetCookie(name string, value string, others ...interface{}) {
cookie := http.Cookie{}
cookie.Name = name
cookie.Value = url.QueryEscape(value)
if len(others) > 0 {
switch v := others[0].(type) {
case int:
cookie.MaxAge = v
case int64:
cookie.MaxAge = int(v)
case int32:
cookie.MaxAge = int(v)
}
}
cookie.Path = "/"
if len(others) > 1 {
if v, ok := others[1].(string); ok && len(v) > 0 {
cookie.Path = v
}
}
if len(others) > 2 {
if v, ok := others[2].(string); ok && len(v) > 0 {
cookie.Domain = v
}
}
if len(others) > 3 {
switch v := others[3].(type) {
case bool:
cookie.Secure = v
default:
if others[3] != nil {
cookie.Secure = true
}
}
}
if len(others) > 4 {
if v, ok := others[4].(bool); ok && v {
cookie.HttpOnly = true
}
}
if len(others) > 5 {
if v, ok := others[5].(time.Time); ok {
cookie.Expires = v
cookie.RawExpires = v.Format(time.UnixDate)
}
}
ctx.w.Header().Add("Set-Cookie", cookie.String())
}
示例12: SetCookie
// Duration is the cookie time-to-live in seconds (0 = forever).
func (ctx *Context) SetCookie(name, value string, age int64) {
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)
}
cookie := http.Cookie{Name: name, Value: value, Expires: utctime, Path: "/"}
ctx.Header().Add("Set-Cookie", cookie.String())
}
示例13: commitSession
func commitSession(headers Headers, env Env, key, secret string, newValue string, options *CookieOptions) {
cookie := new(http.Cookie)
cookie.Name = key
cookie.Value = newValue
cookie.Path = options.Path
cookie.Domain = options.Domain
cookie.MaxAge = options.MaxAge
cookie.Secure = options.Secure
cookie.HttpOnly = options.HttpOnly
headers.Add("Set-Cookie", cookie.String())
}
示例14: commitSession
func commitSession(headers Headers, env Env, key, secret string, options *CookieOptions) {
cookie := new(http.Cookie)
cookie.Name = key
cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
cookie.Path = options.Path
cookie.Domain = options.Domain
cookie.MaxAge = options.MaxAge
cookie.Secure = options.Secure
cookie.HttpOnly = options.HttpOnly
headers.Add("Set-Cookie", cookie.String())
}
示例15: WriteCookie
func (s *Session) WriteCookie(options cookieOptions, response http.ResponseWriter) {
cookie := http.Cookie{
Name: options.Name,
Value: s.ID,
Expires: time.Now().Add(options.MaxAge),
HttpOnly: options.HttpOnly,
Secure: options.Secure,
}
response.Header().Set("Set-Cookie", cookie.String())
}