api_statics.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package api
  7. import (
  8. "fmt"
  9. "net/http"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/api/auto"
  15. "github.com/syncthing/syncthing/lib/assets"
  16. "github.com/syncthing/syncthing/lib/config"
  17. "github.com/syncthing/syncthing/lib/sync"
  18. )
  19. const themePrefix = "theme-assets/"
  20. type staticsServer struct {
  21. assetDir string
  22. assets map[string]assets.Asset
  23. availableThemes []string
  24. mut sync.RWMutex
  25. theme string
  26. lastThemeChange time.Time
  27. untrusted bool
  28. }
  29. func newStaticsServer(theme, assetDir string, untrusted bool) *staticsServer {
  30. s := &staticsServer{
  31. assetDir: assetDir,
  32. assets: auto.Assets(),
  33. mut: sync.NewRWMutex(),
  34. theme: theme,
  35. lastThemeChange: time.Now().UTC(),
  36. untrusted: untrusted,
  37. }
  38. seen := make(map[string]struct{})
  39. // Load themes from compiled in assets.
  40. for file := range auto.Assets() {
  41. theme := strings.Split(file, "/")[0]
  42. if _, ok := seen[theme]; !ok {
  43. seen[theme] = struct{}{}
  44. s.availableThemes = append(s.availableThemes, theme)
  45. }
  46. }
  47. if assetDir != "" {
  48. // Load any extra themes from the asset override dir.
  49. for _, dir := range dirNames(assetDir) {
  50. if _, ok := seen[dir]; !ok {
  51. seen[dir] = struct{}{}
  52. s.availableThemes = append(s.availableThemes, dir)
  53. }
  54. }
  55. }
  56. if untrusted {
  57. l.Infoln(`Feature flag "untrusted":`, untrusted)
  58. }
  59. return s
  60. }
  61. func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  62. switch r.URL.Path {
  63. case "/themes.json":
  64. s.serveThemes(w, r)
  65. default:
  66. s.serveAsset(w, r)
  67. }
  68. }
  69. func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
  70. w.Header().Set("Cache-Control", "no-cache, must-revalidate")
  71. file := r.URL.Path
  72. if file[0] == '/' {
  73. file = file[1:]
  74. }
  75. if len(file) == 0 {
  76. file = "index.html"
  77. }
  78. s.mut.RLock()
  79. theme := s.theme
  80. modificationTime := s.lastThemeChange
  81. untrusted := s.untrusted
  82. s.mut.RUnlock()
  83. // If path starts with special prefix, get theme and file from path
  84. if strings.HasPrefix(file, themePrefix) {
  85. path := file[len(themePrefix):]
  86. i := strings.IndexRune(path, '/')
  87. if i == -1 {
  88. http.NotFound(w, r)
  89. return
  90. }
  91. theme = path[:i]
  92. file = path[i+1:]
  93. }
  94. // Check for an override for the current theme.
  95. if untrusted && s.serveFromAssetDir(file, theme+"/untrusted", w, r) {
  96. l.Debugln("serving", file, "from override untrusted")
  97. return
  98. }
  99. if s.serveFromAssetDir(file, theme, w, r) {
  100. return
  101. }
  102. // Check for a compiled in asset for the current theme.
  103. if untrusted && s.serveFromAssets(file, theme+"/untrusted", modificationTime, w, r) {
  104. l.Debugln("serving", file, "from compiled untrusted")
  105. return
  106. }
  107. if s.serveFromAssets(file, theme, modificationTime, w, r) {
  108. return
  109. }
  110. // Check for an overridden default asset.
  111. if untrusted && s.serveFromAssetDir(file, config.DefaultTheme+"/untrusted", w, r) {
  112. l.Debugln("serving", file, "from override untrusted")
  113. return
  114. }
  115. if s.serveFromAssetDir(file, config.DefaultTheme, w, r) {
  116. return
  117. }
  118. // Check for a compiled in default asset.
  119. if untrusted && s.serveFromAssets(file, config.DefaultTheme+"/untrusted", modificationTime, w, r) {
  120. l.Debugln("serving", file, "from compiled untrusted")
  121. return
  122. }
  123. if s.serveFromAssets(file, config.DefaultTheme, modificationTime, w, r) {
  124. return
  125. }
  126. http.NotFound(w, r)
  127. }
  128. func (s *staticsServer) serveFromAssetDir(file, theme string, w http.ResponseWriter, r *http.Request) bool {
  129. if s.assetDir == "" {
  130. return false
  131. }
  132. p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
  133. if _, err := os.Stat(p); err != nil {
  134. return false
  135. }
  136. mtype := assets.MimeTypeForFile(file)
  137. if len(mtype) != 0 {
  138. w.Header().Set("Content-Type", mtype)
  139. }
  140. http.ServeFile(w, r, p)
  141. return true
  142. }
  143. func (s *staticsServer) serveFromAssets(file, theme string, modificationTime time.Time, w http.ResponseWriter, r *http.Request) bool {
  144. as, ok := s.assets[theme+"/"+file]
  145. if !ok {
  146. return false
  147. }
  148. as.Modified = modificationTime
  149. assets.Serve(w, r, as)
  150. return true
  151. }
  152. func (s *staticsServer) serveThemes(w http.ResponseWriter, r *http.Request) {
  153. sendJSON(w, map[string][]string{
  154. "themes": s.availableThemes,
  155. })
  156. }
  157. func (s *staticsServer) setTheme(theme string) {
  158. s.mut.Lock()
  159. s.theme = theme
  160. s.lastThemeChange = time.Now().UTC()
  161. s.mut.Unlock()
  162. }
  163. func (s *staticsServer) setUntrusted(enabled bool) {
  164. s.mut.Lock()
  165. l.Infoln(`Feature flag "untrusted":`, enabled)
  166. s.untrusted = enabled
  167. s.mut.Unlock()
  168. }
  169. func (s *staticsServer) String() string {
  170. return fmt.Sprintf("staticsServer@%p", s)
  171. }