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.

94 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. if referer != "" && !strings.HasPrefix(referer, Config.siteURL) {
  25. w.WriteHeader(403)
  26. return
  27. }
  28. }
  29. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  30. http.ServeFile(w, r, filePath)
  31. }
  32. var staticCache = make(map[string][]byte)
  33. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  34. path := r.URL.Path
  35. if path[len(path)-1:] == "/" {
  36. notFoundHandler(c, w, r)
  37. return
  38. } else {
  39. if path == "/favicon.ico" {
  40. path = "/static/images/favicon.gif"
  41. }
  42. filePath := strings.TrimPrefix(path, "/static/")
  43. _, exists := staticCache[filePath]
  44. if !exists {
  45. file, err := staticBox.Open(filePath)
  46. if err != nil {
  47. notFoundHandler(c, w, r)
  48. return
  49. }
  50. buf := bytes.NewBuffer(nil)
  51. io.Copy(buf, file)
  52. staticCache[filePath] = buf.Bytes()
  53. }
  54. w.Header().Set("Etag", timeStartedStr)
  55. w.Header().Set("Cache-Control", "max-age=86400")
  56. http.ServeContent(w, r, filePath, timeStarted, bytes.NewReader(staticCache[filePath]))
  57. return
  58. }
  59. }
  60. func checkFile(filename string) error {
  61. filePath := path.Join(Config.filesDir, filename)
  62. _, err := os.Stat(filePath)
  63. if err != nil {
  64. return NotFoundErr
  65. }
  66. expired, err := isFileExpired(filename)
  67. if err != nil {
  68. return err
  69. }
  70. if expired {
  71. os.Remove(path.Join(Config.filesDir, filename))
  72. os.Remove(path.Join(Config.metaDir, filename))
  73. return NotFoundErr
  74. }
  75. return nil
  76. }