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.

static.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2013 The Martini Authors. All rights reserved.
  2. // Copyright 2014 The Gogs Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package middleware
  6. import (
  7. "log"
  8. "net/http"
  9. "path"
  10. "runtime"
  11. "strings"
  12. "github.com/go-martini/martini"
  13. "github.com/gogits/gogs/modules/setting"
  14. )
  15. // StaticOptions is a struct for specifying configuration options for the martini.Static middleware.
  16. type StaticOptions struct {
  17. // Prefix is the optional prefix used to serve the static directory content
  18. Prefix string
  19. // SkipLogging will disable [Static] log messages when a static file is served.
  20. SkipLogging bool
  21. // IndexFile defines which file to serve as index if it exists.
  22. IndexFile string
  23. // Expires defines which user-defined function to use for producing a HTTP Expires Header
  24. // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching
  25. Expires func() string
  26. }
  27. func prepareStaticOptions(options []StaticOptions) StaticOptions {
  28. var opt StaticOptions
  29. if len(options) > 0 {
  30. opt = options[0]
  31. }
  32. // Defaults
  33. if len(opt.IndexFile) == 0 {
  34. opt.IndexFile = "index.html"
  35. }
  36. // Normalize the prefix if provided
  37. if opt.Prefix != "" {
  38. // Ensure we have a leading '/'
  39. if opt.Prefix[0] != '/' {
  40. opt.Prefix = "/" + opt.Prefix
  41. }
  42. // Remove any trailing '/'
  43. opt.Prefix = strings.TrimRight(opt.Prefix, "/")
  44. }
  45. return opt
  46. }
  47. // Static returns a middleware handler that serves static files in the given directory.
  48. func Static(directory string, staticOpt ...StaticOptions) martini.Handler {
  49. if runtime.GOOS == "windows" {
  50. if len(directory) < 2 || directory[1] != ':' {
  51. directory = path.Join(setting.StaticRootPath, directory)
  52. }
  53. } else if !path.IsAbs(directory) {
  54. directory = path.Join(setting.StaticRootPath, directory)
  55. }
  56. dir := http.Dir(directory)
  57. opt := prepareStaticOptions(staticOpt)
  58. return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
  59. if req.Method != "GET" && req.Method != "HEAD" {
  60. return
  61. }
  62. file := req.URL.Path
  63. // if we have a prefix, filter requests by stripping the prefix
  64. if opt.Prefix != "" {
  65. if !strings.HasPrefix(file, opt.Prefix) {
  66. return
  67. }
  68. file = file[len(opt.Prefix):]
  69. if file != "" && file[0] != '/' {
  70. return
  71. }
  72. }
  73. f, err := dir.Open(file)
  74. if err != nil {
  75. // discard the error?
  76. return
  77. }
  78. defer f.Close()
  79. fi, err := f.Stat()
  80. if err != nil {
  81. return
  82. }
  83. // try to serve index file
  84. if fi.IsDir() {
  85. // redirect if missing trailing slash
  86. if !strings.HasSuffix(req.URL.Path, "/") {
  87. http.Redirect(res, req, req.URL.Path+"/", http.StatusFound)
  88. return
  89. }
  90. file = path.Join(file, opt.IndexFile)
  91. f, err = dir.Open(file)
  92. if err != nil {
  93. return
  94. }
  95. defer f.Close()
  96. fi, err = f.Stat()
  97. if err != nil || fi.IsDir() {
  98. return
  99. }
  100. }
  101. if !opt.SkipLogging {
  102. log.Println("[Static] Serving " + file)
  103. }
  104. // Add an Expires header to the static content
  105. if opt.Expires != nil {
  106. res.Header().Set("Expires", opt.Expires())
  107. }
  108. http.ServeContent(res, req, file, fi.ModTime(), f)
  109. }
  110. }