api_statics.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. "compress/gzip"
  9. "fmt"
  10. "io"
  11. "mime"
  12. "net/http"
  13. "os"
  14. "path/filepath"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/syncthing/syncthing/lib/auto"
  19. "github.com/syncthing/syncthing/lib/config"
  20. "github.com/syncthing/syncthing/lib/sync"
  21. )
  22. const themePrefix = "theme-assets/"
  23. type staticsServer struct {
  24. assetDir string
  25. assets map[string]string
  26. availableThemes []string
  27. mut sync.RWMutex
  28. theme string
  29. lastThemeChange time.Time
  30. }
  31. func newStaticsServer(theme, assetDir string) *staticsServer {
  32. s := &staticsServer{
  33. assetDir: assetDir,
  34. assets: auto.Assets(),
  35. mut: sync.NewRWMutex(),
  36. theme: theme,
  37. lastThemeChange: time.Now().UTC(),
  38. }
  39. seen := make(map[string]struct{})
  40. // Load themes from compiled in assets.
  41. for file := range auto.Assets() {
  42. theme := strings.Split(file, "/")[0]
  43. if _, ok := seen[theme]; !ok {
  44. seen[theme] = struct{}{}
  45. s.availableThemes = append(s.availableThemes, theme)
  46. }
  47. }
  48. if assetDir != "" {
  49. // Load any extra themes from the asset override dir.
  50. for _, dir := range dirNames(assetDir) {
  51. if _, ok := seen[dir]; !ok {
  52. seen[dir] = struct{}{}
  53. s.availableThemes = append(s.availableThemes, dir)
  54. }
  55. }
  56. }
  57. return s
  58. }
  59. func (s *staticsServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  60. switch r.URL.Path {
  61. case "/themes.json":
  62. s.serveThemes(w, r)
  63. default:
  64. s.serveAsset(w, r)
  65. }
  66. }
  67. func (s *staticsServer) serveAsset(w http.ResponseWriter, r *http.Request) {
  68. w.Header().Set("Cache-Control", "no-cache, must-revalidate")
  69. file := r.URL.Path
  70. if file[0] == '/' {
  71. file = file[1:]
  72. }
  73. if len(file) == 0 {
  74. file = "index.html"
  75. }
  76. s.mut.RLock()
  77. theme := s.theme
  78. modificationTime := s.lastThemeChange
  79. s.mut.RUnlock()
  80. // If path starts with special prefix, get theme and file from path
  81. if strings.HasPrefix(file, themePrefix) {
  82. path := file[len(themePrefix):]
  83. i := strings.IndexRune(path, '/')
  84. if i == -1 {
  85. http.NotFound(w, r)
  86. return
  87. }
  88. theme = path[:i]
  89. file = path[i+1:]
  90. }
  91. // Check for an override for the current theme.
  92. if s.assetDir != "" {
  93. p := filepath.Join(s.assetDir, theme, filepath.FromSlash(file))
  94. if _, err := os.Stat(p); err == nil {
  95. mtype := s.mimeTypeForFile(file)
  96. if len(mtype) != 0 {
  97. w.Header().Set("Content-Type", mtype)
  98. }
  99. http.ServeFile(w, r, p)
  100. return
  101. }
  102. }
  103. // Check for a compiled in asset for the current theme.
  104. bs, ok := s.assets[theme+"/"+file]
  105. if !ok {
  106. // Check for an overridden default asset.
  107. if s.assetDir != "" {
  108. p := filepath.Join(s.assetDir, config.DefaultTheme, filepath.FromSlash(file))
  109. if _, err := os.Stat(p); err == nil {
  110. mtype := s.mimeTypeForFile(file)
  111. if len(mtype) != 0 {
  112. w.Header().Set("Content-Type", mtype)
  113. }
  114. http.ServeFile(w, r, p)
  115. return
  116. }
  117. }
  118. // Check for a compiled in default asset.
  119. bs, ok = s.assets[config.DefaultTheme+"/"+file]
  120. if !ok {
  121. http.NotFound(w, r)
  122. return
  123. }
  124. }
  125. etag := fmt.Sprintf("%d", modificationTime.Unix())
  126. w.Header().Set("Last-Modified", modificationTime.Format(http.TimeFormat))
  127. w.Header().Set("Etag", etag)
  128. if t, err := time.Parse(http.TimeFormat, r.Header.Get("If-Modified-Since")); err == nil {
  129. if modificationTime.Equal(t) || modificationTime.Before(t) {
  130. w.WriteHeader(http.StatusNotModified)
  131. return
  132. }
  133. }
  134. if match := r.Header.Get("If-None-Match"); match != "" {
  135. if strings.Contains(match, etag) {
  136. w.WriteHeader(http.StatusNotModified)
  137. return
  138. }
  139. }
  140. mtype := s.mimeTypeForFile(file)
  141. if len(mtype) != 0 {
  142. w.Header().Set("Content-Type", mtype)
  143. }
  144. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  145. w.Header().Set("Content-Encoding", "gzip")
  146. w.Header().Set("Content-Length", strconv.Itoa(len(bs)))
  147. io.WriteString(w, bs)
  148. } else {
  149. // ungzip if browser not send gzip accepted header
  150. var gr *gzip.Reader
  151. gr, _ = gzip.NewReader(strings.NewReader(bs))
  152. io.Copy(w, gr)
  153. gr.Close()
  154. }
  155. }
  156. func (s *staticsServer) serveThemes(w http.ResponseWriter, r *http.Request) {
  157. sendJSON(w, map[string][]string{
  158. "themes": s.availableThemes,
  159. })
  160. }
  161. func (s *staticsServer) mimeTypeForFile(file string) string {
  162. // We use a built in table of the common types since the system
  163. // TypeByExtension might be unreliable. But if we don't know, we delegate
  164. // to the system. All our files are UTF-8.
  165. ext := filepath.Ext(file)
  166. switch ext {
  167. case ".htm", ".html":
  168. return "text/html; charset=utf-8"
  169. case ".css":
  170. return "text/css; charset=utf-8"
  171. case ".js":
  172. return "application/javascript; charset=utf-8"
  173. case ".json":
  174. return "application/json; charset=utf-8"
  175. case ".png":
  176. return "image/png"
  177. case ".ttf":
  178. return "application/x-font-ttf"
  179. case ".woff":
  180. return "application/x-font-woff"
  181. case ".svg":
  182. return "image/svg+xml; charset=utf-8"
  183. default:
  184. return mime.TypeByExtension(ext)
  185. }
  186. }
  187. func (s *staticsServer) setTheme(theme string) {
  188. s.mut.Lock()
  189. s.theme = theme
  190. s.lastThemeChange = time.Now().UTC()
  191. s.mut.Unlock()
  192. }
  193. func (s *staticsServer) String() string {
  194. return fmt.Sprintf("staticsServer@%p", s)
  195. }