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.

public.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2016 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 public
  5. import (
  6. "log"
  7. "net/http"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "code.gitea.io/gitea/modules/httpcache"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // Options represents the available options to configure the macaron handler.
  15. type Options struct {
  16. Directory string
  17. IndexFile string
  18. SkipLogging bool
  19. FileSystem http.FileSystem
  20. Prefix string
  21. }
  22. // KnownPublicEntries list all direct children in the `public` directory
  23. var KnownPublicEntries = []string{
  24. "css",
  25. "img",
  26. "js",
  27. "serviceworker.js",
  28. "vendor",
  29. "favicon.ico",
  30. }
  31. // Custom implements the macaron static handler for serving custom assets.
  32. func Custom(opts *Options) func(next http.Handler) http.Handler {
  33. return opts.staticHandler(path.Join(setting.CustomPath, "public"))
  34. }
  35. // staticFileSystem implements http.FileSystem interface.
  36. type staticFileSystem struct {
  37. dir *http.Dir
  38. }
  39. func newStaticFileSystem(directory string) staticFileSystem {
  40. if !filepath.IsAbs(directory) {
  41. directory = filepath.Join(setting.AppWorkPath, directory)
  42. }
  43. dir := http.Dir(directory)
  44. return staticFileSystem{&dir}
  45. }
  46. func (fs staticFileSystem) Open(name string) (http.File, error) {
  47. return fs.dir.Open(name)
  48. }
  49. // StaticHandler sets up a new middleware for serving static files in the
  50. func StaticHandler(dir string, opts *Options) func(next http.Handler) http.Handler {
  51. return opts.staticHandler(dir)
  52. }
  53. func (opts *Options) staticHandler(dir string) func(next http.Handler) http.Handler {
  54. return func(next http.Handler) http.Handler {
  55. // Defaults
  56. if len(opts.IndexFile) == 0 {
  57. opts.IndexFile = "index.html"
  58. }
  59. // Normalize the prefix if provided
  60. if opts.Prefix != "" {
  61. // Ensure we have a leading '/'
  62. if opts.Prefix[0] != '/' {
  63. opts.Prefix = "/" + opts.Prefix
  64. }
  65. // Remove any trailing '/'
  66. opts.Prefix = strings.TrimRight(opts.Prefix, "/")
  67. }
  68. if opts.FileSystem == nil {
  69. opts.FileSystem = newStaticFileSystem(dir)
  70. }
  71. return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
  72. if !opts.handle(w, req, opts) {
  73. next.ServeHTTP(w, req)
  74. }
  75. })
  76. }
  77. }
  78. // parseAcceptEncoding parse Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5 as compress methods
  79. func parseAcceptEncoding(val string) map[string]bool {
  80. parts := strings.Split(val, ";")
  81. var types = make(map[string]bool)
  82. for _, v := range strings.Split(parts[0], ",") {
  83. types[strings.TrimSpace(v)] = true
  84. }
  85. return types
  86. }
  87. func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Options) bool {
  88. if req.Method != "GET" && req.Method != "HEAD" {
  89. return false
  90. }
  91. file := req.URL.Path
  92. // if we have a prefix, filter requests by stripping the prefix
  93. if opt.Prefix != "" {
  94. if !strings.HasPrefix(file, opt.Prefix) {
  95. return false
  96. }
  97. file = file[len(opt.Prefix):]
  98. if file != "" && file[0] != '/' {
  99. return false
  100. }
  101. }
  102. f, err := opt.FileSystem.Open(file)
  103. if err != nil {
  104. // 404 requests to any known entries in `public`
  105. if path.Base(opts.Directory) == "public" {
  106. parts := strings.Split(file, "/")
  107. if len(parts) < 2 {
  108. return false
  109. }
  110. for _, entry := range KnownPublicEntries {
  111. if entry == parts[1] {
  112. w.WriteHeader(404)
  113. return true
  114. }
  115. }
  116. }
  117. return false
  118. }
  119. defer f.Close()
  120. fi, err := f.Stat()
  121. if err != nil {
  122. log.Printf("[Static] %q exists, but fails to open: %v", file, err)
  123. return true
  124. }
  125. // Try to serve index file
  126. if fi.IsDir() {
  127. // Redirect if missing trailing slash.
  128. if !strings.HasSuffix(req.URL.Path, "/") {
  129. http.Redirect(w, req, path.Clean(req.URL.Path+"/"), http.StatusFound)
  130. return true
  131. }
  132. f, err = opt.FileSystem.Open(file)
  133. if err != nil {
  134. return false // Discard error.
  135. }
  136. defer f.Close()
  137. fi, err = f.Stat()
  138. if err != nil || fi.IsDir() {
  139. return false
  140. }
  141. }
  142. if !opt.SkipLogging {
  143. log.Println("[Static] Serving " + file)
  144. }
  145. if httpcache.HandleEtagCache(req, w, fi) {
  146. return true
  147. }
  148. ServeContent(w, req, fi, fi.ModTime(), f)
  149. return true
  150. }