You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httpcache.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package httpcache
  4. import (
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // SetCacheControlInHeader sets suitable cache-control headers in the response
  13. func SetCacheControlInHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
  14. directives := make([]string, 0, 2+len(additionalDirectives))
  15. // "max-age=0 + must-revalidate" (aka "no-cache") is preferred instead of "no-store"
  16. // because browsers may restore some input fields after navigate-back / reload a page.
  17. if setting.IsProd {
  18. if maxAge == 0 {
  19. directives = append(directives, "max-age=0", "private", "must-revalidate")
  20. } else {
  21. directives = append(directives, "private", "max-age="+strconv.Itoa(int(maxAge.Seconds())))
  22. }
  23. } else {
  24. directives = append(directives, "max-age=0", "private", "must-revalidate")
  25. // to remind users they are using non-prod setting.
  26. h.Set("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
  27. }
  28. h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
  29. }
  30. func ServeContentWithCacheControl(w http.ResponseWriter, req *http.Request, name string, modTime time.Time, content io.ReadSeeker) {
  31. SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
  32. http.ServeContent(w, req, name, modTime, content)
  33. }
  34. // HandleGenericETagCache handles ETag-based caching for a HTTP request.
  35. // It returns true if the request was handled.
  36. func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag string) (handled bool) {
  37. if len(etag) > 0 {
  38. w.Header().Set("Etag", etag)
  39. if checkIfNoneMatchIsValid(req, etag) {
  40. w.WriteHeader(http.StatusNotModified)
  41. return true
  42. }
  43. }
  44. SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
  45. return false
  46. }
  47. // checkIfNoneMatchIsValid tests if the header If-None-Match matches the ETag
  48. func checkIfNoneMatchIsValid(req *http.Request, etag string) bool {
  49. ifNoneMatch := req.Header.Get("If-None-Match")
  50. if len(ifNoneMatch) > 0 {
  51. for _, item := range strings.Split(ifNoneMatch, ",") {
  52. item = strings.TrimSpace(item)
  53. if item == etag {
  54. return true
  55. }
  56. }
  57. }
  58. return false
  59. }
  60. // HandleGenericETagTimeCache handles ETag-based caching with Last-Modified caching for a HTTP request.
  61. // It returns true if the request was handled.
  62. func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag string, lastModified *time.Time) (handled bool) {
  63. if len(etag) > 0 {
  64. w.Header().Set("Etag", etag)
  65. }
  66. if lastModified != nil && !lastModified.IsZero() {
  67. w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
  68. }
  69. if len(etag) > 0 {
  70. if checkIfNoneMatchIsValid(req, etag) {
  71. w.WriteHeader(http.StatusNotModified)
  72. return true
  73. }
  74. }
  75. if lastModified != nil && !lastModified.IsZero() {
  76. ifModifiedSince := req.Header.Get("If-Modified-Since")
  77. if ifModifiedSince != "" {
  78. t, err := time.Parse(http.TimeFormat, ifModifiedSince)
  79. if err == nil && lastModified.Unix() <= t.Unix() {
  80. w.WriteHeader(http.StatusNotModified)
  81. return true
  82. }
  83. }
  84. }
  85. SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
  86. return false
  87. }