Contains the Concourse pipeline definition for building a line-server container
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.

95 lines
1.9 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "io"
  5. "net/http"
  6. "os"
  7. "path"
  8. "strings"
  9. "github.com/zenazn/goji/web"
  10. )
  11. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  12. fileName := c.URLParams["name"]
  13. filePath := path.Join(Config.filesDir, fileName)
  14. err := checkFile(fileName)
  15. if err == NotFoundErr {
  16. notFoundHandler(c, w, r)
  17. return
  18. } else if err == BadMetadata {
  19. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  20. return
  21. }
  22. if !Config.allowHotlink {
  23. referer := r.Header.Get("Referer")
  24. prefix := strings.TrimSuffix(Config.siteURL, "/")
  25. if referer != "" && !strings.HasPrefix(referer, prefix) {
  26. w.WriteHeader(403)
  27. return
  28. }
  29. }
  30. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  31. http.ServeFile(w, r, filePath)
  32. }
  33. var staticCache = make(map[string][]byte)
  34. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  35. path := r.URL.Path
  36. if path[len(path)-1:] == "/" {
  37. notFoundHandler(c, w, r)
  38. return
  39. } else {
  40. if path == "/favicon.ico" {
  41. path = "/static/images/favicon.gif"
  42. }
  43. filePath := strings.TrimPrefix(path, "/static/")
  44. _, exists := staticCache[filePath]
  45. if !exists {
  46. file, err := staticBox.Open(filePath)
  47. if err != nil {
  48. notFoundHandler(c, w, r)
  49. return
  50. }
  51. buf := bytes.NewBuffer(nil)
  52. io.Copy(buf, file)
  53. staticCache[filePath] = buf.Bytes()
  54. }
  55. w.Header().Set("Etag", timeStartedStr)
  56. w.Header().Set("Cache-Control", "max-age=86400")
  57. http.ServeContent(w, r, filePath, timeStarted, bytes.NewReader(staticCache[filePath]))
  58. return
  59. }
  60. }
  61. func checkFile(filename string) error {
  62. filePath := path.Join(Config.filesDir, filename)
  63. _, err := os.Stat(filePath)
  64. if err != nil {
  65. return NotFoundErr
  66. }
  67. expired, err := isFileExpired(filename)
  68. if err != nil {
  69. return err
  70. }
  71. if expired {
  72. os.Remove(path.Join(Config.filesDir, filename))
  73. os.Remove(path.Join(Config.metaDir, filename))
  74. return NotFoundErr
  75. }
  76. return nil
  77. }