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.

82 lines
1.6 KiB

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