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 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package httpcache
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "code.gitea.io/gitea/modules/setting"
  14. )
  15. // AddCacheControlToHeader adds suitable cache-control headers to response
  16. func AddCacheControlToHeader(h http.Header, d time.Duration) {
  17. if setting.IsProd() {
  18. h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds())))
  19. } else {
  20. h.Set("Cache-Control", "no-store")
  21. // to remind users they are using non-prod setting.
  22. // some users may be confused by "Cache-Control: no-store" in their setup if they did wrong to `RUN_MODE` in `app.ini`.
  23. h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
  24. h.Add("X-Gitea-Debug", "CacheControl=no-store")
  25. }
  26. }
  27. // generateETag generates an ETag based on size, filename and file modification time
  28. func generateETag(fi os.FileInfo) string {
  29. etag := fmt.Sprint(fi.Size()) + fi.Name() + fi.ModTime().UTC().Format(http.TimeFormat)
  30. return `"` + base64.StdEncoding.EncodeToString([]byte(etag)) + `"`
  31. }
  32. // HandleTimeCache handles time-based caching for a HTTP request
  33. func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
  34. AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
  35. ifModifiedSince := req.Header.Get("If-Modified-Since")
  36. if ifModifiedSince != "" {
  37. t, err := time.Parse(http.TimeFormat, ifModifiedSince)
  38. if err == nil && fi.ModTime().Unix() <= t.Unix() {
  39. w.WriteHeader(http.StatusNotModified)
  40. return true
  41. }
  42. }
  43. w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
  44. return false
  45. }
  46. // HandleFileETagCache handles ETag-based caching for a HTTP request
  47. func HandleFileETagCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
  48. etag := generateETag(fi)
  49. return HandleGenericETagCache(req, w, etag)
  50. }
  51. // HandleGenericETagCache handles ETag-based caching for a HTTP request.
  52. // It returns true if the request was handled.
  53. func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag string) (handled bool) {
  54. if len(etag) > 0 {
  55. w.Header().Set("Etag", etag)
  56. if checkIfNoneMatchIsValid(req, etag) {
  57. w.WriteHeader(http.StatusNotModified)
  58. return true
  59. }
  60. }
  61. AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
  62. return false
  63. }
  64. // checkIfNoneMatchIsValid tests if the header If-None-Match matches the ETag
  65. func checkIfNoneMatchIsValid(req *http.Request, etag string) bool {
  66. ifNoneMatch := req.Header.Get("If-None-Match")
  67. if len(ifNoneMatch) > 0 {
  68. for _, item := range strings.Split(ifNoneMatch, ",") {
  69. item = strings.TrimSpace(item)
  70. if item == etag {
  71. return true
  72. }
  73. }
  74. }
  75. return false
  76. }