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.

81 lines
1.7 KiB

  1. package main
  2. import (
  3. "net/http"
  4. "net/url"
  5. "strings"
  6. "github.com/andreimarcu/linx-server/backends"
  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. err := checkFile(fileName)
  12. if err == NotFoundErr {
  13. notFoundHandler(c, w, r)
  14. return
  15. } else if err == backends.BadMetadata {
  16. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  17. return
  18. }
  19. if !Config.allowHotlink {
  20. referer := r.Header.Get("Referer")
  21. u, _ := url.Parse(referer)
  22. p, _ := url.Parse(getSiteURL(r))
  23. if referer != "" && !sameOrigin(u, p) {
  24. http.Redirect(w, r, Config.sitePath+fileName, 303)
  25. return
  26. }
  27. }
  28. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  29. fileBackend.ServeFile(fileName, w, r)
  30. }
  31. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  32. path := r.URL.Path
  33. if path[len(path)-1:] == "/" {
  34. notFoundHandler(c, w, r)
  35. return
  36. } else {
  37. if path == "/favicon.ico" {
  38. path = Config.sitePath + "/static/images/favicon.gif"
  39. }
  40. filePath := strings.TrimPrefix(path, Config.sitePath+"static/")
  41. file, err := staticBox.Open(filePath)
  42. if err != nil {
  43. notFoundHandler(c, w, r)
  44. return
  45. }
  46. w.Header().Set("Etag", timeStartedStr)
  47. w.Header().Set("Cache-Control", "max-age=86400")
  48. http.ServeContent(w, r, filePath, timeStarted, file)
  49. return
  50. }
  51. }
  52. func checkFile(filename string) error {
  53. _, err := fileBackend.Exists(filename)
  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. fileBackend.Delete(filename)
  63. metaStorageBackend.Delete(filename)
  64. return NotFoundErr
  65. }
  66. return nil
  67. }