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.

96 lines
2.2 KiB

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