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.

80 lines
1.6 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "net/url"
  5. "strings"
  6. "github.com/zenazn/goji/web"
  7. )
  8. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  9. fileName := c.URLParams["name"]
  10. err := checkFile(fileName)
  11. if err == NotFoundErr {
  12. notFoundHandler(c, w, r)
  13. return
  14. } else if err == BadMetadata {
  15. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  16. return
  17. }
  18. if !Config.allowHotlink {
  19. referer := r.Header.Get("Referer")
  20. u, _ := url.Parse(referer)
  21. p, _ := url.Parse(getSiteURL(r))
  22. if referer != "" && !sameOrigin(u, p) {
  23. http.Redirect(w, r, Config.sitePath+fileName, 303)
  24. return
  25. }
  26. }
  27. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  28. fileBackend.ServeFile(fileName, w, r)
  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 = Config.sitePath + "/static/images/favicon.gif"
  38. }
  39. filePath := strings.TrimPrefix(path, Config.sitePath+"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. _, err := fileBackend.Exists(filename)
  53. if err != nil {
  54. return NotFoundErr
  55. }
  56. expired, err := isFileExpired(filename)
  57. if err != nil {
  58. return err
  59. }
  60. if expired {
  61. fileBackend.Delete(filename)
  62. metaBackend.Delete(filename)
  63. return NotFoundErr
  64. }
  65. return nil
  66. }