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.

104 lines
2.5 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/andreimarcu/linx-server/backends"
  11. "github.com/andreimarcu/linx-server/expiry"
  12. "github.com/andreimarcu/linx-server/httputil"
  13. "github.com/zenazn/goji/web"
  14. )
  15. func fileServeHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  16. fileName := c.URLParams["name"]
  17. metadata, err := checkFile(fileName)
  18. if err == backends.NotFoundErr {
  19. notFoundHandler(c, w, r)
  20. return
  21. } else if err != nil {
  22. oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
  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. w.Header().Set("Content-Type", metadata.Mimetype)
  37. w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10))
  38. w.Header().Set("Etag", fmt.Sprintf("\"%s\"", metadata.Sha256sum))
  39. w.Header().Set("Cache-Control", "public, no-cache")
  40. modtime := time.Unix(0, 0)
  41. if done := httputil.CheckPreconditions(w, r, modtime); done == true {
  42. return
  43. }
  44. if r.Method != "HEAD" {
  45. _, reader, err := storageBackend.Get(fileName)
  46. if err != nil {
  47. oopsHandler(c, w, r, RespAUTO, "Unable to open file.")
  48. return
  49. }
  50. defer reader.Close()
  51. if _, err = io.CopyN(w, reader, metadata.Size); err != nil {
  52. oopsHandler(c, w, r, RespAUTO, err.Error())
  53. }
  54. }
  55. }
  56. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  57. path := r.URL.Path
  58. if path[len(path)-1:] == "/" {
  59. notFoundHandler(c, w, r)
  60. return
  61. } else {
  62. if path == "/favicon.ico" {
  63. path = Config.sitePath + "/static/images/favicon.gif"
  64. }
  65. filePath := strings.TrimPrefix(path, Config.sitePath+"static/")
  66. file, err := staticBox.Open(filePath)
  67. if err != nil {
  68. notFoundHandler(c, w, r)
  69. return
  70. }
  71. w.Header().Set("Etag", fmt.Sprintf("\"%s\"", timeStartedStr))
  72. w.Header().Set("Cache-Control", "public, max-age=86400")
  73. http.ServeContent(w, r, filePath, timeStarted, file)
  74. return
  75. }
  76. }
  77. func checkFile(filename string) (metadata backends.Metadata, err error) {
  78. metadata, err = storageBackend.Head(filename)
  79. if err != nil {
  80. return
  81. }
  82. if expiry.IsTsExpired(metadata.Expiry) {
  83. storageBackend.Delete(filename)
  84. err = backends.NotFoundErr
  85. return
  86. }
  87. return
  88. }