ghttp_server_cookie.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package ghttp
  7. import (
  8. "net/http"
  9. "time"
  10. )
  11. // Cookie for HTTP COOKIE management.
  12. type Cookie struct {
  13. data map[string]*cookieItem // Underlying cookie items.
  14. path string // The default cookie path.
  15. domain string // The default cookie domain
  16. maxAge time.Duration // The default cookie max age.
  17. server *Server // Belonged HTTP server
  18. request *Request // Belonged HTTP request.
  19. response *Response // Belonged HTTP response.
  20. }
  21. // cookieItem is the item stored in Cookie.
  22. type cookieItem struct {
  23. *http.Cookie // Underlying cookie items.
  24. FromClient bool // Mark this cookie received from client.
  25. }
  26. // GetCookie creates or retrieves a cookie object with given request.
  27. // It retrieves and returns an existing cookie object if it already exists with given request.
  28. // It creates and returns a new cookie object if it does not exist with given request.
  29. func GetCookie(r *Request) *Cookie {
  30. if r.Cookie != nil {
  31. return r.Cookie
  32. }
  33. return &Cookie{
  34. request: r,
  35. server: r.Server,
  36. }
  37. }
  38. // init does lazy initialization for cookie object.
  39. func (c *Cookie) init() {
  40. if c.data != nil {
  41. return
  42. }
  43. c.data = make(map[string]*cookieItem)
  44. c.path = c.request.Server.GetCookiePath()
  45. c.domain = c.request.Server.GetCookieDomain()
  46. c.maxAge = c.request.Server.GetCookieMaxAge()
  47. c.response = c.request.Response
  48. // DO NOT ADD ANY DEFAULT COOKIE DOMAIN!
  49. //if c.domain == "" {
  50. // c.domain = c.request.GetHost()
  51. //}
  52. for _, v := range c.request.Cookies() {
  53. c.data[v.Name] = &cookieItem{
  54. Cookie: v,
  55. FromClient: true,
  56. }
  57. }
  58. }
  59. // Map returns the cookie items as map[string]string.
  60. func (c *Cookie) Map() map[string]string {
  61. c.init()
  62. m := make(map[string]string)
  63. for k, v := range c.data {
  64. m[k] = v.Value
  65. }
  66. return m
  67. }
  68. // Contains checks if given key exists and not expired in cookie.
  69. func (c *Cookie) Contains(key string) bool {
  70. c.init()
  71. if r, ok := c.data[key]; ok {
  72. if r.Expires.IsZero() || r.Expires.After(time.Now()) {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // Set sets cookie item with default domain, path and expiration age.
  79. func (c *Cookie) Set(key, value string) {
  80. c.SetCookie(key, value, c.domain, c.path, c.maxAge)
  81. }
  82. // SetCookie sets cookie item given given domain, path and expiration age.
  83. // The optional parameter <httpOnly> specifies if the cookie item is only available in HTTP,
  84. // which is usually empty.
  85. func (c *Cookie) SetCookie(key, value, domain, path string, maxAge time.Duration, httpOnly ...bool) {
  86. c.init()
  87. isHttpOnly := false
  88. if len(httpOnly) > 0 {
  89. isHttpOnly = httpOnly[0]
  90. }
  91. httpCookie := &http.Cookie{
  92. Name: key,
  93. Value: value,
  94. Path: path,
  95. Domain: domain,
  96. HttpOnly: isHttpOnly,
  97. }
  98. if maxAge != 0 {
  99. httpCookie.Expires = time.Now().Add(maxAge)
  100. }
  101. c.data[key] = &cookieItem{
  102. Cookie: httpCookie,
  103. }
  104. }
  105. // SetHttpCookie sets cookie with *http.Cookie.
  106. func (c *Cookie) SetHttpCookie(httpCookie *http.Cookie) {
  107. c.init()
  108. c.data[httpCookie.Name] = &cookieItem{
  109. Cookie: httpCookie,
  110. }
  111. }
  112. // GetSessionId retrieves and returns the session id from cookie.
  113. func (c *Cookie) GetSessionId() string {
  114. return c.Get(c.server.GetSessionIdName())
  115. }
  116. // SetSessionId sets session id in the cookie.
  117. func (c *Cookie) SetSessionId(id string) {
  118. c.Set(c.server.GetSessionIdName(), id)
  119. }
  120. // Get retrieves and returns the value with specified key.
  121. // It returns <def> if specified key does not exist and <def> is given.
  122. func (c *Cookie) Get(key string, def ...string) string {
  123. c.init()
  124. if r, ok := c.data[key]; ok {
  125. if r.Expires.IsZero() || r.Expires.After(time.Now()) {
  126. return r.Value
  127. }
  128. }
  129. if len(def) > 0 {
  130. return def[0]
  131. }
  132. return ""
  133. }
  134. // Remove deletes specified key and its value from cookie using default domain and path.
  135. // It actually tells the http client that the cookie is expired, do not send it to server next time.
  136. func (c *Cookie) Remove(key string) {
  137. c.SetCookie(key, "", c.domain, c.path, -86400)
  138. }
  139. // RemoveCookie deletes specified key and its value from cookie using given domain and path.
  140. // It actually tells the http client that the cookie is expired, do not send it to server next time.
  141. func (c *Cookie) RemoveCookie(key, domain, path string) {
  142. c.SetCookie(key, "", domain, path, -86400)
  143. }
  144. // Flush outputs the cookie items to client.
  145. func (c *Cookie) Flush() {
  146. if len(c.data) == 0 {
  147. return
  148. }
  149. for _, v := range c.data {
  150. if v.FromClient {
  151. continue
  152. }
  153. http.SetCookie(c.response.Writer, v.Cookie)
  154. }
  155. }