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.

82 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. w.Header().Set("Referrer-Policy", Config.fileReferrerPolicy)
  30. fileBackend.ServeFile(fileName, w, r)
  31. }
  32. func staticHandler(c web.C, w http.ResponseWriter, r *http.Request) {
  33. path := r.URL.Path
  34. if path[len(path)-1:] == "/" {
  35. notFoundHandler(c, w, r)
  36. return
  37. } else {
  38. if path == "/favicon.ico" {
  39. path = Config.sitePath + "/static/images/favicon.gif"
  40. }
  41. filePath := strings.TrimPrefix(path, Config.sitePath+"static/")
  42. file, err := staticBox.Open(filePath)
  43. if err != nil {
  44. notFoundHandler(c, w, r)
  45. return
  46. }
  47. w.Header().Set("Etag", timeStartedStr)
  48. w.Header().Set("Cache-Control", "max-age=86400")
  49. http.ServeContent(w, r, filePath, timeStarted, file)
  50. return
  51. }
  52. }
  53. func checkFile(filename string) error {
  54. _, err := fileBackend.Exists(filename)
  55. if err != nil {
  56. return NotFoundErr
  57. }
  58. expired, err := isFileExpired(filename)
  59. if err != nil {
  60. return err
  61. }
  62. if expired {
  63. fileBackend.Delete(filename)
  64. metaStorageBackend.Delete(filename)
  65. return NotFoundErr
  66. }
  67. return nil
  68. }