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.

85 lines
1.7 KiB

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