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.

91 lines
2.0 KiB

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