gitea源码

public.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package public
  4. import (
  5. "bytes"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/modules/assetfs"
  13. "code.gitea.io/gitea/modules/container"
  14. "code.gitea.io/gitea/modules/httpcache"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/util"
  18. )
  19. func CustomAssets() *assetfs.Layer {
  20. return assetfs.Local("custom", setting.CustomPath, "public")
  21. }
  22. func AssetFS() *assetfs.LayeredFS {
  23. return assetfs.Layered(CustomAssets(), BuiltinAssets())
  24. }
  25. // FileHandlerFunc implements the static handler for serving files in "public" assets
  26. func FileHandlerFunc() http.HandlerFunc {
  27. assetFS := AssetFS()
  28. return func(resp http.ResponseWriter, req *http.Request) {
  29. if req.Method != "GET" && req.Method != "HEAD" {
  30. resp.WriteHeader(http.StatusMethodNotAllowed)
  31. return
  32. }
  33. handleRequest(resp, req, assetFS, req.URL.Path)
  34. }
  35. }
  36. // parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
  37. func parseAcceptEncoding(val string) container.Set[string] {
  38. parts := strings.Split(val, ";")
  39. types := make(container.Set[string])
  40. for v := range strings.SplitSeq(parts[0], ",") {
  41. types.Add(strings.TrimSpace(v))
  42. }
  43. return types
  44. }
  45. // setWellKnownContentType will set the Content-Type if the file is a well-known type.
  46. // See the comments of detectWellKnownMimeType
  47. func setWellKnownContentType(w http.ResponseWriter, file string) {
  48. mimeType := detectWellKnownMimeType(path.Ext(file))
  49. if mimeType != "" {
  50. w.Header().Set("Content-Type", mimeType)
  51. }
  52. }
  53. func handleRequest(w http.ResponseWriter, req *http.Request, fs http.FileSystem, file string) {
  54. // actually, fs (http.FileSystem) is designed to be a safe interface, relative paths won't bypass its parent directory, it's also fine to do a clean here
  55. f, err := fs.Open(util.PathJoinRelX(file))
  56. if err != nil {
  57. if os.IsNotExist(err) {
  58. w.WriteHeader(http.StatusNotFound)
  59. return
  60. }
  61. w.WriteHeader(http.StatusInternalServerError)
  62. log.Error("[Static] Open %q failed: %v", file, err)
  63. return
  64. }
  65. defer f.Close()
  66. fi, err := f.Stat()
  67. if err != nil {
  68. w.WriteHeader(http.StatusInternalServerError)
  69. log.Error("[Static] %q exists, but fails to open: %v", file, err)
  70. return
  71. }
  72. // need to serve index file? (no at the moment)
  73. if fi.IsDir() {
  74. w.WriteHeader(http.StatusNotFound)
  75. return
  76. }
  77. servePublicAsset(w, req, fi, fi.ModTime(), f)
  78. }
  79. // servePublicAsset serve http content
  80. func servePublicAsset(w http.ResponseWriter, req *http.Request, fi os.FileInfo, modtime time.Time, content io.ReadSeeker) {
  81. setWellKnownContentType(w, fi.Name())
  82. httpcache.SetCacheControlInHeader(w.Header(), httpcache.CacheControlForPublicStatic())
  83. encodings := parseAcceptEncoding(req.Header.Get("Accept-Encoding"))
  84. fiEmbedded, _ := fi.(assetfs.EmbeddedFileInfo)
  85. if encodings.Contains("gzip") && fiEmbedded != nil {
  86. // try to provide gzip content directly from bindata
  87. if gzipBytes, ok := fiEmbedded.GetGzipContent(); ok {
  88. rdGzip := bytes.NewReader(gzipBytes)
  89. // all gzipped static files (from bindata) are managed by Gitea, so we can make sure every file has the correct ext name
  90. // then we can get the correct Content-Type, we do not need to do http.DetectContentType on the decompressed data
  91. if w.Header().Get("Content-Type") == "" {
  92. w.Header().Set("Content-Type", "application/octet-stream")
  93. }
  94. w.Header().Set("Content-Encoding", "gzip")
  95. http.ServeContent(w, req, fi.Name(), modtime, rdGzip)
  96. return
  97. }
  98. }
  99. http.ServeContent(w, req, fi.Name(), modtime, content)
  100. }