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.

103 lines
2.5 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 == backends.BadMetadata {
  19. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  20. return
  21. } else if err != nil {
  22. oopsHandler(c, w, r, RespAUTO, err.Error())
  23. return
  24. }
  25. if !Config.allowHotlink {
  26. referer := r.Header.Get("Referer")
  27. u, _ := url.Parse(referer)
  28. p, _ := url.Parse(getSiteURL(r))
  29. if referer != "" && !sameOrigin(u, p) {
  30. http.Redirect(w, r, Config.sitePath+fileName, 303)
  31. return
  32. }
  33. }
  34. w.Header().Set("Content-Security-Policy", Config.fileContentSecurityPolicy)
  35. w.Header().Set("Referrer-Policy", Config.fileReferrerPolicy)
  36. _, reader, err := storageBackend.Get(fileName)
  37. if err != nil {
  38. oopsHandler(c, w, r, RespAUTO, err.Error())
  39. }
  40. w.Header().Set("Content-Type", metadata.Mimetype)
  41. w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10))
  42. w.Header().Set("Etag", metadata.Sha256sum)
  43. w.Header().Set("Cache-Control", "max-age=0")
  44. // TODO: implement If-Match, If-None-Match (ETag)
  45. // TODO: implement If-Unmodified-Since, If-Modified-Since (Last-Modified)
  46. // TODO: implement range requests?
  47. if r.Method != "HEAD" {
  48. defer reader.Close()
  49. if _, err = io.CopyN(w, reader, metadata.Size); err != nil {
  50. oopsHandler(c, w, r, RespAUTO, err.Error())
  51. }
  52. }
  53. }
  54. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  55. path := r.URL.Path
  56. if path[len(path)-1:] == "/" {
  57. notFoundHandler(c, w, r)
  58. return
  59. } else {
  60. if path == "/favicon.ico" {
  61. path = Config.sitePath + "/static/images/favicon.gif"
  62. }
  63. filePath := strings.TrimPrefix(path, Config.sitePath+"static/")
  64. file, err := staticBox.Open(filePath)
  65. if err != nil {
  66. notFoundHandler(c, w, r)
  67. return
  68. }
  69. w.Header().Set("Etag", timeStartedStr)
  70. w.Header().Set("Cache-Control", "max-age=86400")
  71. http.ServeContent(w, r, filePath, timeStarted, file)
  72. return
  73. }
  74. }
  75. func checkFile(filename string) (metadata backends.Metadata, err error) {
  76. metadata, err = storageBackend.Head(filename)
  77. if err != nil {
  78. return
  79. }
  80. if expiry.IsTsExpired(metadata.Expiry) {
  81. storageBackend.Delete(filename)
  82. err = backends.NotFoundErr
  83. return
  84. }
  85. return
  86. }